Search in sources :

Example 86 with NoSuchAlgorithmException

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

the class FileUtils method calcMD5.

/**
   * Calculates the MD5 value of a file.
   *
   * @param file The file in question.
   * @return A string that holds the MD5 sum of the file.
   *
   * @throws IOException
   */
public static String calcMD5(final File file) throws IOException {
    // TODO: This method read the entire file in RAM. Make it iterative and
    // use a BufferedReader
    final FileInputStream reader = new FileInputStream(file);
    final byte[] data = new byte[(int) file.length()];
    reader.read(data);
    reader.close();
    final MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (final NoSuchAlgorithmException e) {
        assert false : "MD5 not in list of algorithms";
        throw new RuntimeException(e);
    }
    md.update(data);
    final byte[] digest = md.digest();
    final StringBuilder md5 = new StringBuilder();
    for (final byte b : digest) {
        md5.append(String.format("%02X", b));
    }
    return md5.toString();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 87 with NoSuchAlgorithmException

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

the class PluginManager method download.

/**
	 * Downloads a file with optional checksum verification.
	 *
	 * @param url
	 * @param verifyChecksum
	 * @return
	 * @throws IOException
	 */
protected File download(String url, boolean verifyChecksum) throws IOException {
    File file = downloadFile(url);
    if (!verifyChecksum) {
        return file;
    }
    File sha1File = null;
    try {
        sha1File = downloadFile(url + ".sha1");
    } catch (IOException e) {
    }
    File md5File = null;
    try {
        md5File = downloadFile(url + ".md5");
    } catch (IOException e) {
    }
    if (sha1File == null && md5File == null) {
        throw new IOException("Missing SHA1 and MD5 checksums for " + url);
    }
    String expected;
    MessageDigest md = null;
    if (sha1File != null && sha1File.exists()) {
        // prefer SHA1 to MD5
        expected = FileUtils.readContent(sha1File, "\n").split(" ")[0].trim();
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            logger.error(null, e);
        }
    } else {
        expected = FileUtils.readContent(md5File, "\n").split(" ")[0].trim();
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            logger.error(null, e);
        }
    }
    // calculate the checksum
    FileInputStream is = null;
    try {
        is = new FileInputStream(file);
        DigestInputStream dis = new DigestInputStream(is, md);
        byte[] buffer = new byte[1024];
        while ((dis.read(buffer)) > -1) {
        // read
        }
        dis.close();
        byte[] digest = md.digest();
        String calculated = StringUtils.toHex(digest).trim();
        if (!expected.equals(calculated)) {
            String msg = String.format("Invalid checksum for %s\nAlgorithm:  %s\nExpected:   %s\nCalculated: %s", file.getAbsolutePath(), md.getAlgorithm(), expected, calculated);
            file.delete();
            throw new IOException(msg);
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return file;
}
Also used : DigestInputStream(java.security.DigestInputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) File(java.io.File) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FileInputStream(java.io.FileInputStream)

Example 88 with NoSuchAlgorithmException

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

the class TicketModel method getSHA1.

/**
	 * Calculates the SHA1 of the byte array.
	 *
	 * @param bytes
	 * @return sha1 of the byte array
	 */
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 89 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 90 with NoSuchAlgorithmException

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

the class SecureHashUtil method makeSHA1HashBase64.

public static String makeSHA1HashBase64(byte[] bytes) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
Also used : 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