Search in sources :

Example 1 with SHA1Digest

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

the class BcKeyStoreSpi method engineLoad.

public void engineLoad(InputStream stream, char[] password) throws IOException {
    table.clear();
    if (// just initialising
    stream == null) {
        return;
    }
    DataInputStream dIn = new DataInputStream(stream);
    int version = dIn.readInt();
    if (version != STORE_VERSION) {
        if (version != 0 && version != 1) {
            throw new IOException("Wrong version of key store.");
        }
    }
    int saltLength = dIn.readInt();
    if (saltLength <= 0) {
        throw new IOException("Invalid salt detected");
    }
    byte[] salt = new byte[saltLength];
    dIn.readFully(salt);
    int iterationCount = dIn.readInt();
    //
    // we only do an integrity check if the password is provided.
    //
    HMac hMac = new HMac(new SHA1Digest());
    if (password != null && password.length != 0) {
        byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
        PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest());
        pbeGen.init(passKey, salt, iterationCount);
        CipherParameters macParams;
        if (version != 2) {
            macParams = pbeGen.generateDerivedMacParameters(hMac.getMacSize());
        } else {
            macParams = pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8);
        }
        Arrays.fill(passKey, (byte) 0);
        hMac.init(macParams);
        MacInputStream mIn = new MacInputStream(dIn, hMac);
        loadStore(mIn);
        // Finalise our mac calculation
        byte[] mac = new byte[hMac.getMacSize()];
        hMac.doFinal(mac, 0);
        // TODO Should this actually be reading the remainder of the stream?
        // Read the original mac from the stream
        byte[] oldMac = new byte[hMac.getMacSize()];
        dIn.readFully(oldMac);
        if (!Arrays.constantTimeAreEqual(mac, oldMac)) {
            table.clear();
            throw new IOException("KeyStore integrity check failed.");
        }
    } else {
        loadStore(dIn);
        // TODO Should this actually be reading the remainder of the stream?
        // Parse the original mac from the stream too
        byte[] oldMac = new byte[hMac.getMacSize()];
        dIn.readFully(oldMac);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) MacInputStream(org.bouncycastle.crypto.io.MacInputStream) PKCS12ParametersGenerator(org.bouncycastle.crypto.generators.PKCS12ParametersGenerator) HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 2 with SHA1Digest

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

the class BcDefaultDigestProvider method createTable.

private static Map createTable() {
    Map table = new HashMap();
    table.put(OIWObjectIdentifiers.idSHA1, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA1Digest();
        }
    });
    // BEGIN android-removed
    // table.put(NISTObjectIdentifiers.id_sha224, new BcDigestProvider()
    // {
    //     public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier)
    //     {
    //         return new SHA224Digest();
    //     }
    // });
    // END android-removed
    table.put(NISTObjectIdentifiers.id_sha256, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA256Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha384, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA384Digest();
        }
    });
    table.put(NISTObjectIdentifiers.id_sha512, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new SHA512Digest();
        }
    });
    table.put(PKCSObjectIdentifiers.md5, new BcDigestProvider() {

        public ExtendedDigest get(AlgorithmIdentifier digestAlgorithmIdentifier) {
            return new MD5Digest();
        }
    });
    return Collections.unmodifiableMap(table);
}
Also used : ExtendedDigest(org.bouncycastle.crypto.ExtendedDigest) SHA512Digest(org.bouncycastle.crypto.digests.SHA512Digest) MD5Digest(org.bouncycastle.crypto.digests.MD5Digest) HashMap(java.util.HashMap) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) HashMap(java.util.HashMap) Map(java.util.Map) SHA384Digest(org.bouncycastle.crypto.digests.SHA384Digest) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 3 with SHA1Digest

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

the class MSOutlookKeyIdCalculator method calculateKeyId.

static byte[] calculateKeyId(SubjectPublicKeyInfo info) {
    // TODO: include definition of SHA-1 here
    Digest dig = new SHA1Digest();
    byte[] hash = new byte[dig.getDigestSize()];
    byte[] spkiEnc = new byte[0];
    try {
        spkiEnc = info.getEncoded(ASN1Encoding.DER);
    } catch (IOException e) {
        return new byte[0];
    }
    // try the outlook 2010 calculation
    dig.update(spkiEnc, 0, spkiEnc.length);
    dig.doFinal(hash, 0);
    return hash;
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) IOException(java.io.IOException)

Example 4 with SHA1Digest

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

the class AuthenticatorScreen method computePin.

/**
   * Computes the one-time PIN given the secret key.
   * 
   * @param secret
   *          the secret key
   * @return the PIN
   * @throws GeneralSecurityException
   * @throws DecodingException
   *           If the key string is improperly encoded.
   */
public static String computePin(String secret, Long counter) {
    try {
        final byte[] keyBytes = Base32String.decode(secret);
        Mac mac = new HMac(new SHA1Digest());
        mac.init(new KeyParameter(keyBytes));
        PasscodeGenerator pcg = new PasscodeGenerator(mac);
        if (counter == null) {
            // time-based totp
            return pcg.generateTimeoutCode();
        } else {
            // counter-based hotp
            return pcg.generateResponseCode(counter.longValue());
        }
    } catch (RuntimeException e) {
        return "General security exception";
    } catch (DecodingException e) {
        return "Decoding exception";
    }
}
Also used : HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) DecodingException(com.google.authenticator.blackberry.Base32String.DecodingException) HMac(org.bouncycastle.crypto.macs.HMac) Mac(org.bouncycastle.crypto.Mac)

Example 5 with SHA1Digest

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

the class SubjectKeyIdentifier method getDigest.

private static byte[] getDigest(SubjectPublicKeyInfo spki) {
    Digest digest = new SHA1Digest();
    byte[] resBuf = new byte[digest.getDigestSize()];
    byte[] bytes = spki.getPublicKeyData().getBytes();
    digest.update(bytes, 0, bytes.length);
    digest.doFinal(resBuf, 0);
    return resBuf;
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest)

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