(Object request2, Object hash, Object digest2)
| 156 | // ==================================================== |
| 157 | |
| 158 | public static Object Hash(Object request2, Object hash, Object digest2) { |
| 159 | String algorithm = (hash == null) ? null : toString(hash); |
| 160 | String digest = (digest2 == null) ? "hex" : toString(digest2); |
| 161 | |
| 162 | // If the caller already passed raw bytes, hash those directly. |
| 163 | // Otherwise treat the value as a string and hash its UTF-8 encoding. |
| 164 | byte[] input = (request2 instanceof byte[]) ? (byte[]) request2 : toUtf8(request2); |
| 165 | |
| 166 | byte[] signature; |
| 167 | switch (algorithm) { |
| 168 | case "sha256": signature = digestBytes("SHA-256", input); break; |
| 169 | case "sha512": signature = digestBytes("SHA-512", input); break; |
| 170 | case "sha384": signature = digestBytes("SHA-384", input); break; |
| 171 | case "sha1": signature = digestBytes("SHA-1", input); break; |
| 172 | case "md5": signature = digestBytes("MD5", input); break; |
| 173 | case "keccak": |
| 174 | case "sha3": signature = keccakDigest(input); break; |
| 175 | default: throw new IllegalArgumentException("Unsupported hash algo: " + algorithm); |
| 176 | } |
| 177 | |
| 178 | if ("binary".equals(digest)) return signature; |
| 179 | return "hex".equals(digest) ? binaryToHex(signature) : BinaryToBase64(signature); |
| 180 | } |
| 181 | |
| 182 | private static byte[] digestBytes(String algo, byte[] data) { |
| 183 | try { |
no test coverage detected