Search in sources :

Example 6 with SHA1Digest

use of org.bouncycastle.crypto.digests.SHA1Digest in project google-authenticator by google.

the class CheckCodeScreen method getCheckCode.

static String getCheckCode(String secret) throws Base32String.DecodingException {
    final byte[] keyBytes = Base32String.decode(secret);
    Mac mac = new HMac(new SHA1Digest());
    mac.init(new KeyParameter(keyBytes));
    PasscodeGenerator pcg = new PasscodeGenerator(mac);
    return pcg.generateResponseCode(0L);
}
Also used : HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) HMac(org.bouncycastle.crypto.macs.HMac) Mac(org.bouncycastle.crypto.Mac)

Example 7 with SHA1Digest

use of org.bouncycastle.crypto.digests.SHA1Digest in project XobotOS by xamarin.

the class DSAParametersGenerator method generateParameters_FIPS186_2.

private DSAParameters generateParameters_FIPS186_2() {
    byte[] seed = new byte[20];
    byte[] part1 = new byte[20];
    byte[] part2 = new byte[20];
    byte[] u = new byte[20];
    SHA1Digest sha1 = new SHA1Digest();
    int n = (L - 1) / 160;
    byte[] w = new byte[L / 8];
    for (; ; ) {
        random.nextBytes(seed);
        hash(sha1, seed, part1);
        System.arraycopy(seed, 0, part2, 0, seed.length);
        inc(part2);
        hash(sha1, part2, part2);
        for (int i = 0; i != u.length; i++) {
            u[i] = (byte) (part1[i] ^ part2[i]);
        }
        u[0] |= (byte) 0x80;
        u[19] |= (byte) 0x01;
        BigInteger q = new BigInteger(1, u);
        if (!q.isProbablePrime(certainty)) {
            continue;
        }
        byte[] offset = Arrays.clone(seed);
        inc(offset);
        for (int counter = 0; counter < 4096; ++counter) {
            for (int k = 0; k < n; k++) {
                inc(offset);
                hash(sha1, offset, part1);
                System.arraycopy(part1, 0, w, w.length - (k + 1) * part1.length, part1.length);
            }
            inc(offset);
            hash(sha1, offset, part1);
            System.arraycopy(part1, part1.length - ((w.length - (n) * part1.length)), w, 0, w.length - n * part1.length);
            w[0] |= (byte) 0x80;
            BigInteger x = new BigInteger(1, w);
            BigInteger c = x.mod(q.shiftLeft(1));
            BigInteger p = x.subtract(c.subtract(ONE));
            if (p.bitLength() != L) {
                continue;
            }
            if (p.isProbablePrime(certainty)) {
                BigInteger g = calculateGenerator_FIPS186_2(p, q, random);
                return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter));
            }
        }
    }
}
Also used : DSAValidationParameters(org.bouncycastle.crypto.params.DSAValidationParameters) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Example 8 with SHA1Digest

use of org.bouncycastle.crypto.digests.SHA1Digest in project XobotOS by xamarin.

the class CacheManager method setupFiles.

