/** * Utility method supporting both possible digest formats: Base64 and Hex */ private boolean digestMatches(byte[] digest, String providedDigest) { return providedDigest.equalsIgnoreCase(Hex.encodeHexString(digest)) || providedDigest.equalsIgnoreCase(new String(Base64.encode(digest))); }
/** * Returns the digest for the file. * * @param valueToDigest the file to use * @return the digest as a hex String * @throws IOException * If an I/O error occurs. * @since 1.11 */ public String hmacHex(final File valueToDigest) throws IOException { return Hex.encodeHexString(hmac(valueToDigest)); }
/** * Reads through a ByteBuffer and returns the digest for the data * * @param data * Data to digest * @return the digest as a hex string * * @since 1.11 */ public String digestAsHex(final ByteBuffer data) { return Hex.encodeHexString(digest(data)); }
/** * Calculates the SHA-1 digest and returns the value as a hex string. * * @param data * Data to digest * @return SHA-1 digest as a hex string * @since 1.7 */ public static String sha1Hex(final byte[] data) { return Hex.encodeHexString(sha1(data)); }
/** * Returns the digest for the input data. * * @param valueToDigest the input to use * @return the digest as a hex String * @since 1.11 */ public String hmacHex(final byte[] valueToDigest) { return Hex.encodeHexString(hmac(valueToDigest)); }
/** * Calculates the MD5 digest and returns the value as a 32 character hex string. * * @param data * Data to digest * @return MD5 digest as a hex string */ public static String md5Hex(final byte[] data) { return Hex.encodeHexString(md5(data)); }
/** * Calculates the MD5 digest and returns the value as a 32 character hex string. * * @param data * Data to digest * @return MD5 digest as a hex string */ public static String md5Hex(final String data) { return Hex.encodeHexString(md5(data)); }
/** * Reads through a byte array and returns the digest for the data. * * @param data * Data to digest * @return the digest as a hex string * @since 1.11 */ public String digestAsHex(final byte[] data) { return Hex.encodeHexString(digest(data)); }
/** * Reads through a byte array and returns the digest for the data. * * @param data * Data to digest treated as UTF-8 string * @return the digest as a hex string * @since 1.11 */ public String digestAsHex(final String data) { return Hex.encodeHexString(digest(data)); }
/** * Returns the digest for the input data. * * @param valueToDigest the input to use, treated as UTF-8 * @return the digest as a hex String * @since 1.11 */ public String hmacHex(final String valueToDigest) { return Hex.encodeHexString(hmac(valueToDigest)); }
/** * Returns the digest for the input data. * * @param valueToDigest the input to use * @return the digest as a hex String * @since 1.11 */ public String hmacHex(final ByteBuffer valueToDigest) { return Hex.encodeHexString(hmac(valueToDigest)); }
private void println(final String prefix, final byte[] digest, final String fileName) { // The standard appears to be to print // hex, space, then either space or '*' followed by filename // where '*' is used for binary files // shasum(1) has a -b option which generates " *" separator // we don't distinguish binary files at present System.out.println(prefix + Hex.encodeHexString(digest) + (fileName != null ? " " + fileName : "")); }
/** * Calculates the MD2 digest and returns the value as a 32 character hex string. * * @param data * Data to digest * @return MD2 digest as a hex string * @since 1.7 */ public static String md2Hex(final byte[] data) { return Hex.encodeHexString(md2(data)); }
/** * Calculates the SHA-1 digest and returns the value as a hex string. * * @param data * Data to digest * @return SHA-1 digest as a hex string * @since 1.7 */ public static String sha1Hex(final String data) { return Hex.encodeHexString(sha1(data)); }
/** * Calculates the MD2 digest and returns the value as a 32 character hex string. * * @param data * Data to digest * @return MD2 digest as a hex string * @since 1.7 */ public static String md2Hex(final String data) { return Hex.encodeHexString(md2(data)); }
@Test public void testEncodeHexByteString_ByteBufferOfZeroes() { final String c = Hex.encodeHexString(ByteBuffer.allocate(36)); assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", c); }
@Test public void testHmacSha1UpdateWithInpustream() throws IOException { final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); HmacUtils.updateHmac(mac, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, new ByteArrayInputStream("".getBytes())); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); }
@Test public void testEncodeHexByteString_ByteArrayOfZeroes() { final String c = Hex.encodeHexString(new byte[36]); assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", c); }
@Test public void testHmacSha1UpdateWithByteArray() throws IOException { final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, "".getBytes()); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); }
@Test public void testHmacSha1UpdateWithString() throws IOException { final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING); assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, ""); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); }