package j;

import java.io.IOException;
import java.security.MessageDigest;
import javax.crypto.Mac;

/* JADX INFO: compiled from: HashingSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class n extends k {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final MessageDigest f4624b;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public n(b0 b0Var, String str) {
        super(b0Var);
        g.h0.c.l.f(b0Var, "source");
        g.h0.c.l.f(str, "algorithm");
        this.f4624b = MessageDigest.getInstance(str);
    }

    public final h a() {
        byte[] bArrDoFinal;
        MessageDigest messageDigest = this.f4624b;
        if (messageDigest != null) {
            bArrDoFinal = messageDigest.digest();
        } else {
            Mac mac = null;
            g.h0.c.l.c(null);
            bArrDoFinal = mac.doFinal();
        }
        g.h0.c.l.e(bArrDoFinal, "result");
        return new h(bArrDoFinal);
    }

    @Override // j.k, j.b0
    public long read(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "sink");
        long j3 = super.read(dVar, j2);
        if (j3 != -1) {
            long j4 = dVar.f4601e;
            long j5 = j4 - j3;
            w wVar = dVar.f4600b;
            g.h0.c.l.c(wVar);
            while (j4 > j5) {
                wVar = wVar.f4654g;
                g.h0.c.l.c(wVar);
                j4 -= (long) (wVar.f4650c - wVar.f4649b);
            }
            while (j4 < dVar.f4601e) {
                int i2 = (int) ((((long) wVar.f4649b) + j5) - j4);
                MessageDigest messageDigest = this.f4624b;
                if (messageDigest != null) {
                    messageDigest.update(wVar.a, i2, wVar.f4650c - i2);
                } else {
                    Mac mac = null;
                    g.h0.c.l.c(null);
                    mac.update(wVar.a, i2, wVar.f4650c - i2);
                }
                j5 = ((long) (wVar.f4650c - wVar.f4649b)) + j4;
                wVar = wVar.f4653f;
                g.h0.c.l.c(wVar);
                j4 = j5;
            }
        }
        return j3;
    }
}
