Example usage for org.bouncycastle.bcpg HashAlgorithmTags RIPEMD160

List of usage examples for org.bouncycastle.bcpg HashAlgorithmTags RIPEMD160

Introduction

In this page you can find the example usage for org.bouncycastle.bcpg HashAlgorithmTags RIPEMD160.

Prototype

int RIPEMD160

To view the source code for org.bouncycastle.bcpg HashAlgorithmTags RIPEMD160.

Click Source Link

Usage

From source file:divconq.pgp.PGPUtil.java

License:Open Source License

public static String getDigestName(int hashAlgorithm) throws PGPException {
    switch (hashAlgorithm) {
    case HashAlgorithmTags.SHA1:
        return "SHA1";
    case HashAlgorithmTags.MD2:
        return "MD2";
    case HashAlgorithmTags.MD5:
        return "MD5";
    case HashAlgorithmTags.RIPEMD160:
        return "RIPEMD160";
    case HashAlgorithmTags.SHA256:
        return "SHA256";
    case HashAlgorithmTags.SHA384:
        return "SHA384";
    case HashAlgorithmTags.SHA512:
        return "SHA512";
    case HashAlgorithmTags.SHA224:
        return "SHA224";
    case HashAlgorithmTags.TIGER_192:
        return "TIGER";
    default://  w w w . j av  a2s.c  om
        throw new PGPException("unknown hash algorithm tag in getDigestName: " + hashAlgorithm);
    }
}

From source file:org.apache.camel.converter.crypto.PGPDataFormatTest.java

