Search in sources :

Example 81 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hazelcast by hazelcast.

the class MD5Util method toMD5String.

/**
     * Converts given string to MD5 hash
     *
     * @param str str to be hashed with MD5
     */
@SuppressWarnings("checkstyle:magicnumber")
public static String toMD5String(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        if (md == null || str == null) {
            return "NULL";
        }
        byte[] byteData = md.digest(str.getBytes(Charset.forName("UTF-8")));
        StringBuilder sb = new StringBuilder();
        for (byte aByteData : byteData) {
            sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ignored) {
        EmptyStatement.ignore(ignored);
        return null;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 82 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project buck by facebook.

the class Dex method computeSignature.

/**
     * Returns the signature of all but the first 32 bytes of this dex. The
     * first 32 bytes of dex files are not specified to be included in the
     * signature.
     */
public byte[] computeSignature() throws IOException {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError();
    }
    byte[] buffer = new byte[8192];
    // positioned ByteBuffers aren't thread safe
    ByteBuffer data = this.data.duplicate();
    data.limit(data.capacity());
    data.position(SIGNATURE_OFFSET + SIGNATURE_SIZE);
    while (data.hasRemaining()) {
        int count = Math.min(buffer.length, data.remaining());
        data.get(buffer, 0, count);
        digest.update(buffer, 0, count);
    }
    return digest.digest();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer)

Example 83 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project spring-security-oauth by spring-projects.

the class HMAC_SHA1SignatureMethod method sign.

/**
   * Sign the signature base string. The signature is the digest octet string, first base64-encoded per RFC2045, section 6.8, then URL-encoded per
   * OAuth Parameter Encoding.
   *
   * @param signatureBaseString The signature base string.
   * @return The signature.
   */
public String sign(String signatureBaseString) {
    try {
        Mac mac = Mac.getInstance(MAC_NAME);
        mac.init(key);
        byte[] text = signatureBaseString.getBytes("UTF-8");
        byte[] signatureBytes = mac.doFinal(text);
        signatureBytes = Base64.encodeBase64(signatureBytes);
        String signature = new String(signatureBytes, "UTF-8");
        if (LOG.isDebugEnabled()) {
            LOG.debug("signature base: " + signatureBaseString);
            LOG.debug("signature: " + signature);
        }
        return signature;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 84 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project spring-security by spring-projects.

the class CryptoAssumptions method assumeAes256.

private static void assumeAes256(CipherAlgorithm cipherAlgorithm) {
    boolean aes256Available = false;
    try {
        Cipher.getInstance(cipherAlgorithm.toString());
        aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
    } catch (NoSuchAlgorithmException e) {
        throw new AssumptionViolatedException(cipherAlgorithm + " not available, skipping test", e);
    } catch (NoSuchPaddingException e) {
        throw new AssumptionViolatedException(cipherAlgorithm + " padding not available, skipping test", e);
    }
    Assume.assumeTrue("AES key length of 256 not allowed, skipping test", aes256Available);
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 85 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project spring-security-oauth by spring-projects.

the class DefaultAuthenticationKeyGenerator method generateKey.

protected String generateKey(Map<String, String> values) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
        byte[] bytes = digest.digest(values.toString().getBytes("UTF-8"));
        return String.format("%032x", new BigInteger(1, bytes));
    } catch (NoSuchAlgorithmException nsae) {
        throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).", nsae);
    } catch (UnsupportedEncodingException uee) {
        throw new IllegalStateException("UTF-8 encoding not available.  Fatal (should be in the JDK).", uee);
    }
}
Also used : BigInteger(java.math.BigInteger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1557 MessageDigest (java.security.MessageDigest)590 IOException (java.io.IOException)374 InvalidKeyException (java.security.InvalidKeyException)266 KeyStoreException (java.security.KeyStoreException)200 CertificateException (java.security.cert.CertificateException)163 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)162 UnsupportedEncodingException (java.io.UnsupportedEncodingException)141 KeyManagementException (java.security.KeyManagementException)130 KeyFactory (java.security.KeyFactory)105 NoSuchProviderException (java.security.NoSuchProviderException)102 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)96 SSLContext (javax.net.ssl.SSLContext)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)90 KeyStore (java.security.KeyStore)89 UnrecoverableKeyException (java.security.UnrecoverableKeyException)88 InputStream (java.io.InputStream)82 SecureRandom (java.security.SecureRandom)82 Cipher (javax.crypto.Cipher)79 BadPaddingException (javax.crypto.BadPaddingException)75