13

I am trying to encryption a string using SHA1 encryption. Test string

The quick brown fox jumps over the lazy dog

2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 (encryption token)

You can validate this here http://www.sha1-online.com/

But if I use same string in Salesforce with this code

blob hashSHA1 =Crypto.generateDigest('SHA1', blob.valueOf('The quick brown fox jumps over the lazy dog'));
String hashBase64SHA1 = EncodingUtil.base64encode (hashSHA1);
System.debug (hashBase64SHA1);

Then I get the output

L9ThxnotKPzthJ7hu3bnORuT6xI=

If anyone can let me know what I am doing wrong here or if I need to update anything or the correct method that would be helpful.

Peeyush
  • 901
  • 7
  • 20

1 Answers1

24

You Should use this

String shasign = EncodingUtil.convertToHex(Crypto.generateDigest('SHA1',Blob.valueOf(Your String)));

Clarification -:
base64Encode will simply convert a Blob to an unencoded String representing its normal form. It will not encode the blob in any form just simply return the String.

Whereas
convertToHex will return a hexadecimal (base 16) representation of the inputBlob.

Ratan Paul
  • 22,663
  • 13
  • 52
  • 97
Tarachand Karwal
  • 523
  • 4
  • 12
  • 3
    This is correct. To clarify you could maybe add a small description of the difference between base64encode() and convertToHex(). – user254875486 Aug 17 '16 at 08:26