/** * 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); }
/** {@inheritDoc} */ @Override @Nullable public String apply(@Nonnull final String input) { if (input == null) { return null; } return CodecUtil.b64(HashUtil.sha256(input)); } }
/** * Compares the hash of the given data against a known hash output. * * @param hash Known hash value. 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 data Data to hash. * * @return True if hashed data equals known hash output, false otherwise. */ protected boolean compareInternal(final byte[] hash, final Object... data) { return HashUtil.compareHash(digestSpec.newInstance(), hash, iterations, data); } }
/** {@inheritDoc} */ @Override public String convert(@Nonnull final String source) { return "{sha1}" + CodecUtil.hex(HashUtil.sha1(source.getBytes())); } }
/** * Creates a memcached key from one or more parts. * * @param parts Key parts (i.e. namespace, local name) * * @return Key comprised of 250 characters or less. */ private String memcachedKey(final String ... parts) { final String key; if (parts.length > 0) { final StringBuilder sb = new StringBuilder(); int i = 0; for (String part : parts) { if (i++ > 0) { sb.append(':'); } sb.append(part); } key = sb.toString(); } else { key = parts[0]; } if (key.length() > MAX_KEY_LENGTH) { return CodecUtil.hex(HashUtil.sha512(key)); } return key; }
digest.update(bytes, 0, bytes.length); } else if (o instanceof InputStream) { hashStream(digest, (InputStream) o); } else if (o instanceof Resource) { final InputStream in; throw new StreamException(e); hashStream(digest, in); } else { throw new IllegalArgumentException("Invalid input data type " + o);
@Override public String apply(@Nonnull final String input) { return CodecUtil.hex(HashUtil.sha1(input.getBytes())); }
/** * 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); }
/** {@inheritDoc} */ @Override @Nullable public String apply(@Nonnull final String input) { if (input == null) { return null; } return CodecUtil.b64(HashUtil.sha256(input)); } }
/** * 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); }
byteArrayOutputStream.close(); return CodecUtil.b64(HashUtil.sha256(byteArrayOutputStream.toByteArray()));
/** * 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); }
byteArrayOutputStream.close(); return CodecUtil.b64(HashUtil.sha256(byteArrayOutputStream.toByteArray()));
/** * 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());