Nagarajan S R
Nagarajan S R

Reputation: 1436

Output of SHA 1 Checksum using java not matching with example given

I am writing a code to generate SHA-1 check sum using java. I referred to this link http://code.wikia.com/wiki/SHA_checksum. My java code is as below:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHAHashing{

    public static void main(String[] args)throws Exception{
            String password = "ABC0010|txnpassword|0|Test Reference|1.00|20110616221931";
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(password.getBytes("UTF-8")); 
            System.out.println("Converting SHA digest output to Hex String : "+byteArrayToHexString(SHAsum(password.getBytes("UTF-8"))));
            System.out.println("Converting md.digest output to Hex String  : "+byteArrayToHexString(md.digest()));
    }

    public static byte[] SHAsum(byte[] convertme) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md.digest(convertme);
    }

    public static String byteArrayToHexString(byte[] b) {
         String result = "";
         for (int i=0; i < b.length; i++) {
             result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
         }
         return result;
    }
}

The output of the above is:

Converting SHA digest output to Hex String : 7871d5c9a366339da848fc64cb32f6a9ad8fcadd
Converting md.digest output to Hex String : 7871d5c9a366339da848fc64cb32f6a9ad8fcadd

I have a input string : "ABC0010|txnpassword|0|Test Reference|1.00|20110616221931" Whose corresponding output is : 01a1edbb159aa01b99740508d79620251c2f871d as per the document I am using for reference to generate a fingerprint.

Can anyone provide an insight on the above please.

Upvotes: 1

Views: 834

Answers (1)

Devon_C_Miller
Devon_C_Miller

Reputation: 16518

This is a case of it being helpful to include referenced documents. The example appears that it may come from this document: http://www.securepay.com.au/uploads/Integration%20Guides/SecureFrame_Integration_Guide.pdf

Section 3.3.5 of that document, "Transaction Amount," specifies that the amount field must be in the "base unit of currency". So that 1.00 on your string needs to be in cents, not dollars.

If you change the 1.00 to 100 you will get the SHA-1 sum that document expects.

However, that is neither of the sums you report.

Recheck your documentation and verify you're not missing a transformation on the underlying data.

Upvotes: 1

Related Questions