package org.bouncycastle.crypto.prng;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DigestRandomGenerator implements org.bouncycastle.crypto.prng.RandomGenerator {
    private static long f = 10;
    private org.bouncycastle.crypto.Digest c;
    private byte[] d;
    private byte[] e;
    private long b = 1;
    private long a = 1;

    public DigestRandomGenerator(org.bouncycastle.crypto.Digest digest) {
        this.c = digest;
        this.e = new byte[digest.getDigestSize()];
        this.d = new byte[digest.getDigestSize()];
    }

    private void b() {
        e(this.e);
        long j = this.b;
        this.b = 1 + j;
        c(j);
        d(this.e);
    }

    private void c(long j) {
        for (int i = 0; i != 8; i++) {
            this.c.update((byte) j);
            j >>>= 8;
        }
    }

    private void d(byte[] bArr) {
        this.c.doFinal(bArr, 0);
    }

    private void e(byte[] bArr) {
        this.c.update(bArr, 0, bArr.length);
    }

    private void f() {
        long j = this.a;
        this.a = 1 + j;
        c(j);
        e(this.d);
        e(this.e);
        d(this.d);
        if (this.a % f == 0) {
            b();
        }
    }

    public void a(byte[] bArr) {
        synchronized (this) {
            try {
                if (!org.bouncycastle.util.Arrays.c0(bArr)) {
                    e(bArr);
                }
                e(this.e);
                d(this.e);
            } catch (java.lang.Throwable th) {
                throw th;
            }
        }
    }

    public void g(byte[] bArr) {
        h(bArr, 0, bArr.length);
    }

    public void h(byte[] bArr, int i, int i2) {
        synchronized (this) {
            try {
                f();
                int i3 = i2 + i;
                int i4 = 0;
                while (i != i3) {
                    if (i4 == this.d.length) {
                        f();
                        i4 = 0;
                    }
                    bArr[i] = this.d[i4];
                    i++;
                    i4++;
                }
            } catch (java.lang.Throwable th) {
                throw th;
            }
        }
    }
}
