SHA1 Hash Results

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.

Answer

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.

Attribution
Source : Link , Question Author : Peeyush , Answer Author : Ratan Paul

Leave a Comment