@Test public void hmacSha256() throws Exception { HashingSource hashingSource = HashingSource.hmacSha256(source, HMAC_KEY); source.writeUtf8("abc"); assertEquals(3L, hashingSource.read(sink, Long.MAX_VALUE)); assertEquals(HMAC_SHA256_abc, hashingSource.hash()); }
private void download(int workIndex, String url) { try { Response response=ok.newCall(new Request.Builder().url(url).build()).execute(); HashingSource hashingSource=HashingSource.sha256(response.body().source()); EventBus.getDefault().post(new Result(hashingSource.hash(), workIndex, null)); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Exception from OkHttp", e); EventBus.getDefault().post(new Result(null, workIndex, e)); } }
@Test public void hmacSha512() throws Exception { HashingSource hashingSource = HashingSource.hmacSha512(source, HMAC_KEY); source.writeUtf8("abc"); assertEquals(3L, hashingSource.read(sink, Long.MAX_VALUE)); assertEquals(HMAC_SHA512_abc, hashingSource.hash()); }
@Test public void hmacEmptyKey() throws Exception { try { HashingSource.hmacSha256(source, ByteString.EMPTY); fail(); } catch (IllegalArgumentException expected) { } } }
@Test public void multipleHashes() throws Exception { HashingSource hashingSource = HashingSource.sha256(source); source.writeUtf8("abc"); assertEquals(3L, hashingSource.read(sink, Long.MAX_VALUE)); ByteString hash_abc = hashingSource.hash(); assertEquals(SHA256_abc, hash_abc); source.writeUtf8("def"); assertEquals(3L, hashingSource.read(sink, Long.MAX_VALUE)); assertEquals(SHA256_def, hashingSource.hash()); assertEquals(SHA256_abc, hash_abc); }
@Test public void readIntoSuffixOfBuffer() throws Exception { HashingSource hashingSource = HashingSource.sha256(source); source.write(r32k); sink.writeUtf8(TestUtil.repeat('z', SEGMENT_SIZE * 2 - 1)); assertEquals(r32k.size(), hashingSource.read(sink, Long.MAX_VALUE)); assertEquals(SHA256_r32k, hashingSource.hash()); }
@Test public void multipleReads() throws Exception { HashingSource hashingSource = HashingSource.sha256(source); BufferedSource bufferedSource = Okio.buffer(hashingSource); source.writeUtf8("a"); assertEquals('a', bufferedSource.readUtf8CodePoint()); source.writeUtf8("b"); assertEquals('b', bufferedSource.readUtf8CodePoint()); source.writeUtf8("c"); assertEquals('c', bufferedSource.readUtf8CodePoint()); assertEquals(SHA256_abc, hashingSource.hash()); }