Samples
The key in the sample code is the key preliminary agreed upon between the bank and the merchant, and the message is the value generated according to the given formula or combination of data.
Code
private static String encode(String key, String message) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] hmacArray = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder(hmacArray.length * 2);
for (byte b : hmacArray)
sb.append(String.format("%02x", b & 0xff));
return sb.toString();
}
function hmac($key, $message){
return hash_hmac('sha256', $message, $key);
}
var crypto = require("crypto");
function hmac256(key, message) {
let hash = crypto.createHmac("sha256", key).update(message);
return hash.digest("hex");
}
private static string HashHMACHex(string key, string message)
{
var encoding = new ASCIIEncoding();
var hash = new HMACSHA256(encoding.GetBytes(key));
return BitConverter.ToString(hash.ComputeHash(encoding.GetBytes(message))).Replace("-", "").ToLower();
}
import hmac
import hashlib
def hmac256(key,message):
digest_maker=hmac.new(key,message, hashlib.sha256)
return digest_maker.hexdigest()