// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.internal;

import java.security.Provider;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;

public final class Random
{
    private static final ThreadLocal<SecureRandom> localRandom;
    
    private static SecureRandom create() {
        final Provider conscryptProvider = ConscryptUtil.providerOrNull();
        if (conscryptProvider != null) {
            try {
                return SecureRandom.getInstance("SHA1PRNG", conscryptProvider);
            }
            catch (final GeneralSecurityException ex) {}
        }
        final Provider conscryptProviderWithReflection = ConscryptUtil.providerWithReflectionOrNull();
        if (conscryptProviderWithReflection != null) {
            try {
                return SecureRandom.getInstance("SHA1PRNG", conscryptProviderWithReflection);
            }
            catch (final GeneralSecurityException ex2) {}
        }
        return new SecureRandom();
    }
    
    private static SecureRandom newDefaultSecureRandom() {
        final SecureRandom retval = create();
        retval.nextLong();
        return retval;
    }
    
    public static byte[] randBytes(final int size) {
        final byte[] rand = new byte[size];
        Random.localRandom.get().nextBytes(rand);
        return rand;
    }
    
    public static final int randInt(final int max) {
        return Random.localRandom.get().nextInt(max);
    }
    
    public static final int randInt() {
        return Random.localRandom.get().nextInt();
    }
    
    public static final void validateUsesConscrypt() throws GeneralSecurityException {
        if (!ConscryptUtil.isConscryptProvider(Random.localRandom.get().getProvider())) {
            throw new GeneralSecurityException("Requires GmsCore_OpenSSL, AndroidOpenSSL or Conscrypt to generate randomness, but got " + Random.localRandom.get().getProvider().getName());
        }
    }
    
    private Random() {
    }
    
    static {
        localRandom = new ThreadLocal<SecureRandom>() {
            @Override
            protected SecureRandom initialValue() {
                return newDefaultSecureRandom();
            }
        };
    }
}
