public TreeChunker(int branches, MessageDigest hasher) { this.branches = branches; this.hasher = hasher; hashSize = hasher.getDigestLength(); chunkSize = hashSize * branches; }
private DigestRandomGenerator(MessageDigest digest) { this.digest = digest; this.seed = new byte[digest.getDigestLength()]; this.seedCounter = 1; this.state = new byte[digest.getDigestLength()]; this.stateCounter = 1; }
int getDigestLength() { return digest.getDigestLength(); }
MessageDigestHashFunction(String algorithmName, int bytes, String toString) { this.toString = checkNotNull(toString); this.prototype = getMessageDigest(algorithmName); int maxLength = prototype.getDigestLength(); checkArgument( bytes >= 4 && bytes <= maxLength, "bytes (%s) must be >= 4 and < %s", bytes, maxLength); this.bytes = bytes; this.supportsClone = supportsClone(prototype); }
MessageDigestHashFunction(String algorithmName, String toString) { this.prototype = getMessageDigest(algorithmName); this.bytes = prototype.getDigestLength(); this.toString = checkNotNull(toString); this.supportsClone = supportsClone(prototype); }
@Override public HashCode hash() { checkNotDone(); done = true; return (bytes == digest.getDigestLength()) ? HashCode.fromBytesNoCopy(digest.digest()) : HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes)); } }
public ByteStringBuilder appendDigestResult(final MessageDigest messageDigest) throws DigestException { reserve(messageDigest.getDigestLength(), false); final int length = this.length; final byte[] content = this.content; this.length = length + messageDigest.digest(content, length, content.length - length); return this; }
public ByteStringBuilder appendDigestResult(final MessageDigest messageDigest) throws DigestException { reserve(messageDigest.getDigestLength(), false); final int length = this.length; final byte[] content = this.content; this.length = length + messageDigest.digest(content, length, content.length - length); return this; }
MessageDigestHashFunction(String algorithmName, int bytes, String toString) { this.toString = checkNotNull(toString); this.prototype = getMessageDigest(algorithmName); int maxLength = prototype.getDigestLength(); checkArgument( bytes >= 4 && bytes <= maxLength, "bytes (%s) must be >= 4 and < %s", bytes, maxLength); this.bytes = bytes; this.supportsClone = supportsClone(prototype); }
@Override public HashCode hash() { checkNotDone(); done = true; return (bytes == digest.getDigestLength()) ? HashCode.fromBytesNoCopy(digest.digest()) : HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes)); } }
MessageDigestHashFunction(String algorithmName, String toString) { this.prototype = getMessageDigest(algorithmName); this.bytes = prototype.getDigestLength(); this.toString = checkNotNull(toString); this.supportsClone = supportsClone(prototype); }
@Override public HashCode hash() { checkNotDone(); done = true; return (bytes == digest.getDigestLength()) ? HashCode.fromBytesNoCopy(digest.digest()) : HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes)); } }
public SimpleNonceManager(final String hashAlg) { // Verify it is a valid algorithm (at least for now) MessageDigest digest = getDigest(hashAlg); this.hashAlg = hashAlg; this.hashLength = digest.getDigestLength(); // Create a new secret only valid within this NonceManager instance. Random rand = new SecureRandom(); byte[] secretBytes = new byte[32]; rand.nextBytes(secretBytes); secret = FlexBase64.encodeString(digest.digest(secretBytes), false); }
MessageDigestHashFunction(String algorithmName, int bytes, String toString) { this.toString = checkNotNull(toString); this.prototype = getMessageDigest(algorithmName); int maxLength = prototype.getDigestLength(); checkArgument( bytes >= 4 && bytes <= maxLength, "bytes (%s) must be >= 4 and < %s", bytes, maxLength); this.bytes = bytes; this.supportsClone = supportsClone(prototype); }
MessageDigestHashFunction(String algorithmName, String toString) { this.prototype = getMessageDigest(algorithmName); this.bytes = prototype.getDigestLength(); this.toString = checkNotNull(toString); this.supportsClone = supportsClone(prototype); }
@Override public HashCode hash() { checkNotDone(); done = true; return (bytes == digest.getDigestLength()) ? HashCode.fromBytesNoCopy(digest.digest()) : HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes)); } }
MessageDigestHashFunction(String algorithmName, String toString) { this.prototype = getMessageDigest(algorithmName); this.bytes = prototype.getDigestLength(); this.toString = checkNotNull(toString); this.supportsClone = supportsClone(prototype); }
MessageDigestHashFunction(String algorithmName, int bytes, String toString) { this.toString = checkNotNull(toString); this.prototype = getMessageDigest(algorithmName); int maxLength = prototype.getDigestLength(); checkArgument( bytes >= 4 && bytes <= maxLength, "bytes (%s) must be >= 4 and < %s", bytes, maxLength); this.bytes = bytes; this.supportsClone = supportsClone(prototype); }
/** * Generate a new encoded nonce to send to the client. * * @param salt additional data to use when creating the overall signature for the nonce. * @return a new encoded nonce to send to the client. */ String generateNonce(byte[] salt) { try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); ByteBuffer byteBuffer = ByteBuffer.allocate(PREFIX_LENGTH + messageDigest.getDigestLength()); byteBuffer.putInt(nonceCounter.incrementAndGet()); byteBuffer.putLong(System.nanoTime()); byteBuffer.put(digest(byteBuffer.array(), 0, PREFIX_LENGTH, salt, messageDigest)); String nonce = ByteIterator.ofBytes(byteBuffer.array()).base64Encode().drainToString(); if (log.isTraceEnabled()) { String saltString = salt == null ? "null" : ByteIterator.ofBytes(salt).hexEncode().drainToString(); log.tracef("New nonce generated %s, using seed %s", nonce, saltString); } return nonce; } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
private static void assertMessageDigestHashing(byte[] input, String algorithmName) { try { MessageDigest digest = MessageDigest.getInstance(algorithmName); assertEquals( HashCode.fromBytes(digest.digest(input)), ALGORITHMS.get(algorithmName).hashBytes(input)); for (int bytes = 4; bytes <= digest.getDigestLength(); bytes++) { assertEquals( HashCode.fromBytes(Arrays.copyOf(digest.digest(input), bytes)), new MessageDigestHashFunction(algorithmName, bytes, algorithmName).hashBytes(input)); } try { int maxSize = digest.getDigestLength(); new MessageDigestHashFunction(algorithmName, maxSize + 1, algorithmName); fail(); } catch (IllegalArgumentException expected) { } } catch (NoSuchAlgorithmException nsae) { throw new AssertionError(nsae); } } }