HMAC-SHA-256 (RFC 2104) is used for checking the integrity of the data sent between the systems. The flow for the creation of the sign is the following:
- A message is created as a concatenation of parameters in a specified order.
- A HMAC-SHA-256 code (32 bytes) is generated using a key, which is associated with the customer.
- The code is converted into a string that is a hexadecimal representation of the code (64 uppercase characters).
Code Samples
Test your sign computing implementation for a sample operation using the following data:
PARAMETER | VALUE |
name | Player01 |
amount | 12.30 |
currency | USD |
reference | 473285 |
True |
Concatenating parameters we get input into the hash function Player0112.30USD473285True.
Please note that the amount always has to be adjusted to exactly 2 decimal places.
Using the hash function and secret key 1234567890 we get hash (converted to hexadecimal).
4786EEBD8BCBED542D4402A69378AB279494C62A9E96019A20A59F8D2B4F8A98.
HMAC Generator website - https://www.freeformatter.com/hmac-generator.html
Below we provide examples of code computing SIGN in some of the major programming languages.
.NET framework 2.0 (C#)
public static string GetSign(string key, string message) {
System.Security.Cryptography.HMAC hmac =
System.Security.Cryptography.HMAC.Create("HMACSHA256");
hmac.Key = System.Text.Encoding.UTF8.GetBytes(key);
byte[] hash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(message));
return BitConverter.ToString(hash).Replace("-", "").ToUpperInvariant();
}
PHP
function GetSign($key, $message) {
return strtoupper(hash_hmac('sha256', pack('A*', $message), pack('A*', $key)));
}
JAVA
public static String GetSign(String key, String message) throws Exception {
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA256");
byte[] keyBytes = key.getBytes("UTF-8");
byte[] messageBytes = message.getBytes("UTF-8");
mac.init(new javax.crypto.spec.SecretKeySpec(keyBytes, mac.getAlgorithm()));
return ByteArray2HexString(mac.doFinal(messageBytes));
}
public static String ByteArray2HexString(byte[] b) {
java.math.BigInteger bi = new java.math.BigInteger(1, b);
return String.format("%0" + (b.length << 1) + "X", bi);
}
Comments
0 comments
Article is closed for comments.