License:Apache License

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        public void configure() throws Exception {
            // START SNIPPET: pgp-format
            // Public Key FileName
            String keyFileName = getKeyFileName();
            // Private Key FileName
            String keyFileNameSec = getKeyFileNameSec();
            // Keyring Userid Used to Encrypt
            String keyUserid = getKeyUserId();
            // Private key password
            String keyPassword = getKeyPassword();

            from("direct:inline").marshal().pgp(keyFileName, keyUserid).to("mock:encrypted").unmarshal()
                    .pgp(keyFileNameSec, null, keyPassword).to("mock:unencrypted");
            // END SNIPPET: pgp-format

            // START SNIPPET: pgp-format-header
            PGPDataFormat pgpEncrypt = new PGPDataFormat();
            pgpEncrypt.setKeyFileName(keyFileName);
            pgpEncrypt.setKeyUserid(keyUserid);
            pgpEncrypt.setProvider(getProvider());
            pgpEncrypt.setAlgorithm(getAlgorithm());
            pgpEncrypt.setCompressionAlgorithm(getCompressionAlgorithm());

            PGPDataFormat pgpDecrypt = new PGPDataFormat();
            pgpDecrypt.setKeyFileName(keyFileNameSec);
            pgpDecrypt.setPassword(keyPassword);
            pgpDecrypt.setProvider(getProvider());

            from("direct:inline2").marshal(pgpEncrypt).to("mock:encrypted").unmarshal(pgpDecrypt)
                    .to("mock:unencrypted");

            from("direct:inline-armor").marshal().pgp(keyFileName, keyUserid, null, true, true)
                    .to("mock:encrypted").unmarshal().pgp(keyFileNameSec, null, keyPassword, true, true)
                    .to("mock:unencrypted");
            // END SNIPPET: pgp-format-header

            // START SNIPPET: pgp-format-signature
            PGPDataFormat pgpSignAndEncrypt = new PGPDataFormat();
            pgpSignAndEncrypt.setKeyFileName(keyFileName);
            pgpSignAndEncrypt.setKeyUserid(keyUserid);
            pgpSignAndEncrypt.setSignatureKeyFileName(keyFileNameSec);
            PGPPassphraseAccessor passphraseAccessor = getPassphraseAccessor();
            pgpSignAndEncrypt.setSignatureKeyUserid(keyUserid);
            pgpSignAndEncrypt.setPassphraseAccessor(passphraseAccessor);
            pgpSignAndEncrypt.setProvider(getProvider());
            pgpSignAndEncrypt.setAlgorithm(getAlgorithm());
            pgpSignAndEncrypt.setHashAlgorithm(getHashAlgorithm());
            pgpSignAndEncrypt.setCompressionAlgorithm(getCompressionAlgorithm());

            PGPDataFormat pgpVerifyAndDecrypt = new PGPDataFormat();
            pgpVerifyAndDecrypt.setKeyFileName(keyFileNameSec);
            pgpVerifyAndDecrypt.setPassword(keyPassword);
            pgpVerifyAndDecrypt.setSignatureKeyFileName(keyFileName);
            pgpVerifyAndDecrypt.setProvider(getProvider());

            from("direct:inline-sign").marshal(pgpSignAndEncrypt).to("mock:encrypted")
                    .unmarshal(pgpVerifyAndDecrypt).to("mock:unencrypted");
            // END SNIPPET: pgp-format-signature
            /* ---- key ring as byte array -- */
            // START SNIPPET: pgp-format-key-ring-byte-array
            PGPDataFormat pgpEncryptByteArray = new PGPDataFormat();
            pgpEncryptByteArray.setEncryptionKeyRing(getPublicKeyRing());
            pgpEncryptByteArray.setKeyUserids(getKeyUserIds());
            pgpEncryptByteArray.setProvider(getProvider());
            pgpEncryptByteArray.setAlgorithm(SymmetricKeyAlgorithmTags.DES);
            pgpEncryptByteArray.setCompressionAlgorithm(CompressionAlgorithmTags.UNCOMPRESSED);

            PGPDataFormat pgpDecryptByteArray = new PGPDataFormat();
            pgpDecryptByteArray.setEncryptionKeyRing(getSecKeyRing());
            pgpDecryptByteArray.setPassphraseAccessor(passphraseAccessor);
            pgpDecryptByteArray.setProvider(getProvider());

            from("direct:key-ring-byte-array").streamCaching().marshal(pgpEncryptByteArray).to("mock:encrypted")
                    .unmarshal(pgpDecryptByteArray).to("mock:unencrypted");
            // END SNIPPET: pgp-format-key-ring-byte-array

            // START SNIPPET: pgp-format-signature-key-ring-byte-array
            PGPDataFormat pgpSignAndEncryptByteArray = new PGPDataFormat();
            pgpSignAndEncryptByteArray.setKeyUserid(keyUserid);
            pgpSignAndEncryptByteArray.setSignatureKeyRing(getSecKeyRing());
            pgpSignAndEncryptByteArray.setSignatureKeyUserid(keyUserid);
            pgpSignAndEncryptByteArray.setSignaturePassword(keyPassword);
            pgpSignAndEncryptByteArray.setProvider(getProvider());
            pgpSignAndEncryptByteArray.setAlgorithm(SymmetricKeyAlgorithmTags.BLOWFISH);
            pgpSignAndEncryptByteArray.setHashAlgorithm(HashAlgorithmTags.RIPEMD160);
            pgpSignAndEncryptByteArray.setCompressionAlgorithm(CompressionAlgorithmTags.ZLIB);

            PGPDataFormat pgpVerifyAndDecryptByteArray = new PGPDataFormat();
            pgpVerifyAndDecryptByteArray.setPassphraseAccessor(passphraseAccessor);
            pgpVerifyAndDecryptByteArray.setEncryptionKeyRing(getSecKeyRing());
            pgpVerifyAndDecryptByteArray.setProvider(getProvider());

            from("direct:sign-key-ring-byte-array").streamCaching()
                    // encryption key ring can also be set as header
                    .setHeader(PGPDataFormat.ENCRYPTION_KEY_RING).constant(getPublicKeyRing())
                    .marshal(pgpSignAndEncryptByteArray)
                    // it is recommended to remove the header immediately when it is no longer needed
                    .removeHeader(PGPDataFormat.ENCRYPTION_KEY_RING).to("mock:encrypted")
                    // signature key ring can also be set as header
                    .setHeader(PGPDataFormat.SIGNATURE_KEY_RING).constant(getPublicKeyRing())
                    .unmarshal(pgpVerifyAndDecryptByteArray)
                    // it is recommended to remove the header immediately when it is no longer needed
                    .removeHeader(PGPDataFormat.SIGNATURE_KEY_RING).to("mock:unencrypted");
            // END SNIPPET: pgp-format-signature-key-ring-byte-array
        }/*  ww w .  j a  v a 2 s.  com*/
    };
}

From source file:org.m1theo.apt.repo.builder.RepoBuilder.java

License:Open Source License

private static int getDigestCode(String digestName) throws AptRepoException {
    if ("SHA1".equals(digestName)) {
        return HashAlgorithmTags.SHA1;
    } else if ("MD2".equals(digestName)) {
        return HashAlgorithmTags.MD2;
    } else if ("MD5".equals(digestName)) {
        return HashAlgorithmTags.MD5;
    } else if ("RIPEMD160".equals(digestName)) {
        return HashAlgorithmTags.RIPEMD160;
    } else if ("SHA256".equals(digestName)) {
        return HashAlgorithmTags.SHA256;
    } else if ("SHA384".equals(digestName)) {
        return HashAlgorithmTags.SHA384;
    } else if ("SHA512".equals(digestName)) {
        return HashAlgorithmTags.SHA512;
    } else if ("SHA224".equals(digestName)) {
        return HashAlgorithmTags.SHA224;
    } else {/*  w w w .  j a v a 2 s.  c o m*/
        throw new AptRepoException("unknown hash algorithm tag in digestName: " + digestName);
    }
}

