Package 

Class HashingSource

  • All Implemented Interfaces:
    com.mobwith.httpmodule.Source , java.io.Closeable , java.lang.AutoCloseable

    
    public final class HashingSource
    extends ForwardingSource
                        

    A source that computes a hash of the full stream of bytes it has supplied. To use, create an instance with your preferred hash algorithm. Exhaust the source by reading all of its bytes and then call hash to compute the final hash value.

    In this example we use {@code HashingSource} with a BufferedSource to make reading from the source easier.

       {@code * * HashingSource hashingSource = HashingSource.sha256(rawSource); * BufferedSource bufferedSource = Okio.buffer(hashingSource); * * ... // Read all of bufferedSource. * * ByteString hash = hashingSource.hash(); * }
    • Method Summary

      Modifier and Type Method Description
      static HashingSource md5(Source source) Returns a sink that uses the obsolete MD5 hash algorithm to produce 128-bit hashes.
      static HashingSource sha1(Source source) Returns a sink that uses the obsolete SHA-1 hash algorithm to produce 160-bit hashes.
      static HashingSource sha256(Source source) Returns a sink that uses the SHA-256 hash algorithm to produce 256-bit hashes.
      static HashingSource hmacSha1(Source source, ByteString key) Returns a sink that uses the obsolete SHA-1 HMAC algorithm to produce 160-bit hashes.
      static HashingSource hmacSha256(Source source, ByteString key) Returns a sink that uses the SHA-256 HMAC algorithm to produce 256-bit hashes.
      long read(Buffer sink, long byteCount) Removes at least 1, and up to {@code byteCount} bytes from this and appendsthem to {@code sink}.
      final ByteString hash() Returns the hash of the bytes supplied thus far and resets the internal state of this source.
      • Methods inherited from class com.mobwith.httpmodule.ForwardingSource

        close, delegate, timeout, toString
      • Methods inherited from class java.io.Closeable

        close
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • md5

         static HashingSource md5(Source source)

        Returns a sink that uses the obsolete MD5 hash algorithm to produce 128-bit hashes.

      • sha1

         static HashingSource sha1(Source source)

        Returns a sink that uses the obsolete SHA-1 hash algorithm to produce 160-bit hashes.

      • sha256

         static HashingSource sha256(Source source)

        Returns a sink that uses the SHA-256 hash algorithm to produce 256-bit hashes.

      • read

         long read(Buffer sink, long byteCount)

        Removes at least 1, and up to {@code byteCount} bytes from this and appendsthem to {@code sink}. Returns the number of bytes read, or -1 if thissource is exhausted.

      • hash

         final ByteString hash()

        Returns the hash of the bytes supplied thus far and resets the internal state of this source.

        Warning: This method is not idempotent. Each time this method is called itsinternal state is cleared. This starts a new hash with zero bytes supplied.