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

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

Introduction

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

Prototype

public int doFinal(byte[] out, int outOff) 

Source Link

Usage

From source file:com.yacme.ext.oxsit.cust_it.comp.security.cert.X509CertDisplayBase_IT.java

License:Open Source License

protected void initThumbPrints() {
    //obtain a byte block of the entire certificate data
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    try {/*from   w  ww . j  a v a2s  . c  om*/
        dOut.writeObject(m_aX509);
        byte[] certBlock = bOut.toByteArray();

        //now compute the certificate SHA1 & MD5 digest
        SHA1Digest digsha1 = new SHA1Digest();
        digsha1.update(certBlock, 0, certBlock.length);
        byte[] hashsha1 = new byte[digsha1.getDigestSize()];
        digsha1.doFinal(hashsha1, 0);
        m_sSHA1Thumbprint = Helpers.printHexBytes(hashsha1);
        MD5Digest digmd5 = new MD5Digest();
        digmd5.update(certBlock, 0, certBlock.length);
        byte[] hashmd5 = new byte[digmd5.getDigestSize()];
        digmd5.doFinal(hashmd5, 0);
        m_sMD5Thumbprint = Helpers.printHexBytes(hashmd5);
    } catch (IOException e) {
        m_aLogger.severe("initThumbPrints", e);
    }
}

From source file:cybervillains.ca.ThumbprintUtil.java

License:Open Source License

/**
 * Generates a SHA1 thumbprint of a certificate for long-term mapping.
 * /*from   w  ww. j  a  v  a  2 s  .  co m*/
 * @param cert
 * @return
 * @throws CertificateEncodingException
 */
public static String getThumbprint(final X509Certificate cert) throws CertificateEncodingException {

    if (cert == null) {
        return null;
    }

    byte[] rawOctets = cert.getEncoded();

    SHA1Digest digest = new SHA1Digest();

    byte[] digestOctets = new byte[digest.getDigestSize()];

    digest.update(rawOctets, 0, rawOctets.length);

    digest.doFinal(digestOctets, 0);

    return new String(Base64.encode(digestOctets));
}

From source file:de.tsenger.animamea.crypto.KeyDerivationFunction.java

License:Open Source License

/**
 * //from   w ww  . ja v  a 2  s  .  co m
 * Das MRZ-Passwort besteht aus dem SHA1-Wert der Dokumentennummer +
 * Geburtsdatum + Gltigkeitsdatum (jeweils mit Prfziffer)
 * 
 * @param documentNr
 *            Dokumentennummer plus Prfziffer
 * @param dateOfBirth
 *            Geburtsdatum aus der MRZ plus Prfziffer
 * @param dateOfExpiry
 *            Gltigkeitsdatum aus der MRZ plus Prfziffer
 * @return K = SHA-1(Serial Number||Date of Birth||Date of Expiry)
 */
public static byte[] getMRZBytes(String documentNr, String dateOfBirth, String dateOfExpiry) {
    String mrzInfo = documentNr + dateOfBirth + dateOfExpiry;
    byte[] passwordBytes = mrzInfo.getBytes();

    byte[] K = new byte[20];

    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(passwordBytes, 0, passwordBytes.length);
    sha1.doFinal(K, 0);

    return K;
}

From source file:de.tsenger.animamea.crypto.KeyDerivationFunction.java

License:Open Source License

/**
 * Erzeugt 3DES Schlssel//from  w  ww.  j a  va  2  s . com
 * 
 * @return 112bit-3DES-Schlssel in 24 Bytes mit korrekten Parity-Bits
 */
public byte[] getDESedeKey() {

    byte[] checksum = new byte[20];

    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(mergedData, 0, mergedData.length);
    sha1.doFinal(checksum, 0);

    byte[] ka = new byte[8];
    byte[] kb = new byte[8];

    System.arraycopy(checksum, 0, ka, 0, ka.length);
    System.arraycopy(checksum, 8, kb, 0, kb.length);

    // Adjust Parity-Bits
    adjustParity(ka, 0);
    adjustParity(kb, 0);

    byte[] key = new byte[24];
    System.arraycopy(ka, 0, key, 0, 8);
    System.arraycopy(kb, 0, key, 8, 8);
    System.arraycopy(ka, 0, key, 16, 8);

    return key;

}

