package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSADigestSigner implements org.bouncycastle.crypto.Signer {
    private final org.bouncycastle.crypto.DSA g;
    private final org.bouncycastle.crypto.Digest h;
    private final org.bouncycastle.crypto.signers.DSAEncoding i;
    private boolean j;

    @Override // org.bouncycastle.crypto.Signer
    public boolean a(byte[] bArr) {
        if (this.j) {
            throw new java.lang.IllegalStateException("DSADigestSigner not initialised for verification");
        }
        byte[] bArr2 = new byte[this.h.getDigestSize()];
        this.h.doFinal(bArr2, 0);
        try {
            java.math.BigInteger[] bigIntegerArrA = this.i.a(c(), bArr);
            return this.g.b(bArr2, bigIntegerArrA[0], bigIntegerArrA[1]);
        } catch (java.lang.Exception unused) {
            return false;
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] b() {
        if (!this.j) {
            throw new java.lang.IllegalStateException("DSADigestSigner not initialised for signature generation.");
        }
        byte[] bArr = new byte[this.h.getDigestSize()];
        this.h.doFinal(bArr, 0);
        java.math.BigInteger[] bigIntegerArrA = this.g.a(bArr);
        try {
            return this.i.b(c(), bigIntegerArrA[0], bigIntegerArrA[1]);
        } catch (java.lang.Exception unused) {
            throw new java.lang.IllegalStateException("unable to encode signature");
        }
    }

    protected java.math.BigInteger c() {
        org.bouncycastle.crypto.DSA dsa = this.g;
        if (dsa instanceof org.bouncycastle.crypto.DSAExt) {
            return ((org.bouncycastle.crypto.DSAExt) dsa).getOrder();
        }
        return null;
    }

    public void d() {
        this.h.reset();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.j = z;
        org.bouncycastle.crypto.params.AsymmetricKeyParameter asymmetricKeyParameter = cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom ? (org.bouncycastle.crypto.params.AsymmetricKeyParameter) ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a() : (org.bouncycastle.crypto.params.AsymmetricKeyParameter) cipherParameters;
        if (z && !asymmetricKeyParameter.f()) {
            throw new java.lang.IllegalArgumentException("Signing Requires Private Key.");
        }
        if (!z && asymmetricKeyParameter.f()) {
            throw new java.lang.IllegalArgumentException("Verification Requires Public Key.");
        }
        d();
        this.g.init(z, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) {
        this.h.update(b);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) {
        this.h.update(bArr, i, i2);
    }
}
