package org.bouncycastle.crypto.digests;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ParallelHash implements org.bouncycastle.crypto.Xof, org.bouncycastle.crypto.Digest {
    private static final byte[] l = org.bouncycastle.util.Strings.f("ParallelHash");
    private final org.bouncycastle.crypto.digests.CSHAKEDigest a;
    private final org.bouncycastle.crypto.digests.CSHAKEDigest b;
    private final int c;
    private final int d;
    private final int e;
    private final byte[] f;
    private final byte[] g;
    private boolean h;
    private int i;
    private int j;
    private final org.bouncycastle.crypto.CryptoServicePurpose k;

    public ParallelHash(int i, byte[] bArr, int i2, int i3) {
        this(i, bArr, i2, i3, org.bouncycastle.crypto.CryptoServicePurpose.ANY);
    }

    private void a() {
        d(this.f, 0, this.j);
        this.j = 0;
    }

    private void d(byte[] bArr, int i, int i2) {
        this.b.update(bArr, i, i2);
        org.bouncycastle.crypto.digests.CSHAKEDigest cSHAKEDigest = this.b;
        byte[] bArr2 = this.g;
        cSHAKEDigest.b(bArr2, 0, bArr2.length);
        org.bouncycastle.crypto.digests.CSHAKEDigest cSHAKEDigest2 = this.a;
        byte[] bArr3 = this.g;
        cSHAKEDigest2.update(bArr3, 0, bArr3.length);
        this.i++;
    }

    private void e(int i) {
        if (this.j != 0) {
            a();
        }
        byte[] bArrD = org.bouncycastle.crypto.digests.XofUtils.d(this.i);
        byte[] bArrD2 = org.bouncycastle.crypto.digests.XofUtils.d(i * 8);
        this.a.update(bArrD, 0, bArrD.length);
        this.a.update(bArrD2, 0, bArrD2.length);
        this.h = false;
    }

    @Override // org.bouncycastle.crypto.Xof
    public int b(byte[] bArr, int i, int i2) {
        if (this.h) {
            e(this.d);
        }
        int iB = this.a.b(bArr, i, i2);
        reset();
        return iB;
    }

    @Override // org.bouncycastle.crypto.Xof
    public int c(byte[] bArr, int i, int i2) {
        if (this.h) {
            e(0);
        }
        return this.a.c(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.Digest
    public int doFinal(byte[] bArr, int i) {
        if (this.h) {
            e(this.d);
        }
        int iB = this.a.b(bArr, i, getDigestSize());
        reset();
        return iB;
    }

    @Override // org.bouncycastle.crypto.Digest
    public java.lang.String getAlgorithmName() {
        return "ParallelHash" + this.a.getAlgorithmName().substring(6);
    }

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return this.a.getByteLength();
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return this.d;
    }

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this.a.reset();
        org.bouncycastle.util.Arrays.i(this.f);
        byte[] bArrC = org.bouncycastle.crypto.digests.XofUtils.c(this.e);
        this.a.update(bArrC, 0, bArrC.length);
        this.i = 0;
        this.j = 0;
        this.h = true;
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        byte[] bArr = this.f;
        int i = this.j;
        int i2 = i + 1;
        this.j = i2;
        bArr[i] = b;
        if (i2 == bArr.length) {
            a();
        }
    }

    public ParallelHash(int i, byte[] bArr, int i2, int i3, org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose) {
        this.a = new org.bouncycastle.crypto.digests.CSHAKEDigest(i, l, bArr);
        this.b = new org.bouncycastle.crypto.digests.CSHAKEDigest(i, new byte[0], new byte[0]);
        this.c = i;
        this.e = i2;
        this.d = (i3 + 7) / 8;
        this.f = new byte[i2];
        this.g = new byte[(i * 2) / 8];
        this.k = cryptoServicePurpose;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.digests.Utils.a(this, i, cryptoServicePurpose));
        reset();
    }

    @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.j != 0) {
            while (i3 < iMax) {
                int i4 = this.j;
                byte[] bArr2 = this.f;
                if (i4 == bArr2.length) {
                    break;
                }
                this.j = i4 + 1;
                bArr2[i4] = bArr[i3 + i];
                i3++;
            }
            if (this.j == this.f.length) {
                a();
            }
        }
        if (i3 < iMax) {
            while (true) {
                int i5 = iMax - i3;
                int i6 = this.e;
                if (i5 < i6) {
                    break;
                }
                d(bArr, i + i3, i6);
                i3 += this.e;
            }
        }
        while (i3 < iMax) {
            update(bArr[i3 + i]);
            i3++;
        }
    }

    public ParallelHash(org.bouncycastle.crypto.digests.ParallelHash parallelHash) {
        this.a = new org.bouncycastle.crypto.digests.CSHAKEDigest(parallelHash.a);
        this.b = new org.bouncycastle.crypto.digests.CSHAKEDigest(parallelHash.b);
        int i = parallelHash.c;
        this.c = i;
        this.e = parallelHash.e;
        this.d = parallelHash.d;
        this.f = org.bouncycastle.util.Arrays.j(parallelHash.f);
        this.g = org.bouncycastle.util.Arrays.j(parallelHash.g);
        org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose = parallelHash.k;
        this.k = cryptoServicePurpose;
        this.h = parallelHash.h;
        this.i = parallelHash.i;
        this.j = parallelHash.j;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.digests.Utils.a(this, i, cryptoServicePurpose));
    }
}