From source file:de.tsenger.animamea.crypto.KeyDerivationFunction.java

License:Open Source License

/**
 * Erzeugt AES-128 Schlssel//w  w  w .  ja v  a2s. com
 * 
 * @return Schlssel als Byte-Array
 */
public byte[] getAES128Key() {

    byte[] checksum = new byte[20];

    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(mergedData, 0, mergedData.length);
    sha1.doFinal(checksum, 0);

    // keydata = H(K||r||c)
    // keydata sind die ersten 16 Byte der Hashfunktion ber "mergedData"
    byte[] keydata = new byte[16];
    System.arraycopy(checksum, 0, keydata, 0, 16);
    return keydata;
}

From source file:dorkbox.util.crypto.CryptoDSA.java

License:Apache License

/**
 * The message will have the SHA1 hash calculated and used for the signature.
 * <p/>/*from w w  w . ja  va  2  s.co  m*/
 * Note: this is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
 * <p/>
 * The returned signature is the {r,s} signature array.
 */
public static BigInteger[] generateSignature(DSAPrivateKeyParameters privateKey, SecureRandom secureRandom,
        byte[] message) {
    ParametersWithRandom param = new ParametersWithRandom(privateKey, secureRandom);

    DSASigner dsa = new DSASigner();

    dsa.init(true, param);

    SHA1Digest sha1Digest = new SHA1Digest();
    byte[] checksum = new byte[sha1Digest.getDigestSize()];

    sha1Digest.update(message, 0, message.length);
    sha1Digest.doFinal(checksum, 0);

    return dsa.generateSignature(checksum);
}

From source file:dorkbox.util.crypto.CryptoDSA.java

License:Apache License

/**
 * The message will have the SHA1 hash calculated and used for the signature.
 * <p/>// w  w  w  .  j a v a2s.c om
 * Note: this is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
 *
 * @param signature
 *                 is the {r,s} signature array.
 *
 * @return true if the signature is valid
 */
public static boolean verifySignature(DSAPublicKeyParameters publicKey, byte[] message,
        BigInteger[] signature) {
    SHA1Digest sha1Digest = new SHA1Digest();
    byte[] checksum = new byte[sha1Digest.getDigestSize()];

    sha1Digest.update(message, 0, message.length);
    sha1Digest.doFinal(checksum, 0);

    DSASigner dsa = new DSASigner();

    dsa.init(false, publicKey);

    return dsa.verifySignature(checksum, signature[0], signature[1]);
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

/**
 * Computes the SHA-1 checksum of given message.
 */// w w  w.j  a  v  a 2 s. co  m
public synchronized String digest(String message) {
    try {
        SHA1Digest stomach = new SHA1Digest();
        stomach.reset();
        byte[] food = message.getBytes("UTF-8");
        stomach.update(food, 0, food.length);
        byte[] poop = new byte[64];
        stomach.doFinal(poop, 0);
        return (new String(Base64.encode(poop))).substring(0, 27);
    } catch (UnsupportedEncodingException ex) {
        Logger.error(this, "UTF-8 encoding is not supported : " + ex.toString());
    }
    return null;
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

/**
 * Computes the SHA-1 checksum of given file.
 *//*from  w w  w.  j ava  2  s .  co  m*/
public synchronized String digest(File file) {
    SHA1Digest stomach = new SHA1Digest();
    byte[] poop = new byte[64];
    FileChannel chan = null;
    try {
        chan = (new FileInputStream(file)).getChannel();
    } catch (IOException e) {
        Logger.error(this, "Exception thrown in digest(File file): " + e.toString());
    }
    byte[] temp = new byte[4 * 1024];
    ByteBuffer _temp = ByteBuffer.wrap(temp);
    try {
        while (true) {
            //if (y >= file.length()) break;
            //if (y > file.length()) y = file.length();
            int pos = _temp.position();
            int read = chan.read(_temp);
            if (read == -1)
                break;
            stomach.update(temp, pos, read);
            if (_temp.remaining() == 0)
                _temp.position(0);
        }
        chan.close();
    } catch (IOException e) {
        Logger.error(this, "Exception thrown in digest(File file): " + e.toString());
    }
    stomach.doFinal(poop, 0);
    return (new String(Base64.encode(poop))).substring(0, 27);
}

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

License:Apache License

/**
 * /* www  .java  2  s.com*/
 * 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();
    }

}