Example usage for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest

List of usage examples for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest.

Prototype

public SHA1Digest() 

Source Link

Document

Standard constructor

Usage

From source file:net.sourceforge.keepassj2me.keydb.KeydbDatabase.java

License:Open Source License

protected int getRandom() {
    byte[] id = new byte[4];
    RandomGenerator rnd = new DigestRandomGenerator(new SHA1Digest());
    rnd.addSeedMaterial(System.currentTimeMillis());
    rnd.nextBytes(id);// w  w  w . j  av a 2 s.  com
    return (id[0] | (id[1] << 8) | (id[2] << 16) | (id[3] << 24));
}

From source file:net.sourceforge.keepassj2me.keydb.KeydbEntry.java

License:Open Source License

/**
 * Generate entry uuid/*from   w  w  w. j  a  v a  2 s .co  m*/
 * @return uuid
 */
public byte[] createUUID() {//FIXME: make sure this is unique
    byte[] uuid = new byte[16];
    RandomGenerator rnd = new DigestRandomGenerator(new SHA1Digest());
    rnd.addSeedMaterial(System.currentTimeMillis());
    rnd.nextBytes(uuid);
    return uuid;
}

From source file:net.sourceforge.keepassj2me.keydb.KeydbHeader.java

License:Open Source License

/**
 * Reinitialize header/*ww  w.j  a  va  2 s  .co m*/
 * @param rounds
 */
public void reinitialize(int rounds) {
    numKeyEncRounds = rounds;
    //SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
    //rnd.setSeed(System.currentTimeMillis());
    RandomGenerator rnd = new DigestRandomGenerator(new SHA1Digest());
    rnd.addSeedMaterial(System.currentTimeMillis());
    rnd.nextBytes(masterSeed);
    rnd.nextBytes(encryptionIV);
    rnd.nextBytes(masterSeed2);
}

From source file:net.wstech2.me.httpsclient.CertificateValidatorUtils.java

License:Apache License

/**
 * /*from  w w  w . jav a  2  s  .  c o  m*/
 * Inspected and display various informations from the Certificate passed as
 * parameter. Keys are presented in HEX values and ASN1 structures dumped
 * using ASN1Dump.dumpAsString.
 * 
 * This method is intended for debug purposes only.
 * 
 * 
 * @param cert
 *            The X509CertificateStructure to be inspected.
 * 
 */
