0

I'm using a specification document for accessing API, and it says that hash is calculated on the signature generated from the following logic:

Signature = (field1 + field2 + field3 + field4 + field5 + field6) + (field7 + field8)

I'm just wondering what does this mean?

So, when I concatenate the fields and hash using sha-256, I'm getting a different hash format than the expected 32 byte; a sample hash has this format:

PajZG3NEUUHgrycwtPKcKkvTdBg/Kkx6OhlULgSV+ko= as opposed to this example:

c7477242d3901f537387b2b6c61099380634c013a060960a5bf4d87734d54f0e

This is my code:

stringToHash = "field1field2field3field4field5field6field7field8";
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(stringToHash.getBytes(StandardCharsets.UTF_8));
String encoded = Base64.getEncoder().encodeToString(hash);

What could I be missing?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367

1 Answers1

0

I found out the answer from this link: http://www.mytecbits.com/tools/cryptography/sha2generator This code does it:

String input = "String to hash";
MessageDigest objSHA = MessageDigest.getInstance("SHA-256");
byte[] bytSHA = objSHA.digest(input.getBytes());
BigInteger intNumber = new BigInteger(1, bytSHA);
String stringHashCode = intNumber.toString(16);

// pad with 0 if the hexa digits are less then 64.
 while (stringHashCode.length() < 64) {
    stringHashCode = "0" + stringHashCode;
}
  • So the difference is, do not specify character set when getting byte array from the raw string. Therefore, stringToHash.getBytes(StandardCharsets.UTF_8) becomes stringToHash.getBytes() Then converting the hash (byte array) to BigInteger, then to String. And it worked – Samuel Waithaka Feb 09 '18 at 14:01