package org.bouncycastle.crypto.digests;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public abstract class GeneralDigest implements org.bouncycastle.crypto.ExtendedDigest, org.bouncycastle.util.Memoable {
    protected final org.bouncycastle.crypto.CryptoServicePurpose a;
    private final byte[] b;
    private int c;
    private long d;

    protected GeneralDigest(org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose) {
        this.b = new byte[4];
        this.a = cryptoServicePurpose;
        this.c = 0;
    }

    protected void e(org.bouncycastle.crypto.digests.GeneralDigest generalDigest) {
        byte[] bArr = generalDigest.b;
        java.lang.System.arraycopy(bArr, 0, this.b, 0, bArr.length);
        this.c = generalDigest.c;
        this.d = generalDigest.d;
    }

    public void f() {
        long j = this.d << 3;
        byte b = -128;
        while (true) {
            update(b);
            if (this.c == 0) {
                h(j);
                g();
                return;
            }
            b = 0;
        }
    }

    protected abstract void g();

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return 64;
    }

    protected abstract void h(long j);

    protected abstract void i(byte[] bArr, int i);

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this.d = 0L;
        this.c = 0;
        int i = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i >= bArr.length) {
                return;
            }
            bArr[i] = 0;
            i++;
        }
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        byte[] bArr = this.b;
        int i = this.c;
        int i2 = i + 1;
        this.c = i2;
        bArr[i] = b;
        if (i2 == bArr.length) {
            i(bArr, 0);
            this.c = 0;
        }
        this.d++;
    }

    protected GeneralDigest(org.bouncycastle.crypto.digests.GeneralDigest generalDigest) {
        this.b = new byte[4];
        this.a = generalDigest.a;
        e(generalDigest);
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        int i3 = 0;
        int iMax = java.lang.Math.max(0, i2);
        if (this.c != 0) {
            int i4 = 0;
            while (true) {
                if (i4 >= iMax) {
                    i3 = i4;
                    break;
                }
                byte[] bArr2 = this.b;
                int i5 = this.c;
                int i6 = i5 + 1;
                this.c = i6;
                int i7 = i4 + 1;
                bArr2[i5] = bArr[i4 + i];
                if (i6 == 4) {
                    i(bArr2, 0);
                    this.c = 0;
                    i3 = i7;
                    break;
                }
                i4 = i7;
            }
        }
        int i8 = iMax - 3;
        while (i3 < i8) {
            i(bArr, i + i3);
            i3 += 4;
        }
        while (i3 < iMax) {
            byte[] bArr3 = this.b;
            int i9 = this.c;
            this.c = i9 + 1;
            bArr3[i9] = bArr[i3 + i];
            i3++;
        }
        this.d += (long) iMax;
    }
}
