package org.bouncycastle.crypto.digests;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class MD2Digest implements org.bouncycastle.crypto.ExtendedDigest, org.bouncycastle.util.Memoable {
    private static final byte[] h = {41, 46, 67, -55, -94, -40, 124, 1, 61, 54, 84, -95, -20, -16, 6, 19, 98, -89, 5, -13, -64, -57, 115, -116, -104, -109, 43, -39, -68, 76, -126, -54, 30, -101, 87, 60, -3, -44, -32, 22, 103, 66, 111, 24, -118, 23, -27, 18, -66, 78, -60, -42, -38, -98, -34, 73, -96, -5, -11, -114, -69, 47, -18, 122, -87, 104, 121, -111, 21, -78, 7, 63, -108, -62, 16, -119, 11, 34, 95, 33, -128, 127, 93, -102, 90, -112, 50, 39, 53, 62, -52, -25, -65, -9, -105, 3, -1, 25, 48, -77, 72, -91, -75, -47, -41, 94, -110, 42, -84, 86, -86, -58, 79, -72, 56, -46, -106, -92, 125, -74, 118, -4, 107, -30, -100, 116, 4, -15, 69, -99, 112, 89, 100, 113, -121, 32, -122, 91, -49, 101, -26, 45, -88, 2, 27, 96, 37, -83, -82, -80, -71, -10, 28, 70, 97, 105, 52, 64, 126, 15, 85, 71, -93, 35, -35, 81, -81, 58, -61, 92, -7, -50, -70, -59, -22, 38, 44, 83, 13, 110, -123, 40, -124, 9, -45, -33, -51, -12, 65, -127, 77, 82, 106, -36, 55, -56, 108, -63, -85, -6, 36, -31, 123, 8, 12, -67, -79, 74, 120, -120, -107, -117, -29, 99, -24, 109, -23, -53, -43, -2, 59, 0, 29, 57, -14, -17, -73, 14, 102, 88, -48, -28, -90, 119, 114, -8, -21, 117, 75, 10, 49, 68, 80, -76, -113, -19, 31, 26, -37, -103, -115, 51, -97, 17, -125, 20};
    private final org.bouncycastle.crypto.CryptoServicePurpose a;
    private byte[] b;
    private int c;
    private byte[] d;
    private int e;
    private byte[] f;
    private int g;

    public MD2Digest() {
        this(org.bouncycastle.crypto.CryptoServicePurpose.ANY);
    }

    private void e(org.bouncycastle.crypto.digests.MD2Digest mD2Digest) {
        byte[] bArr = mD2Digest.b;
        java.lang.System.arraycopy(bArr, 0, this.b, 0, bArr.length);
        this.c = mD2Digest.c;
        byte[] bArr2 = mD2Digest.d;
        java.lang.System.arraycopy(bArr2, 0, this.d, 0, bArr2.length);
        this.e = mD2Digest.e;
        byte[] bArr3 = mD2Digest.f;
        java.lang.System.arraycopy(bArr3, 0, this.f, 0, bArr3.length);
        this.g = mD2Digest.g;
    }

    public org.bouncycastle.util.Memoable a() {
        return new org.bouncycastle.crypto.digests.MD2Digest(this);
    }

    public void d(org.bouncycastle.util.Memoable memoable) {
        e((org.bouncycastle.crypto.digests.MD2Digest) memoable);
    }

    @Override // org.bouncycastle.crypto.Digest
    public int doFinal(byte[] bArr, int i) {
        int length = this.d.length;
        int i2 = this.e;
        byte b = (byte) (length - i2);
        while (true) {
            byte[] bArr2 = this.d;
            if (i2 >= bArr2.length) {
                g(bArr2);
                f(this.d);
                f(this.f);
                java.lang.System.arraycopy(this.b, this.c, bArr, i, 16);
                reset();
                return 16;
            }
            bArr2[i2] = b;
            i2++;
        }
    }

    protected void f(byte[] bArr) {
        for (int i = 0; i < 16; i++) {
            byte[] bArr2 = this.b;
            bArr2[i + 16] = bArr[i];
            bArr2[i + 32] = (byte) (bArr[i] ^ bArr2[i]);
        }
        int i2 = 0;
        for (int i3 = 0; i3 < 18; i3++) {
            for (int i4 = 0; i4 < 48; i4++) {
                byte[] bArr3 = this.b;
                byte b = (byte) (h[i2] ^ bArr3[i4]);
                bArr3[i4] = b;
                i2 = b & 255;
            }
            i2 = (i2 + i3) % 256;
        }
    }

    protected void g(byte[] bArr) {
        byte b = this.f[15];
        for (int i = 0; i < 16; i++) {
            byte[] bArr2 = this.f;
            b = (byte) (h[(b ^ bArr[i]) & 255] ^ bArr2[i]);
            bArr2[i] = b;
        }
    }

    @Override // org.bouncycastle.crypto.Digest
    public java.lang.String getAlgorithmName() {
        return "MD2";
    }

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return 16;
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return 16;
    }

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this.c = 0;
        int i = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i == bArr.length) {
                break;
            }
            bArr[i] = 0;
            i++;
        }
        this.e = 0;
        int i2 = 0;
        while (true) {
            byte[] bArr2 = this.d;
            if (i2 == bArr2.length) {
                break;
            }
            bArr2[i2] = 0;
            i2++;
        }
        this.g = 0;
        int i3 = 0;
        while (true) {
            byte[] bArr3 = this.f;
            if (i3 == bArr3.length) {
                return;
            }
            bArr3[i3] = 0;
            i3++;
        }
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        byte[] bArr = this.d;
        int i = this.e;
        int i2 = i + 1;
        this.e = i2;
        bArr[i] = b;
        if (i2 == 16) {
            g(bArr);
            f(this.d);
            this.e = 0;
        }
    }

    public MD2Digest(org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose) {
        this.b = new byte[48];
        this.d = new byte[16];
        this.f = new byte[16];
        this.a = cryptoServicePurpose;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.digests.Utils.a(this, 64, cryptoServicePurpose));
        reset();
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        while (this.e != 0 && i2 > 0) {
            update(bArr[i]);
            i++;
            i2--;
        }
        while (i2 >= 16) {
            java.lang.System.arraycopy(bArr, i, this.d, 0, 16);
            g(this.d);
            f(this.d);
            i2 -= 16;
            i += 16;
        }
        while (i2 > 0) {
            update(bArr[i]);
            i++;
            i2--;
        }
    }

    public MD2Digest(org.bouncycastle.crypto.digests.MD2Digest mD2Digest) {
        this.b = new byte[48];
        this.d = new byte[16];
        this.f = new byte[16];
        org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose = mD2Digest.a;
        this.a = cryptoServicePurpose;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.digests.Utils.a(this, 64, cryptoServicePurpose));
        e(mD2Digest);
    }
}
