|
| 1 | +package net.authorize.sample.Sha512; |
| 2 | + |
| 3 | +import java.security.*; |
| 4 | +import java.util.Formatter; |
| 5 | + |
| 6 | +import javax.crypto.Mac; |
| 7 | +import javax.crypto.spec.SecretKeySpec; |
| 8 | + |
| 9 | +public class ComputeTransHashSHA2 { |
| 10 | + |
| 11 | + public static void main(String[] args) throws Exception { |
| 12 | + String signatureKey = "56E529FE6C63D60E545F84686096E6AA01D5E18A119F18A130F7CFB3983104216979E95D84C91BDD382AA0875264A63940A2D0AA5548F6023B4C78A9D52C18DA"; |
| 13 | + String transId = "60115868980"; |
| 14 | + String apiLogin = "5T9cRn9FK"; |
| 15 | + String amount = "15.00"; |
| 16 | + //transHashSHA2 represents the computed TransHash2 using SignatureKey for the given transaction |
| 17 | + //textToHash is formed by concatenating apilogin id , transId for the given transaction and transaction amount. |
| 18 | + // For more details please visit https://developer.authorize.net/support/hash_upgrade/?utm_campaign=19Q2%20MD5%20Hash%20EOL%20Partner&utm_medium=email&utm_source=Eloqua for implementation details. |
| 19 | + String transHashSHA2=getHMACSHA512(signatureKey, "^"+ apiLogin+"^"+ transId +"^"+ amount+"^"); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * This is method to generate HMAC512 key for a given signature key and Text to Hash |
| 24 | + * @param signatureKey |
| 25 | + * @param textToHash |
| 26 | + * @return |
| 27 | + * @throws Exception |
| 28 | + */ |
| 29 | + public static String getHMACSHA512(String signatureKey, String textToHash)throws Exception |
| 30 | + { |
| 31 | + // Check if Key is null or empty |
| 32 | + if (signatureKey==null||signatureKey.isEmpty()) |
| 33 | + throw new IllegalArgumentException("HMACSHA512: key Parameter cannot be empty."); |
| 34 | + //Check if texttoHash is null or empty |
| 35 | + if (textToHash==null||textToHash.isEmpty()) |
| 36 | + throw new IllegalArgumentException("HMACSHA512: textToHash Parameter cannot be empty."); |
| 37 | + |
| 38 | + // Signature Key size cannot be less than 2 or odd |
| 39 | + if (signatureKey.length() % 2 != 0 || signatureKey.trim().length() < 2) |
| 40 | + { |
| 41 | + throw new IllegalArgumentException("HMACSHA512: key Parameter cannot be odd or less than 2 characters."); |
| 42 | + } |
| 43 | + |
| 44 | + // Convert Hex string to byte array |
| 45 | + byte signatureKeyInByteArray[]=hexStringToByteArray(signatureKey); |
| 46 | + // Calculate hmac 512 for the byte array contents and text to hash. |
| 47 | + String hashedValue=calculateHMAC(signatureKeyInByteArray,textToHash); |
| 48 | + if(hashedValue==null) |
| 49 | + throw new NullPointerException("For Signature Key :["+signatureKey+"] And Text To Hash ["+textToHash+"] the generated HMAC512 Key is null .Please contact Authorize.net for more information"); |
| 50 | + |
| 51 | + return hashedValue.toUpperCase(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This is the method to Calculate Hmac sha512 for given signature signatureKey in byte array and text to hash in byte array |
| 56 | + * @param signatureKey |
| 57 | + * @param textToHash |
| 58 | + * @return |
| 59 | + * @throws SignatureException |
| 60 | + * @throws NoSuchAlgorithmException |
| 61 | + * @throws InvalidKeyException |
| 62 | + */ |
| 63 | + public static String calculateHMAC(byte signatureKey[], String textToHash) |
| 64 | + throws SignatureException, NoSuchAlgorithmException, InvalidKeyException |
| 65 | + { |
| 66 | + SecretKeySpec secretKeySpec = new SecretKeySpec(signatureKey, "HmacSHA512"); |
| 67 | + Mac mac = Mac.getInstance("HmacSHA512"); |
| 68 | + mac.init(secretKeySpec); |
| 69 | + return toHexString(mac.doFinal(textToHash.getBytes())); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * This is the method to convert byte array into hexadecimal string |
| 74 | + * @param bytes |
| 75 | + * @return hexadecimal string. |
| 76 | + */ |
| 77 | + private static String toHexString(byte[] bytes) { |
| 78 | + Formatter formatter = new Formatter(); |
| 79 | + for (byte b : bytes) { |
| 80 | + formatter.format("%02x", b); |
| 81 | + } |
| 82 | + return formatter.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * This is the method convert hexadecimal string to byte array |
| 87 | + * @param str |
| 88 | + * @return byte array |
| 89 | + */ |
| 90 | + public static byte[] hexStringToByteArray(String str) { |
| 91 | + int len = str.length(); |
| 92 | + byte[] data = new byte[len / 2]; |
| 93 | + for (int i = 0; i < len; i += 2) { |
| 94 | + data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4) |
| 95 | + + Character.digit(str.charAt(i+1), 16)); |
| 96 | + } |
| 97 | + return data; |
| 98 | + } |
| 99 | +} |
0 commit comments