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.

2
  • 7
    This is a hash, it is not encryption.
    – Mark Keats
    Commented Aug 17, 2016 at 8:16
  • 2
    One is hex, the other is base64 encoded. If you didn't want base64 encoded output, why did you call the base64encode function? Commented Aug 17, 2016 at 8:32

1 Answer 1

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.

1
  • 3
    This is correct. To clarify you could maybe add a small description of the difference between base64encode() and convertToHex(). Commented Aug 17, 2016 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .