package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class HMac implements org.bouncycastle.crypto.Mac {
    private static java.util.Hashtable h;
    private org.bouncycastle.crypto.Digest a;
    private int b;
    private int c;
    private org.bouncycastle.util.Memoable d;
    private org.bouncycastle.util.Memoable e;
    private byte[] f;
    private byte[] g;

    static {
        java.util.Hashtable hashtable = new java.util.Hashtable();
        h = hashtable;
        hashtable.put("GOST3411", org.bouncycastle.util.Integers.f(32));
        h.put("MD2", org.bouncycastle.util.Integers.f(16));
        h.put("MD4", org.bouncycastle.util.Integers.f(64));
        h.put("MD5", org.bouncycastle.util.Integers.f(64));
        h.put("RIPEMD128", org.bouncycastle.util.Integers.f(64));
        h.put("RIPEMD160", org.bouncycastle.util.Integers.f(64));
        h.put("SHA-1", org.bouncycastle.util.Integers.f(64));
        h.put("SHA-224", org.bouncycastle.util.Integers.f(64));
        h.put("SHA-256", org.bouncycastle.util.Integers.f(64));
        h.put("SHA-384", org.bouncycastle.util.Integers.f(128));
        h.put("SHA-512", org.bouncycastle.util.Integers.f(128));
        h.put("Tiger", org.bouncycastle.util.Integers.f(64));
        h.put("Whirlpool", org.bouncycastle.util.Integers.f(64));
    }

    public HMac(org.bouncycastle.crypto.Digest digest) {
        this(digest, a(digest));
    }

    private static int a(org.bouncycastle.crypto.Digest digest) {
        if (digest instanceof org.bouncycastle.crypto.ExtendedDigest) {
            return ((org.bouncycastle.crypto.ExtendedDigest) digest).getByteLength();
        }
        java.lang.Integer num = (java.lang.Integer) h.get(digest.getAlgorithmName());
        if (num != null) {
            return num.intValue();
        }
        throw new java.lang.IllegalArgumentException("unknown digest passed: " + digest.getAlgorithmName());
    }

    private static void c(byte[] bArr, int i, byte b) {
        for (int i2 = 0; i2 < i; i2++) {
            bArr[i2] = (byte) (bArr[i2] ^ b);
        }
    }

    public org.bouncycastle.crypto.Digest b() {
        return this.a;
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        this.a.doFinal(this.g, this.c);
        org.bouncycastle.util.Memoable memoable = this.e;
        if (memoable != null) {
            this.a.d(memoable);
            org.bouncycastle.crypto.Digest digest = this.a;
            digest.update(this.g, this.c, digest.getDigestSize());
        } else {
            org.bouncycastle.crypto.Digest digest2 = this.a;
            byte[] bArr2 = this.g;
            digest2.update(bArr2, 0, bArr2.length);
        }
        int iDoFinal = this.a.doFinal(bArr, i);
        int i2 = this.c;
        while (true) {
            byte[] bArr3 = this.g;
            if (i2 >= bArr3.length) {
                break;
            }
            bArr3[i2] = 0;
            i2++;
        }
        org.bouncycastle.util.Memoable memoable2 = this.d;
        if (memoable2 != null) {
            this.a.d(memoable2);
            return iDoFinal;
        }
        org.bouncycastle.crypto.Digest digest3 = this.a;
        byte[] bArr4 = this.f;
        digest3.update(bArr4, 0, bArr4.length);
        return iDoFinal;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return this.a.getAlgorithmName() + "/HMAC";
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return this.b;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArr;
        this.a.reset();
        byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b();
        int length = bArrB.length;
        if (length > this.c) {
            this.a.update(bArrB, 0, length);
            this.a.doFinal(this.f, 0);
            length = this.b;
        } else {
            java.lang.System.arraycopy(bArrB, 0, this.f, 0, length);
        }
        while (true) {
            bArr = this.f;
            if (length >= bArr.length) {
                break;
            }
            bArr[length] = 0;
            length++;
        }
        java.lang.System.arraycopy(bArr, 0, this.g, 0, this.c);
        c(this.f, this.c, (byte) 54);
        c(this.g, this.c, (byte) 92);
        org.bouncycastle.util.Memoable memoable = this.a;
        if (memoable instanceof org.bouncycastle.util.Memoable) {
            org.bouncycastle.crypto.Digest digestA = memoable.a();
            this.e = digestA;
            digestA.update(this.g, 0, this.c);
        }
        org.bouncycastle.crypto.Digest digest = this.a;
        byte[] bArr2 = this.f;
        digest.update(bArr2, 0, bArr2.length);
        org.bouncycastle.util.Memoable memoable2 = this.a;
        if (memoable2 instanceof org.bouncycastle.util.Memoable) {
            this.d = memoable2.a();
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        org.bouncycastle.util.Memoable memoable = this.d;
        if (memoable != null) {
            this.a.d(memoable);
            return;
        }
        this.a.reset();
        org.bouncycastle.crypto.Digest digest = this.a;
        byte[] bArr = this.f;
        digest.update(bArr, 0, bArr.length);
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        this.a.update(b);
    }

    private HMac(org.bouncycastle.crypto.Digest digest, int i) {
        this.a = digest;
        int digestSize = digest.getDigestSize();
        this.b = digestSize;
        this.c = i;
        this.f = new byte[i];
        this.g = new byte[i + digestSize];
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        this.a.update(bArr, i, i2);
    }
}
