/** * Produces the SHA-256 hash of the given data. * * @param data Data to hash. See {@link #hash(Digest, Object...)} for supported inputs. * * @return 32-byte array containing hash output. * * @see #hash(Digest, Object...) */ public static byte[] sha256(final Object... data) { return hash(new SHA256Digest(), data); }
/** * Hashes the given data. * * @param data Data to hash. * * @return Digest output. */ protected byte[] hashInternal(final Object... data) { return HashUtil.hash(digestSpec.newInstance(), iterations, data); }
/** * Produces the SHA-512 hash of the given data. * * @param data Data to hash. See {@link #hash(Digest, Object...)} for supported inputs. * * @return 64-byte array containing hash output. * * @see #hash(Digest, Object...) */ public static byte[] sha512(final Object... data) { return hash(new SHA512Digest(), data); }
/** * Produces the SHA-3 hash of the given data. * * @param bitLength One of the supported SHA-3 output bit lengths: 224, 256, 384, or 512. * @param data Data to hash. See {@link #hash(Digest, Object...)} for supported inputs. * * @return Byte array of size <code>bitLength</code> containing hash output. * * @see #hash(Digest, Object...) */ public static byte[] sha3(final int bitLength, final Object... data) { return hash(new SHA3Digest(bitLength), data); }
/** * Produces the SHA-1 hash of the given data. * * @param data Data to hash. See {@link #hash(Digest, Object...)} for supported inputs. * * @return 20-byte array containing hash output. * * @see #hash(Digest, Object...) */ public static byte[] sha1(final Object... data) { return hash(new SHA1Digest(), data); }
/** {@inheritDoc} */ @Override public String convert(@Nonnull final String source) { return "{md5}" + CodecUtil.hex(HashUtil.hash(new MD5Digest(), source.getBytes())); } }
/** * Determines whether the hash of the given input equals a known value. * * @param digest Hash algorithm. * @param hash Hash to compare with. If the length of the array is greater than the length of the digest output, * anything beyond the digest length is considered salt data that is hashed <strong>after</strong> the * input data. * @param iterations Number of hash rounds. * @param data Data to hash. * * @return True if the hash of the data under the given digest is equal to the hash, false otherwise. * * @throws CryptoException on hash computation errors. * @throws StreamException on stream IO errors. */ public static boolean compareHash(final Digest digest, final byte[] hash, final int iterations, final Object... data) throws CryptoException, StreamException { if (hash.length > digest.getDigestSize()) { final byte[] hashPart = Arrays.copyOfRange(hash, 0, digest.getDigestSize()); final byte[] saltPart = Arrays.copyOfRange(hash, digest.getDigestSize(), hash.length); final Object[] dataWithSalt = Arrays.copyOf(data, data.length + 1); dataWithSalt[data.length] = saltPart; return Arrays.equals(hash(digest, iterations, dataWithSalt), hashPart); } return Arrays.equals(hash(digest, iterations, data), hash); }
final byte[] output = hash(digest, data); try { for (int i = 1; i < iterations; i++) {
System.arraycopy(data, 0, dataWithSalt, 1, data.length); return Arrays.equals(hash(digest, iterations, dataWithSalt), hash.getHash());