From source file:org.sufficientlysecure.keychain.remote.SshAuthenticationService.java

License:Open Source License

private int getHashAlgorithm(Intent data) {
    int hashAlgorithm = data.getIntExtra(SshAuthenticationApi.EXTRA_HASH_ALGORITHM, HASHALGORITHM_NONE);

    switch (hashAlgorithm) {
    case SshAuthenticationApi.SHA1:
        return HashAlgorithmTags.SHA1;
    case SshAuthenticationApi.RIPEMD160:
        return HashAlgorithmTags.RIPEMD160;
    case SshAuthenticationApi.SHA224:
        return HashAlgorithmTags.SHA224;
    case SshAuthenticationApi.SHA256:
        return HashAlgorithmTags.SHA256;
    case SshAuthenticationApi.SHA384:
        return HashAlgorithmTags.SHA384;
    case SshAuthenticationApi.SHA512:
        return HashAlgorithmTags.SHA512;
    default:// w ww  .  j  a  v  a2 s.c om
        return HASHALGORITHM_NONE;
    }
}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenConnection.java

License:Open Source License

private byte[] prepareDsi(byte[] hash, int hashAlgo) throws IOException {
    byte[] dsi;/*w  w  w  . j  av  a  2  s. c o  m*/

    Log.i(Constants.TAG, "Hash: " + hashAlgo);
    switch (hashAlgo) {
    case HashAlgorithmTags.SHA1:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 10!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021" // Tag/Length of Sequence, the 0x21 includes all following 33 bytes
                + "3009" // Tag/Length of Sequence, the 0x09 are the following header bytes
                + "0605" + "2B0E03021A" // OID of SHA1
                + "0500" // TLV coding of ZERO
                + "0414"), hash); // 0x14 are 20 hash bytes
        break;
    case HashAlgorithmTags.RIPEMD160:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 20!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021300906052B2403020105000414"), hash);
        break;
    case HashAlgorithmTags.SHA224:
        if (hash.length != 28) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 28!");
        }
        dsi = Arrays.concatenate(Hex.decode("302D300D06096086480165030402040500041C"), hash);
        break;
    case HashAlgorithmTags.SHA256:
        if (hash.length != 32) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 32!");
        }
        dsi = Arrays.concatenate(Hex.decode("3031300D060960864801650304020105000420"), hash);
        break;
    case HashAlgorithmTags.SHA384:
        if (hash.length != 48) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 48!");
        }
        dsi = Arrays.concatenate(Hex.decode("3041300D060960864801650304020205000430"), hash);
        break;
    case HashAlgorithmTags.SHA512:
        if (hash.length != 64) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 64!");
        }
        dsi = Arrays.concatenate(Hex.decode("3051300D060960864801650304020305000440"), hash);
        break;
    default:
        throw new IOException("Not supported hash algo!");
    }
    return dsi;
}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenHelper.java

License:Open Source License

/**
 * Call COMPUTE DIGITAL SIGNATURE command and returns the MPI value
 *
 * @param hash the hash for signing/*from   ww w. j a va 2s. com*/
 * @return a big integer representing the MPI for the given hash
 */
public byte[] calculateSignature(byte[] hash, int hashAlgo) throws IOException {
    if (!mPw1ValidatedForSignature) {
        verifyPin(0x81); // (Verify PW1 with mode 81 for signing)
    }

    byte[] dsi;

    Log.i(Constants.TAG, "Hash: " + hashAlgo);
    switch (hashAlgo) {
    case HashAlgorithmTags.SHA1:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 10!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021" // Tag/Length of Sequence, the 0x21 includes all following 33 bytes
                + "3009" // Tag/Length of Sequence, the 0x09 are the following header bytes
                + "0605" + "2B0E03021A" // OID of SHA1
                + "0500" // TLV coding of ZERO
                + "0414"), hash); // 0x14 are 20 hash bytes
        break;
    case HashAlgorithmTags.RIPEMD160:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 20!");
        }
        dsi = Arrays.concatenate(Hex.decode("3021300906052B2403020105000414"), hash);
        break;
    case HashAlgorithmTags.SHA224:
        if (hash.length != 28) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 28!");
        }
        dsi = Arrays.concatenate(Hex.decode("302D300D06096086480165030402040500041C"), hash);
        break;
    case HashAlgorithmTags.SHA256:
        if (hash.length != 32) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 32!");
        }
        dsi = Arrays.concatenate(Hex.decode("3031300D060960864801650304020105000420"), hash);
        break;
    case HashAlgorithmTags.SHA384:
        if (hash.length != 48) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 48!");
        }
        dsi = Arrays.concatenate(Hex.decode("3041300D060960864801650304020205000430"), hash);
        break;
    case HashAlgorithmTags.SHA512:
        if (hash.length != 64) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 64!");
        }
        dsi = Arrays.concatenate(Hex.decode("3051300D060960864801650304020305000440"), hash);
        break;
    default:
        throw new IOException("Not supported hash algo!");
    }

    // Command APDU for PERFORM SECURITY OPERATION: COMPUTE DIGITAL SIGNATURE (page 37)
    CommandAPDU command = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A, dsi, MAX_APDU_NE_EXT);
    ResponseAPDU response = communicate(command);

    if (response.getSW() != APDU_SW_SUCCESS) {
        throw new CardException("Failed to sign", response.getSW());
    }

    if (!mOpenPgpCapabilities.isPw1ValidForMultipleSignatures()) {
        mPw1ValidatedForSignature = false;
    }

    byte[] signature = response.getData();

    // Make sure the signature we received is actually the expected number of bytes long!
    if (signature.length != 128 && signature.length != 256 && signature.length != 384
            && signature.length != 512) {
        throw new IOException("Bad signature length! Expected 128/256/384/512 bytes, got " + signature.length);
    }

    return signature;
}

