Search in sources :

Example 61 with MessageDigest

use of java.security.MessageDigest 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 62 with MessageDigest

use of java.security.MessageDigest 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 63 with MessageDigest

use of java.security.MessageDigest 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)

Example 64 with MessageDigest

use of java.security.MessageDigest in project fqrouter by fqrouter.

the class IOUtils method copy.

public static String copy(InputStream inputStream, OutputStream outputStream, ChunkCopied callback) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    byte[] buffer = new byte[1024 * 32];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        md5.update(buffer, 0, length);
        if (null != outputStream) {
            outputStream.write(buffer, 0, length);
        }
        if (null != callback) {
            callback.onChunkCopied(buffer, length);
        }
    }
    return new BigInteger(1, md5.digest()).toString(16);
}
Also used : BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest)

Example 65 with MessageDigest

use of java.security.MessageDigest 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)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22