Package 

Class HashingSink

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

    
    public final class HashingSink
    extends ForwardingSink
                        

    A sink that computes a hash of the full stream of bytes it has accepted. To use, create an instance with your preferred hash algorithm. Write all of the data to the sink and then call hash to compute the final hash value.

    In this example we use {@code HashingSink} with a BufferedSink to make writing to the sink easier.

       {@code * * HashingSink hashingSink = HashingSink.sha256(s); * BufferedSink bufferedSink = Okio.buffer(hashingSink); * * ... // Write to bufferedSink and either flush or close it. * * ByteString hash = hashingSink.hash(); * }
    • Method Summary

      Modifier and Type Method Description
      static HashingSink md5(Sink sink) Returns a sink that uses the obsolete MD5 hash algorithm to produce 128-bit hashes.
      static HashingSink sha1(Sink sink) Returns a sink that uses the obsolete SHA-1 hash algorithm to produce 160-bit hashes.
      static HashingSink sha256(Sink sink) Returns a sink that uses the SHA-256 hash algorithm to produce 256-bit hashes.
      static HashingSink sha512(Sink sink) Returns a sink that uses the SHA-512 hash algorithm to produce 512-bit hashes.
      static HashingSink hmacSha1(Sink sink, ByteString key) Returns a sink that uses the obsolete SHA-1 HMAC algorithm to produce 160-bit hashes.
      static HashingSink hmacSha256(Sink sink, ByteString key) Returns a sink that uses the SHA-256 HMAC algorithm to produce 256-bit hashes.
      static HashingSink hmacSha512(Sink sink, ByteString key) Returns a sink that uses the SHA-512 HMAC algorithm to produce 512-bit hashes.
      void write(Buffer source, long byteCount) Removes {@code byteCount} bytes from {@code source} and appends them to this.
      final ByteString hash() Returns the hash of the bytes accepted thus far and resets the internal state of this sink.
      • Methods inherited from class com.mobwith.httpmodule.ForwardingSink

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

        close
      • Methods inherited from class java.io.Flushable

        flush
      • Methods inherited from class java.lang.Object

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

      • md5

         static HashingSink md5(Sink sink)

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

      • sha1

         static HashingSink sha1(Sink sink)

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

      • sha256

         static HashingSink sha256(Sink sink)

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

      • sha512

         static HashingSink sha512(Sink sink)

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

      • write

         void write(Buffer source, long byteCount)

        Removes {@code byteCount} bytes from {@code source} and appends them to this.

      • hash

         final ByteString hash()

        Returns the hash of the bytes accepted thus far and resets the internal state of this sink.

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