From source file:org.sufficientlysecure.keychain.ui.base.BaseSecurityTokenNfcActivity.java

License:Open Source License

/**
 * Call COMPUTE DIGITAL SIGNATURE command and returns the MPI value
 *
 * @param hash the hash for signing/*  www  .  java  2  s  .  c  om*/
 * @return a big integer representing the MPI for the given hash
 */
public byte[] nfcCalculateSignature(byte[] hash, int hashAlgo) throws IOException {
    if (!mPw1ValidatedForSignature) {
        nfcVerifyPin(0x81); // (Verify PW1 with mode 81 for signing)
    }

    // dsi, including Lc
    String dsi;

    Log.i(Constants.TAG, "Hash: " + hashAlgo);
    switch (hashAlgo) {
    case HashAlgorithmTags.SHA1:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 10!");
        }
        dsi = "23" // Lc
                + "3021" // Tag/Length of Sequence, the 0x21 includes all following 33 bytes
                + "3009" // Tag/Length of Sequence, the 0x09 are the following header bytes
                + "0605" + "2B0E03021A" // OID of SHA1
                + "0500" // TLV coding of ZERO
                + "0414" + getHex(hash); // 0x14 are 20 hash bytes
        break;
    case HashAlgorithmTags.RIPEMD160:
        if (hash.length != 20) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 20!");
        }
        dsi = "233021300906052B2403020105000414" + getHex(hash);
        break;
    case HashAlgorithmTags.SHA224:
        if (hash.length != 28) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 28!");
        }
        dsi = "2F302D300D06096086480165030402040500041C" + getHex(hash);
        break;
    case HashAlgorithmTags.SHA256:
        if (hash.length != 32) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 32!");
        }
        dsi = "333031300D060960864801650304020105000420" + getHex(hash);
        break;
    case HashAlgorithmTags.SHA384:
        if (hash.length != 48) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 48!");
        }
        dsi = "433041300D060960864801650304020205000430" + getHex(hash);
        break;
    case HashAlgorithmTags.SHA512:
        if (hash.length != 64) {
            throw new IOException("Bad hash length (" + hash.length + ", expected 64!");
        }
        dsi = "533051300D060960864801650304020305000440" + getHex(hash);
        break;
    default:
        throw new IOException("Not supported hash algo!");
    }

    // Command APDU for PERFORM SECURITY OPERATION: COMPUTE DIGITAL SIGNATURE (page 37)
    String apdu = "002A9E9A" // CLA, INS, P1, P2
            + dsi // digital signature input
            + "00"; // Le

    String response = nfcCommunicate(apdu);

    // split up response into signature and status
    String status = response.substring(response.length() - 4);
    String signature = response.substring(0, response.length() - 4);

    // while we are getting 0x61 status codes, retrieve more data
    while (status.substring(0, 2).equals("61")) {
        Log.d(Constants.TAG, "requesting more data, status " + status);
        // Send GET RESPONSE command
        response = nfcCommunicate("00C00000" + status.substring(2));
        status = response.substring(response.length() - 4);
        signature += response.substring(0, response.length() - 4);
    }

    Log.d(Constants.TAG, "final response:" + status);

    if (!mPw1ValidForMultipleSignatures) {
        mPw1ValidatedForSignature = false;
    }

    if (!"9000".equals(status)) {
        throw new CardException("Bad NFC response code: " + status, parseCardStatus(response));
    }

    // Make sure the signature we received is actually the expected number of bytes long!
    if (signature.length() != 256 && signature.length() != 512) {
        throw new IOException("Bad signature length! Expected 128 or 256 bytes, got " + signature.length() / 2);
    }

    return Hex.decode(signature);
}