Search in sources :

Example 51 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Fairphone by Kwamecorp.

the class GappsInstallerHelper method calculateMD5.

public static String calculateMD5(File updateFile) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Exception while getting Digest", e);
        return null;
    }
    InputStream is;
    try {
        is = new FileInputStream(updateFile);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Exception while getting FileInputStream", e);
        return null;
    }
    byte[] buffer = new byte[8192];
    int read;
    try {
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        String output = bigInt.toString(16);
        // Fill to 32 chars
        output = String.format("%32s", output).replace(' ', '0');
        return output;
    } catch (IOException e) {
        throw new RuntimeException("Unable to process file for MD5", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "Exception on closing MD5 input stream", e);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 52 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Fairphone by Kwamecorp.

the class FairphoneUpdater method calculateMD5.

public static String calculateMD5(File updateFile) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Exception while getting Digest", e);
        return null;
    }
    InputStream is;
    try {
        is = new FileInputStream(updateFile);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Exception while getting FileInputStream", e);
        return null;
    }
    byte[] buffer = new byte[8192];
    int read;
    try {
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        String output = bigInt.toString(16);
        // Fill to 32 chars
        output = String.format("%32s", output).replace(' ', '0');
        return output;
    } catch (IOException e) {
        throw new RuntimeException("Unable to process file for MD5", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "Exception on closing MD5 input stream", e);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 53 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project nanohttpd by NanoHttpd.

the class NanoWSD method handleWebSocket.

public Response handleWebSocket(final IHTTPSession session) {
    Map<String, String> headers = session.getHeaders();
    if (isWebsocketRequested(session)) {
        if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) {
            return Response.newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION));
        }
        if (!headers.containsKey(NanoWSD.HEADER_WEBSOCKET_KEY)) {
            return Response.newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key");
        }
        WebSocket webSocket = openWebSocket(session);
        Response handshakeResponse = webSocket.getHandshakeResponse();
        try {
            handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(NanoWSD.HEADER_WEBSOCKET_KEY)));
        } catch (NoSuchAlgorithmException e) {
            return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "The SHA-1 Algorithm required for websockets is not available on the server.");
        }
        if (headers.containsKey(NanoWSD.HEADER_WEBSOCKET_PROTOCOL)) {
            handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL, headers.get(NanoWSD.HEADER_WEBSOCKET_PROTOCOL).split(",")[0]);
        }
        return handshakeResponse;
    } else {
        return null;
    }
}
Also used : Response(org.nanohttpd.protocols.http.response.Response) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 54 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project ribbon by Netflix.

the class AbstractSslContextFactory method createKeyManagers.

/**
     * Creates the key managers to be used by the factory from the associated key store and password.
     *
     * @return the newly created array of key managers
     * @throws ClientSslSocketFactoryException if an exception is detected in loading the key store
     */
private KeyManager[] createKeyManagers() throws ClientSslSocketFactoryException {
    final KeyManagerFactory factory;
    try {
        factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        factory.init(this.keyStore, this.keyStorePassword.toCharArray());
    } catch (NoSuchAlgorithmException e) {
        throw new ClientSslSocketFactoryException(String.format("Failed to create the key store because the algorithm %s is not supported. ", KeyManagerFactory.getDefaultAlgorithm()), e);
    } catch (UnrecoverableKeyException e) {
        throw new ClientSslSocketFactoryException("Unrecoverable Key Exception initializing key manager factory; this is probably fatal", e);
    } catch (KeyStoreException e) {
        throw new ClientSslSocketFactoryException("KeyStore exception initializing key manager factory; this is probably fatal", e);
    }
    KeyManager[] managers = factory.getKeyManagers();
    LOGGER.debug("Key managers are initialized. Total {} managers. ", managers.length);
    return managers;
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 55 with NoSuchAlgorithmException

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

the class Pkcs12 method save.

/**
     * Saves the keystore to the resource passed in to the constructor.
     * 
     * @throws IOException if there was an error writing the keystore to the resource
     * @throws KeyStoreException if the keystore has not been initialized, or if the integrity check algorithm is not available, or if any certificates in the keystore could not be loaded
     */
public void save() throws IOException, KeyStoreException {
    try {
        OutputStream out = keystoreResource.getOutputStream();
        //, 
        keystore.store(out, keystorePassword.toCharArray());
        out.close();
    } catch (NoSuchAlgorithmException e) {
        // if the algorithm used to check the integrity of the keystore cannot be found
        throw new KeyStoreException(e);
    } catch (CertificateException e) {
        // if any certificates in the keystore could not be loaded
        throw new KeyStoreException(e);
    }
}
Also used : OutputStream(java.io.OutputStream) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1403 MessageDigest (java.security.MessageDigest)548 IOException (java.io.IOException)328 InvalidKeyException (java.security.InvalidKeyException)242 KeyStoreException (java.security.KeyStoreException)168 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)145 CertificateException (java.security.cert.CertificateException)138 UnsupportedEncodingException (java.io.UnsupportedEncodingException)131 KeyManagementException (java.security.KeyManagementException)105 KeyFactory (java.security.KeyFactory)96 NoSuchProviderException (java.security.NoSuchProviderException)93 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)79 UnrecoverableKeyException (java.security.UnrecoverableKeyException)78 KeyStore (java.security.KeyStore)73 SecureRandom (java.security.SecureRandom)72 SSLContext (javax.net.ssl.SSLContext)72 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)69 BadPaddingException (javax.crypto.BadPaddingException)69 Cipher (javax.crypto.Cipher)69