java.lang.Object
net.i2p.crypto.SHA256Generator

public final class SHA256Generatorextends Object
Defines a wrapper for SHA-256 operation.

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 Details

    • POOL_SIZE

      private static final int POOL_SIZE
      Bound 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

      private final ConcurrentLinkedQueue<MessageDigest> _digests
      Reuse pool of SHA-256 MessageDigests.

      A ConcurrentLinkedQueue rather than a blocking queue, which took a lock twice per hash.

      Deliberately NOT a ThreadLocal: digests are not acquire/release scoped. com.southernstorm.noise's SymmetricState checks 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

      private final AtomicInteger _pooled
      Number of elements in _digests, maintained explicitly because ConcurrentLinkedQueue.size() is O(n). May drift by a constant factor under races, which does not matter for a reuse bound.
  • Constructor Details

    • SHA256Generator

      public SHA256Generator(I2PAppContext context)
      Accept and ignore the context, for construction compatibility.
      Parameters:
      context - accepted but never read; the generator keeps no reference to it
  • Method Details

    • getInstance

      public static final SHA256Generator 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

      public final Hash calculateHash(byte[] source)
      Calculate the SHA-256 hash of the source and cache the result.
      Parameters:
      source - what to hash
      Returns:
      hash of the source
    • calculateHash

      public final Hash calculateHash(byte[] source, int start, int len)
      Calculate the hash and cache the result.
      Parameters:
      source - what to hash
      start - the starting offset
      len - the length to hash
      Returns:
      a Hash wrapping the 32-byte SHA-256 digest of the requested bytes
    • 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 hash
      start - the starting offset
      len - the length to hash
      out - needs 32 bytes starting at outOffset
      outOffset - where in the output array to store the 32-byte digest
    • acquire

      public MessageDigest 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 via release(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

      public void release(MessageDigest digest)
      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

      public static MessageDigest getDigestInstance()
      Return a new MessageDigest from the system libs.
      Returns:
      the digest instance
    • main

      public static void main(String[] args)
      Hash the first argument and print the base64 result.
      Parameters:
      args - a single element holding the UTF-8 text to hash