1

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Nagarajan S R
  • 1,418
  • 2
  • 19
  • 31
  • You should convert the byte array to hex string. See http://stackoverflow.com/a/4895572/130224 – reprogrammer Nov 28 '12 at 04:49
  • Hi reprogrammer, As per your direction I have modified my code as below. But still it doesn't procue the desired output. Please use the test string to produce the result. – Nagarajan S R Nov 28 '12 at 05:27
  • and How is it that the md.digest() produce a different result from the method SHAsum() when both are calling the same method. – Nagarajan S R Nov 28 '12 at 05:32
  • The first string is the sha1 sum of the second one. This is because the first string is the sha1 sum of sha1 sum of the input string and the second string is the sha1 sum of the input string. – reprogrammer Nov 28 '12 at 05:35
  • Oh yes.....! Your correct reprogrammer. I have edited my code again. Now I get the same output for both the methods. Converting SHA digest output to Hex String : 7871d5c9a366339da848fc64cb32f6a9ad8fcadd Converting md.digest output to Hex String : 7871d5c9a366339da848fc64cb32f6a9ad8fcadd But I am not able to produce an output digest for "ABC0010|txnpassword|0|Test Reference|1.00|20110616221931" Whose corresponding output is : "01a1edbb159aa01b99740508d79620251c2f871d" – Nagarajan S R Nov 28 '12 at 06:31
  • I guess using SHA-1 we can produce only one unique output for a particular input. If this is correct I guess the digest produced in the guide is wrong. But still I have a hitch in this. – Nagarajan S R Nov 28 '12 at 06:38

1 Answers1

1

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.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
  • Thank you for the reply Devon_C_Miller, Using the document you provided I am able to produce the same output as in the document. I am actually generating this for a fingerprint. Example: Setting the fingerprint Fields joined with a | separator: ABC0010|txnpassword|0|Test Reference|1.00|20110616221931 SHA1 the above string:01a1edbb159aa01b99740508d79620251c2f871d – Nagarajan S R Nov 30 '12 at 07:36