(Object request2, Object secret2, Object algorithm2, String digest)
| 114 | // ==================================================== |
| 115 | |
| 116 | public static String Hmac(Object request2, Object secret2, Object algorithm2, String digest) { |
| 117 | byte[] request = (request2 instanceof String) ? toUtf8(request2) : (byte[]) request2; |
| 118 | byte[] secret = (secret2 instanceof String) ? toUtf8(secret2) : (byte[]) secret2; |
| 119 | |
| 120 | if (digest == null) { |
| 121 | digest = "hex"; // default |
| 122 | } |
| 123 | |
| 124 | String algo = "md5"; |
| 125 | if (algorithm2 != null) { |
| 126 | algo = toString(algorithm2); // pass "sha256"/"sha512"/"sha384"/"md5" |
| 127 | } |
| 128 | |
| 129 | byte[] sig; |
| 130 | switch (algo) { |
| 131 | case "sha256": sig = hmac("HmacSHA256", request, secret); break; |
| 132 | case "sha512": sig = hmac("HmacSHA512", request, secret); break; |
| 133 | case "sha384": sig = hmac("HmacSHA384", request, secret); break; |
| 134 | case "md5": sig = hmac("HmacMD5", request, secret); break; |
| 135 | default: throw new IllegalArgumentException("Unsupported HMAC algo: " + algo); |
| 136 | } |
| 137 | return "hex".equals(digest) ? binaryToHex(sig) : BinaryToBase64(sig); |
| 138 | } |
| 139 | |
| 140 | private static byte[] hmac(String jceName, byte[] data, byte[] key) { |
| 141 | try { |
no test coverage detected