| 67 | /// is no enforced minimum but for security 128-256 bits of entropy is |
| 68 | /// recommended. |
| 69 | public static Hmac create(String algorithm, byte[] key) { |
| 70 | MessageDigestImpl h = MessageDigestImpl.create(algorithm); |
| 71 | int blockSize = blockSizeFor(algorithm); |
| 72 | |
| 73 | byte[] k = key; |
| 74 | if (k.length > blockSize) { |
| 75 | h.update(k, 0, k.length); |
| 76 | k = h.digest(); |
| 77 | h.reset(); |
| 78 | } |
| 79 | byte[] paddedKey = new byte[blockSize]; |
| 80 | System.arraycopy(k, 0, paddedKey, 0, k.length); |
| 81 | |
| 82 | byte[] inner = new byte[blockSize]; |
| 83 | byte[] outer = new byte[blockSize]; |
| 84 | for (int i = 0; i < blockSize; i++) { |
| 85 | inner[i] = (byte) (paddedKey[i] ^ 0x36); |
| 86 | outer[i] = (byte) (paddedKey[i] ^ 0x5c); |
| 87 | } |
| 88 | return new Hmac(h, outer, inner); |
| 89 | } |
| 90 | |
| 91 | private static int blockSizeFor(String algorithm) { |
| 92 | String a = MessageDigestImpl.normalise(algorithm); |