As of release 0.8.7, uses java.security.MessageDigest by default. As of release 0.9.25, uses only MessageDigest. GNU-Crypto gnu.crypto.hash.Sha256Standalone is removed as of 0.9.28. As of release 0.9.71, the digest pool is lock-free (ConcurrentLinkedQueue) instead of a LinkedBlockingQueue.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentLinkedQueue<MessageDigest> Reuse pool of SHA-256MessageDigests.private final AtomicIntegerNumber of elements in_digests, maintained explicitly becauseConcurrentLinkedQueue.size()is O(n).private static final intBound on how many digests are kept for reuse.Constructor Summary
ConstructorsConstructorDescriptionSHA256Generator(I2PAppContext context) Accept and ignore the context, for construction compatibility.Method Summary
Modifier and TypeMethodDescriptionacquire()Check out a SHA-256 MessageDigest, reset and ready to use.final HashcalculateHash(byte[] source) Calculate the SHA-256 hash of the source and cache the result.final HashcalculateHash(byte[] source, int start, int len) Calculate the hash and cache the result.final voidcalculateHash(byte[] source, int start, int len, byte[] out, int outOffset) Use this if you only need the data, not a Hash object.static MessageDigestReturn a new MessageDigest from the system libs.static final SHA256GeneratorReturn the global SHA-256 generator.static voidHash the first argument and print the base64 result.voidrelease(MessageDigest digest) Release a digest back to the pool, reset so that no hashed data survives in a pooled digest between uses.
Field Details
POOL_SIZE
private static final int POOL_SIZEBound on how many digests are kept for reuse. It caps retained digests, not concurrency: an exhausted pool simply hands out a fresh digest.- See Also:
_digests
Reuse pool of SHA-256MessageDigests.A
ConcurrentLinkedQueuerather than a blocking queue, which took a lock twice per hash.Deliberately NOT a
ThreadLocal: digests are not acquire/release scoped.com.southernstorm.noise'sSymmetricStatechecks one out in its constructor and holds it for the life of the Noise state, so more than one can be live on a thread at once and a thread-confined digest would silently alias them and corrupt the key derivation._pooled
Number of elements in_digests, maintained explicitly becauseConcurrentLinkedQueue.size()is O(n). May drift by a constant factor under races, which does not matter for a reuse bound.
Constructor Details
SHA256Generator
Accept and ignore the context, for construction compatibility.- Parameters:
context- accepted but never read; the generator keeps no reference to it
Method Details
getInstance
Return the global SHA-256 generator.- Returns:
- the generator bound to the global I2PAppContext, created on first call if it does not exist yet
calculateHash
Calculate the SHA-256 hash of the source and cache the result.- Parameters:
source- what to hash- Returns:
- hash of the source
calculateHash
calculateHash
public final void calculateHash(byte[] source, int start, int len, byte[] out, int outOffset) Use this if you only need the data, not a Hash object. Does not cache.- Parameters:
source- what to hashstart- the starting offsetlen- the length to hashout- needs 32 bytes starting at outOffsetoutOffset- where in the output array to store the 32-byte digest
acquire
Check out a SHA-256 MessageDigest, reset and ready to use.For uses where the one-shot
calculateHash(byte[])would require copying the data. Hand it back viarelease(MessageDigest)when done. A MessageDigest is not thread-safe and must not be shared, so a caller holding two at once must have checked out two.- Returns:
- a reset MessageDigest instance
release
Release a digest back to the pool, reset so that no hashed data survives in a pooled digest between uses.The digest must be SHA-256, i.e. one
acquire()returned. That invariant is not re-checked:MessageDigest.getAlgorithm()is a provider-dependent string comparison on a hot path.- Parameters:
digest- must be SHA-256
getDigestInstance
Return a new MessageDigest from the system libs.- Returns:
- the digest instance
main
Hash the first argument and print the base64 result.- Parameters:
args- a single element holding the UTF-8 text to hash