Search in sources :

Example 91 with NoSuchAlgorithmException

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

the class WebSocketHandler method generateServerKey.

private static String generateServerKey(String clientKey) {
    try {
        String serverKey = clientKey + SERVER_KEY_GUID;
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(Utf8Charset.encodeUTF8(serverKey));
        return Base64.encodeToString(sha1.digest(), Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 92 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project cardslib by gabrielemariotti.

the class Security method generatePublicKey.

/**
     * Generates a PublicKey instance from a string containing the
     * Base64-encoded public key.
     *
     * @param encodedPublicKey Base64-encoded public key
     * @throws IllegalArgumentException if encodedPublicKey is invalid
     */
public static PublicKey generatePublicKey(String encodedPublicKey) {
    try {
        byte[] decodedKey = Base64.decode(encodedPublicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "Invalid key specification.");
        throw new IllegalArgumentException(e);
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
        throw new IllegalArgumentException(e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 93 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project cardslib by gabrielemariotti.

the class Security method verify.

/**
     * Verifies that the signature from the server matches the computed
     * signature on the data.  Returns true if the data is correctly signed.
     *
     * @param publicKey public key associated with the developer account
     * @param signedData signed data from server
     * @param signature server signature
     * @return true if the data and signature match
     */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key specification.");
    } catch (SignatureException e) {
        Log.e(TAG, "Signature exception.");
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
    }
    return false;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 94 with NoSuchAlgorithmException

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

the class StringUtils method getSHA1.

/**
	 * Calculates the SHA1 of the byte array.
	 *
	 * @param bytes
	 * @return sha1 of the byte array
	 */
public static String getSHA1(byte[] bytes) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] digest = md.digest();
        return toHex(digest);
    } catch (NoSuchAlgorithmException t) {
        throw new RuntimeException(t);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 95 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project ExoPlayer by google.

the class Aes128DataSource method open.

@Override
public long open(DataSpec dataSpec) throws IOException {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
    Key cipherKey = new SecretKeySpec(encryptionKey, "AES");
    AlgorithmParameterSpec cipherIV = new IvParameterSpec(encryptionIv);
    try {
        cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherIV);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }
    cipherInputStream = new CipherInputStream(new DataSourceInputStream(upstream, dataSpec), cipher);
    return C.LENGTH_UNSET;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

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