Package okio
Class HashingSource
- java.lang.Object
-
- okio.ForwardingSource
-
- okio.HashingSource
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Source
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 callhash()to compute the final hash value.In this example we use
HashingSourcewith aBufferedSourceto make reading from the source easier.HashingSource hashingSource = HashingSource.sha256(rawSource); BufferedSource bufferedSource = Okio.buffer(hashingSource); ... // Read all of bufferedSource. ByteString hash = hashingSource.hash();
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteStringhash()Returns the hash of the bytes supplied thus far and resets the internal state of this source.static HashingSourcehmacSha1(Source source, ByteString key)Returns a sink that uses the obsolete SHA-1 HMAC algorithm to produce 160-bit hashes.static HashingSourcehmacSha256(Source source, ByteString key)Returns a sink that uses the SHA-256 HMAC algorithm to produce 256-bit hashes.static HashingSourcemd5(Source source)Returns a sink that uses the obsolete MD5 hash algorithm to produce 128-bit hashes.longread(Buffer sink, long byteCount)Removes at least 1, and up tobyteCountbytes from this and appends them tosink.static HashingSourcesha1(Source source)Returns a sink that uses the obsolete SHA-1 hash algorithm to produce 160-bit hashes.static HashingSourcesha256(Source source)Returns a sink that uses the SHA-256 hash algorithm to produce 256-bit hashes.-
Methods inherited from class okio.ForwardingSource
close, delegate, timeout, toString
-
-
-
-
Method Detail
-
md5
public static HashingSource md5(Source source)
Returns a sink that uses the obsolete MD5 hash algorithm to produce 128-bit hashes.
-
sha1
public static HashingSource sha1(Source source)
Returns a sink that uses the obsolete SHA-1 hash algorithm to produce 160-bit hashes.
-
sha256
public static HashingSource sha256(Source source)
Returns a sink that uses the SHA-256 hash algorithm to produce 256-bit hashes.
-
hmacSha1
public static HashingSource hmacSha1(Source source, ByteString key)
Returns a sink that uses the obsolete SHA-1 HMAC algorithm to produce 160-bit hashes.
-
hmacSha256
public static HashingSource hmacSha256(Source source, ByteString key)
Returns a sink that uses the SHA-256 HMAC algorithm to produce 256-bit hashes.
-
read
public long read(Buffer sink, long byteCount) throws java.io.IOException
Description copied from interface:SourceRemoves at least 1, and up tobyteCountbytes from this and appends them tosink. Returns the number of bytes read, or -1 if this source is exhausted.- Specified by:
readin interfaceSource- Overrides:
readin classForwardingSource- Throws:
java.io.IOException
-
hash
public 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 its internal state is cleared. This starts a new hash with zero bytes supplied.
-
-