public static void dumpCertificateInfo(org.bouncycastle.asn1.x509.Certificate cert) {
    boolean valid = false;
    TBSCertificate tbs = cert.getTBSCertificate();
    RSAEngine engine = new RSAEngine();
    SHA1Digest digest = new SHA1Digest();

    GenericSigner signer = new GenericSigner((engine), digest);
    RSAPublicKey signingKey;
    try {
        signingKey = RSAPublicKey.getInstance(cert.getSubjectPublicKeyInfo().parsePublicKey());

        HttpsConnectionUtils.logDebug("Public Key:[[" + cert.getSubjectPublicKeyInfo().parsePublicKey() + "]]");

        RSAKeyParameters keySpec = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        signer.init(false, keySpec);
        HttpsConnectionUtils.logDebug("TBS DER object:[[" + tbs.getEncoded("DER") + "]]");

        signer.update(tbs.getEncoded(), 0, tbs.getEncoded().length);

        valid = signer.verifySignature(cert.getSignature().getBytes());

        HttpsConnectionUtils.logDebug("signer.verifySignature:[[" + valid + "]]");

        SHA1Digest d2 = new SHA1Digest();
        d2.update(tbs.getEncoded("DER"), 0, tbs.getEncoded("DER").length);
        byte[] hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("tbs.getDEREncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");
        DEROctetString asn1Hash = new DEROctetString(hash);
        HttpsConnectionUtils.logDebug(
                "ASN1 DEROctetString hash:[[" + new String(Hex.encode(asn1Hash.getEncoded("DER"))) + "]]");

        d2 = new SHA1Digest();
        d2.update(cert.getEncoded(), 0, cert.getEncoded().length);
        hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("cert.getEncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");

        byte[] signature = cert.getSignature().getBytes();
        HttpsConnectionUtils
                .logDebug("cert.getSignature().getBytes():[[" + new String(Hex.encode(signature)) + "]]");

        PKCS1Encoding engine2 = new PKCS1Encoding(new RSAEngine());
        engine2.init(false, keySpec);
        byte[] decryptedHash = engine2.processBlock(signature, 0, signature.length);
        HttpsConnectionUtils.logDebug("decryptedHash:[[" + new String(Hex.encode(decryptedHash)) + "]]");

        ASN1Object o = ASN1Primitive.fromByteArray(decryptedHash);
        HttpsConnectionUtils.logDebug(
                "decryptedHash.getDEREncoded():[[" + new String(Hex.encode(o.getEncoded("DER"))) + "]]");

        HttpsConnectionUtils.logDebug(
                "ASN1Dump.dumpAsString(decryptedHash,true):[[" + ASN1Dump.dumpAsString(o, true) + "]]");

        HttpsConnectionUtils.logDebug("engine.getInputBlockSize():[[" + engine2.getInputBlockSize() + "]]");

        HttpsConnectionUtils.logDebug("engine.getOutputBlockSize():[[" + engine2.getOutputBlockSize() + "]]");

        ASN1Sequence asn1SignSeq = (ASN1Sequence) ASN1Sequence.fromByteArray(decryptedHash);
        HttpsConnectionUtils
                .logDebug("Signature ASN1 Sequence:[[" + ASN1Dump.dumpAsString(asn1SignSeq, true) + "]]");

        AlgorithmIdentifier algorithm = AlgorithmIdentifier.getInstance(asn1SignSeq.getObjectAt(0));
        HttpsConnectionUtils.logDebug("AlgorithmIdentifier:[[" + ASN1Dump.dumpAsString(algorithm, true) + "]]");

        DEROctetString signedHash = (DEROctetString) DEROctetString.getInstance(asn1SignSeq.getObjectAt(1));
        HttpsConnectionUtils.logDebug("signedHash:[[" + ASN1Dump.dumpAsString(signedHash, true) + "]]");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.wstech2.me.httpsclient.CertificateValidatorUtils.java

License:Apache License

/**
 * /*from  w ww. j  a  v a2s. c o m*/
 * Obtains the digest instance corresponding to the Signature Algorithm OID
 * stored within the X509CertificateStructure @cert parameter.
 * 
 * @param cert
 *            The X509CertificateStructure to be analyzed.
 * 
 * @return A Digest (SHA1Digest, MD5Digest, etc.) instance. Null if no
 *         digest corresponding to the OID could be found.
 */

public static ExtendedDigest getDigestInstance(org.bouncycastle.asn1.x509.Certificate cert) {
    String digestId = cert.getSignatureAlgorithm().getAlgorithm().toString();
    if (digestId.equalsIgnoreCase(SHA1_OID)) {
        return new SHA1Digest();
    }
    return null;
}

From source file:org.albertschmitt.crypto.common.HMAC.java

License:Open Source License

public static String sha1(byte[] msg, byte[] keyBytes) throws UnsupportedEncodingException {
    SHA1Digest digest = new SHA1Digest();
    return hmacDigest(msg, keyBytes, digest);
}

From source file:org.albertschmitt.crypto.common.HMAC.java

License:Open Source License

public static String sha1(String msg, byte[] keyBytes) throws UnsupportedEncodingException {
    SHA1Digest digest = new SHA1Digest();
    return hmacDigest(msg.getBytes("UTF-8"), keyBytes, digest);
}

From source file:org.apache.kerby.pkix.EndEntityGenerator.java

License:Apache License

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);//from  www.  ja  v a  2s  . c om
    return resBuf;
}

From source file:org.apache.nifi.processors.standard.util.crypto.PBKDF2CipherProvider.java

License:Apache License

private Digest resolvePRF(final String prf) {
    if (StringUtils.isEmpty(prf)) {
        throw new IllegalArgumentException("Cannot resolve empty PRF");
    }/* w  ww . ja v  a  2s .co m*/
    String formattedPRF = prf.toLowerCase().replaceAll("[\\W]+", "");
    logger.debug("Resolved PRF {} to {}", prf, formattedPRF);
    switch (formattedPRF) {
    case "md5":
        return new MD5Digest();
    case "sha1":
        return new SHA1Digest();
    case "sha384":
        return new SHA384Digest();
    case "sha256":
        return new SHA256Digest();
    case "sha512":
        return new SHA512Digest();
    default:
        logger.warn("Could not resolve PRF {}. Using default PRF {} instead", prf, DEFAULT_PRF);
        return new SHA512Digest();
    }
}

From source file:org.bunkr.cli.commands.HashCommand.java

License:Open Source License

private GeneralDigest getDigest(String algorithm) throws CLIException {
    if (algorithm.equals("md5"))
        return new MD5Digest();
    if (algorithm.equals("sha1"))
        return new SHA1Digest();
    if (algorithm.equals("sha224"))
        return new SHA224Digest();
    if (algorithm.equals("sha256"))
        return new SHA256Digest();
    throw new CLIException("unsupported algorithm: " + algorithm);
}