Search in sources :

Example 66 with MessageDigest

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

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

the class DeepCopier method checksum.

/**
	 * Utility method to calculate the checksum of an object.
	 * @param sourceObject The object from which to establish the checksum.
	 * @return The checksum
	 * @throws IOException
	 */
public static BigInteger checksum(Object sourceObject) {
    if (sourceObject == null) {
        return BigInteger.ZERO;
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(sourceObject);
        oos.close();
        MessageDigest m = MessageDigest.getInstance("SHA-1");
        m.update(baos.toByteArray());
        return new BigInteger(1, m.digest());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
    // impossible
    }
    return BigInteger.ZERO;
}
Also used : BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ObjectOutputStream(java.io.ObjectOutputStream) MessageDigest(java.security.MessageDigest)

Example 68 with MessageDigest

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

the class DexFile method calcSignature.

/**
     * Calculates the signature for the {@code .dex} file in the
     * given array, and modify the array to contain it.
     *
     * @param bytes {@code non-null;} the bytes of the file
     */
private static void calcSignature(byte[] bytes) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
    md.update(bytes, 32, bytes.length - 32);
    try {
        int amt = md.digest(bytes, 12, 20);
        if (amt != 20) {
            throw new RuntimeException("unexpected digest write: " + amt + " bytes");
        }
    } catch (DigestException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 69 with MessageDigest

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

the class SecureHashUtil method makeHash.

private static String makeHash(byte[] bytes, String algorithm) {
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(bytes, 0, bytes.length);
        byte[] hash = md.digest();
        return convertToHex(hash);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 70 with MessageDigest

use of java.security.MessageDigest in project XobotOS by xamarin.

the class HttpResponseCache method uriToKey.

private String uriToKey(URI uri) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5bytes = messageDigest.digest(uri.toString().getBytes(Charsets.UTF_8));
        return IntegralToString.bytesToHexString(md5bytes, false);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(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