· 7 years ago · Aug 07, 2018, 07:56 PM
1HMC SHA1 hash - Java producing different hash output than C#
2var privateKey = Configuration.RecurlySection.Current.PrivateKey;
3var hashedKey = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(privateKey));
4var hmac = new HMACSHA1(hashedKey);
5var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(dataToProtect));
6return BitConverter.ToString(hash).Replace("-", "").ToLower();
7
8String unencryptedMessage = "[1312701386,transactioncreate,[account_code:ABC,amount_in_cents:5000,currency:USD]]";
9String privateKey = "0123456789ABCDEF0123456789ABCDEF";
10String encryptedMessage = getHMACSHA1(unencryptedMessage, getSHA1(privateKey));
11
12private static byte[] getSHA1(String source) throws NoSuchAlgorithmException, UnsupportedEncodingException{
13 MessageDigest md = MessageDigest.getInstance("SHA-1");
14 byte[] bytes = md.digest(source.getBytes("UTF-8"));
15 return bytes;
16}
17
18private static String getHMACSHA1(String baseString, byte[] keyBytes) throws GeneralSecurityException, UnsupportedEncodingException {
19 SecretKey secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
20 Mac mac = Mac.getInstance("HmacSHA1");
21 mac.init(secretKey);
22 byte[] bytes = baseString.getBytes("ASCII");
23 return Hex.encodeHexString(mac.doFinal(bytes));
24}
25
26string message = "[1312701386,transactioncreate,[account_code:ABC,amount_in_cents:5000,currency:USD]]";
27string privateKey = "0123456789ABCDEF0123456789ABCDEF";
28
29var hashedKey = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(privateKey));
30var hmac = new HMACSHA1(hashedKey);
31var hash = hmac.ComputeHash(Encoding.ASCII.GetBytes(message));
32
33Console.WriteLine(" Message: {0}", message);
34Console.WriteLine(" Key: {0}n", privateKey);
35Console.WriteLine("Key bytes: {0}", BitConverter.ToString(hashedKey).Replace("-", "").ToLower());
36Console.WriteLine(" Result: {0}", BitConverter.ToString(hash).Replace("-", "").ToLower());
37
38String message = "[1312701386,transactioncreate,[account_code:ABC,amount_in_cents:5000,currency:USD]]";
39String privateKey = "0123456789ABCDEF0123456789ABCDEF";
40
41MessageDigest md = MessageDigest.getInstance("SHA-1");
42byte[] keyBytes = md.digest(privateKey.getBytes("UTF-8"));
43
44SecretKey sk = new SecretKeySpec(keyBytes, "HmacSHA1");
45Mac mac = Mac.getInstance("HmacSHA1");
46mac.init(sk);
47byte[] result = mac.doFinal(message.getBytes("ASCII"));
48
49System.out.println(" Message: " + message);
50System.out.println(" Key: " + privateKey + "n");
51System.out.println("Key Bytes: " + toHex(keyBytes));
52System.out.println(" Results: " + toHex(result));
53
54String message = "[1312701386,transactioncreate,[account_code:ABC,amount_in_cents:5000,currency:USD]]";
55String privateKey = "0123456789ABCDEF0123456789ABCDEF";
56
57MessageDigest md = MessageDigest.getInstance("SHA-1");
58byte[] keyBytes = md.digest(privateKey.getBytes("UTF-8"));
59
60SecretKey sk = new SecretKeySpec(keyBytes, "HmacSHA1");
61Mac mac = Mac.getInstance("HmacSHA1");
62mac.init(sk);
63byte[] result = mac.doFinal(message.getBytes("ASCII"));
64
65System.out.println(" Message: " + message);
66System.out.println(" Key: " + privateKey + "n");
67System.out.println("Key Bytes: " + toHex(keyBytes));
68System.out.println(" Results: " + toHex(result));
69
70GetBytes