package com.czhj.wire.okio; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class HashingSource extends ForwardingSource { private final MessageDigest a; private HashingSource(Source source, String str) { super(source); try { this.a = MessageDigest.getInstance(str); } catch (NoSuchAlgorithmException unused) { throw new AssertionError(); } } public static HashingSource md5(Source source) { return new HashingSource(source, "MD5"); } public static HashingSource sha1(Source source) { return new HashingSource(source, "SHA-1"); } public static HashingSource sha256(Source source) { return new HashingSource(source, "SHA-256"); } public ByteString hash() { return ByteString.of(this.a.digest()); } @Override // com.czhj.wire.okio.ForwardingSource, com.czhj.wire.okio.Source public long read(Buffer buffer, long j) throws IOException { long read = super.read(buffer, j); if (read != -1) { long j2 = buffer.c - read; long j3 = buffer.c; Segment segment = buffer.b; while (j3 > buffer.c - read) { segment = segment.i; j3 -= (long) (segment.e - segment.d); } while (j3 < buffer.c) { int i = (int) ((((long) segment.d) + j2) - j3); this.a.update(segment.c, i, segment.e - i); j3 += (long) (segment.e - segment.d); j2 = j3; } } return read; } }