@SuppressWarnings("deprecation")
private static void setupFiles(String url, CacheResult cacheRet) {
    assert !JniUtil.useChromiumHttpStack();
    if (true) {
        // Note: SHA1 is much stronger hash. But the cost of setupFiles() is
        // 3.2% cpu time for a fresh load of nytimes.com. While a simple
        // String.hashCode() is only 0.6%. If adding the collision resolving
        // to String.hashCode(), it makes the cpu time to be 1.6% for a 
        // fresh load, but 5.3% for the worst case where all the files 
        // already exist in the file system, but database is gone. So it
        // needs to resolve collision for every file at least once.
        int hashCode = url.hashCode();
        StringBuffer ret = new StringBuffer(8);
        appendAsHex(hashCode, ret);
        String path = ret.toString();
        File file = new File(mBaseDir, path);
        if (true) {
            boolean checkOldPath = true;
            // cache file. If it is not, resolve the collision.
            while (file.exists()) {
                if (checkOldPath) {
                    CacheResult oldResult = mDataBase.getCache(url);
                    if (oldResult != null && oldResult.contentLength > 0) {
                        if (path.equals(oldResult.localPath)) {
                            path = oldResult.localPath;
                        } else {
                            path = oldResult.localPath;
                            file = new File(mBaseDir, path);
                        }
                        break;
                    }
                    checkOldPath = false;
                }
                ret = new StringBuffer(8);
                appendAsHex(++hashCode, ret);
                path = ret.toString();
                file = new File(mBaseDir, path);
            }
        }
        cacheRet.localPath = path;
        cacheRet.outFile = file;
    } else {
        // get hash in byte[]
        Digest digest = new SHA1Digest();
        int digestLen = digest.getDigestSize();
        byte[] hash = new byte[digestLen];
        int urlLen = url.length();
        byte[] data = new byte[urlLen];
        url.getBytes(0, urlLen, data, 0);
        digest.update(data, 0, urlLen);
        digest.doFinal(hash, 0);
        // convert byte[] to hex String
        StringBuffer result = new StringBuffer(2 * digestLen);
        for (int i = 0; i < digestLen; i = i + 4) {
            int h = (0x00ff & hash[i]) << 24 | (0x00ff & hash[i + 1]) << 16 | (0x00ff & hash[i + 2]) << 8 | (0x00ff & hash[i + 3]);
            appendAsHex(h, result);
        }
        cacheRet.localPath = result.toString();
        cacheRet.outFile = new File(mBaseDir, cacheRet.localPath);
    }
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) File(java.io.File)

Example 9 with SHA1Digest

use of org.bouncycastle.crypto.digests.SHA1Digest in project robovm by robovm.

the class BcKeyStoreSpi method engineStore.

public void engineStore(OutputStream stream, char[] password) throws IOException {
    DataOutputStream dOut = new DataOutputStream(stream);
    byte[] salt = new byte[STORE_SALT_SIZE];
    int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff);
    random.nextBytes(salt);
    dOut.writeInt(version);
    dOut.writeInt(salt.length);
    dOut.write(salt);
    dOut.writeInt(iterationCount);
    HMac hMac = new HMac(new SHA1Digest());
    MacOutputStream mOut = new MacOutputStream(hMac);
    PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest());
    byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
    pbeGen.init(passKey, salt, iterationCount);
    if (version < 2) {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize()));
    } else {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8));
    }
    for (int i = 0; i != passKey.length; i++) {
        passKey[i] = 0;
    }
    saveStore(new TeeOutputStream(dOut, mOut));
    byte[] mac = new byte[hMac.getMacSize()];
    hMac.doFinal(mac, 0);
    dOut.write(mac);
    dOut.close();
}
Also used : TeeOutputStream(org.bouncycastle.util.io.TeeOutputStream) PKCS12ParametersGenerator(org.bouncycastle.crypto.generators.PKCS12ParametersGenerator) DataOutputStream(java.io.DataOutputStream) HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) MacOutputStream(org.bouncycastle.crypto.io.MacOutputStream) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Aggregations

SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)9 HMac (org.bouncycastle.crypto.macs.HMac)4 Digest (org.bouncycastle.crypto.Digest)3 IOException (java.io.IOException)2 Mac (org.bouncycastle.crypto.Mac)2 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)2 PKCS12ParametersGenerator (org.bouncycastle.crypto.generators.PKCS12ParametersGenerator)2 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)2 DecodingException (com.google.authenticator.blackberry.Base32String.DecodingException)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 CipherParameters (org.bouncycastle.crypto.CipherParameters)1 ExtendedDigest (org.bouncycastle.crypto.ExtendedDigest)1 MD5Digest (org.bouncycastle.crypto.digests.MD5Digest)1 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)1