Home > Net >  Change the input type of the sha1 alg
Change the input type of the sha1 alg

Time:10-03

I am faced with a curious task. I have to generate a SHA1 hash based on an input parameter. Input is the following input 303934393837353974570C61944628F63032313430303030303043384137 and the result of the sha1 hash should be 89b302d863e996d73f6fcbcf20489969ca4d593c.

But if I try following:

   var result = org.apache.commons.codec.digest.DigestUtils.sha1Hex( input);

The result will be e099219046775bc9528599c3077f7f49756f9bb1.

The curious is, thus the online converter SHA-Online-Tool if I change the input type, then the result will be as expected.

For any hint I am grateful!

CodePudding user response:

To achieve the output you get from the online tool by selecting the input type as hex. It means the input string is a hex of the original string. So you need to decode hex to a string first and calculate sha1.

org.apache.commons.codec.digest.DigestUtils.sha1Hex(org.apache.commons.codec.binary.Hex.decodeHex("303934393837353974570C61944628F63032313430303030303043384137"))

Will give you output 89b302d863e996d73f6fcbcf20489969ca4d593c

  • Related