/** * 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)); }
/** * 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)); }
/** * Reads through a File and returns the digest for the data * * @param data * Data to digest * @return the digest as a hex string * @throws IOException * On error reading from the stream * @since 1.11 */ public String digestAsHex(final File data) throws IOException { return Hex.encodeHexString(digest(data)); }
/** * Reads through an InputStream and returns the digest for the data * * @param data * Data to digest * @return the digest as a hex string * @throws IOException * On error reading from the stream * @since 1.11 */ public String digestAsHex(final InputStream data) throws IOException { return Hex.encodeHexString(digest(data)); }
private void run(final String prefix, final MessageDigest messageDigest, final File[] files) throws IOException { for (final File file : files) { if (file.isFile()) { println(prefix, DigestUtils.digest(messageDigest, file), file.getName()); } } }
/** * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. * * @param data * Data to digest * @return SHA-1 digest * @throws IOException * On error reading from the stream * @since 1.7 */ public static byte[] sha1(final InputStream data) throws IOException { return digest(getSha1Digest(), data); }
/** * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>. * * @param data * Data to digest * @return MD2 digest * @throws IOException * On error reading from the stream * @since 1.7 */ public static byte[] md2(final InputStream data) throws IOException { return digest(getMd2Digest(), data); }
/** * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. * * @param data * Data to digest * @return MD5 digest * @throws IOException * On error reading from the stream * @since 1.4 */ public static byte[] md5(final InputStream data) throws IOException { return digest(getMd5Digest(), data); }
/** * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>. * <p> * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. * </p> * * @param data * Data to digest * @return SHA-256 digest * @throws IOException * On error reading from the stream * @since 1.4 */ public static byte[] sha256(final InputStream data) throws IOException { return digest(getSha256Digest(), data); }
/** * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>. * <p> * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. * </p> * * @param data * Data to digest * @return SHA-384 digest * @throws IOException * On error reading from the stream * @since 1.4 */ public static byte[] sha384(final InputStream data) throws IOException { return digest(getSha384Digest(), data); }
/** * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>. * <p> * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. * </p> * * @param data * Data to digest * @return SHA-512 digest * @throws IOException * On error reading from the stream * @since 1.4 */ public static byte[] sha512(final InputStream data) throws IOException { return digest(getSha512Digest(), data); }
private void run(final String prefix, final MessageDigest messageDigest) throws IOException { if (inputs == null) { println(prefix, DigestUtils.digest(messageDigest, System.in)); return; } for(final String source : inputs) { final File file = new File(source); if (file.isFile()) { println(prefix, DigestUtils.digest(messageDigest, file), source); } else if (file.isDirectory()) { final File[] listFiles = file.listFiles(); if (listFiles != null) { run(prefix, messageDigest, listFiles); } } else { // use the default charset for the command-line parameter final byte[] bytes = source.getBytes(Charset.defaultCharset()); println(prefix, DigestUtils.digest(messageDigest, bytes)); } } }
private static byte[] traditionalHashStreaming(HashAlgorithm algorithm, InputStream value) throws IOException { MessageDigest digest = DigestUtils.getDigest(algorithm.getName()); // DigestInputStream digestInputStream = new DigestInputStream(value, digest); return DigestUtils.digest(digest, value); }
private byte[] digestTestData() throws IOException { return DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm),getTestData()); }
@Test public void testDigestByteBuffer() throws IOException { Assume.assumeTrue(DigestUtils.isAvailable(messageDigestAlgorithm)); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), ByteBuffer.wrap(getTestData()))); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm),ByteBuffer.wrap(getTestData()))); }
@Test public void testDigestInputStream() throws IOException { Assume.assumeTrue(DigestUtils.isAvailable(messageDigestAlgorithm)); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), new ByteArrayInputStream(getTestData()))); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm),new ByteArrayInputStream(getTestData()))); }
@Test public void testDigestByteArray() throws IOException { Assume.assumeTrue(DigestUtils.isAvailable(messageDigestAlgorithm)); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), getTestData())); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm),getTestData())); }
@Test public void testDigestFile() throws IOException { Assume.assumeTrue(DigestUtils.isAvailable(messageDigestAlgorithm)); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), getTestFile())); Assert.assertArrayEquals(digestTestData(), DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm),getTestFile())); }
/** * 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)); }