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

package com.google.crypto.tink.subtle;

import java.nio.ByteBuffer;
import java.security.MessageDigest;
import com.google.crypto.tink.internal.BigIntegerEncoding;
import java.math.BigInteger;
import com.google.crypto.tink.internal.Util;
import java.security.GeneralSecurityException;

public final class SubtleUtil
{
    public static String toEcdsaAlgo(final Enums.HashType hash) throws GeneralSecurityException {
        Validators.validateSignatureHash(hash);
        return hash + "withECDSA";
    }
    
    public static String toRsaSsaPkcs1Algo(final Enums.HashType hash) throws GeneralSecurityException {
        Validators.validateSignatureHash(hash);
        return hash + "withRSA";
    }
    
    public static String toDigestAlgo(final Enums.HashType hash) throws GeneralSecurityException {
        switch (hash) {
            case SHA1: {
                return "SHA-1";
            }
            case SHA224: {
                return "SHA-224";
            }
            case SHA256: {
                return "SHA-256";
            }
            case SHA384: {
                return "SHA-384";
            }
            case SHA512: {
                return "SHA-512";
            }
            default: {
                throw new GeneralSecurityException("Unsupported hash " + hash);
            }
        }
    }
    
    public static boolean isAndroid() {
        return "The Android Project".equals(System.getProperty("java.vendor"));
    }
    
    @Deprecated
    public static int androidApiLevel() {
        final Integer androidApiLevel = Util.getAndroidApiLevel();
        if (androidApiLevel != null) {
            return androidApiLevel;
        }
        return -1;
    }
    
    public static BigInteger bytes2Integer(final byte[] bs) {
        return BigIntegerEncoding.fromUnsignedBigEndianBytes(bs);
    }
    
    public static byte[] integer2Bytes(final BigInteger num, final int intendedLength) throws GeneralSecurityException {
        return BigIntegerEncoding.toBigEndianBytesOfFixedLength(num, intendedLength);
    }
    
    public static byte[] mgf1(final byte[] mgfSeed, final int maskLen, final Enums.HashType mgfHash) throws GeneralSecurityException {
        final MessageDigest digest = EngineFactory.MESSAGE_DIGEST.getInstance(toDigestAlgo(mgfHash));
        final int hLen = digest.getDigestLength();
        final byte[] t = new byte[maskLen];
        int tPos = 0;
        for (int counter = 0; counter <= (maskLen - 1) / hLen; ++counter) {
            digest.reset();
            digest.update(mgfSeed);
            digest.update(integer2Bytes(BigInteger.valueOf(counter), 4));
            final byte[] c = digest.digest();
            System.arraycopy(c, 0, t, tPos, Math.min(c.length, t.length - tPos));
            tPos += c.length;
        }
        return t;
    }
    
    public static void putAsUnsigedInt(final ByteBuffer buffer, final long value) throws GeneralSecurityException {
        if (0L > value || value >= 4294967296L) {
            throw new GeneralSecurityException("Index out of range");
        }
        buffer.putInt((int)value);
    }
    
    private SubtleUtil() {
    }
}
