Search in sources :

Example 31 with MessageDigest

use of java.security.MessageDigest in project otter by alibaba.

the class SecurityUtils method getMD5Str.

/**
     * MD5 加密
     */
public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException caught!");
        System.exit(-1);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }
    return md5StrBuff.toString();
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 32 with MessageDigest

use of java.security.MessageDigest in project grpc-java by grpc.

the class Util method shaBase64.

/** Returns a Base 64-encoded string containing a SHA-1 hash of {@code s}. */
public static String shaBase64(String s) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        byte[] sha1Bytes = messageDigest.digest(s.getBytes("UTF-8"));
        return ByteString.of(sha1Bytes).base64();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 33 with MessageDigest

use of java.security.MessageDigest in project grpc-java by grpc.

the class Util method sha1.

/** Returns a SHA-1 hash of {@code s}. */
public static ByteString sha1(ByteString s) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        byte[] sha1Bytes = messageDigest.digest(s.toByteArray());
        return ByteString.of(sha1Bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 34 with MessageDigest

use of java.security.MessageDigest in project hibernate-orm by hibernate.

the class NamingHelper method hashedName.

/**
	 * Hash a constraint name using MD5. Convert the MD5 digest to base 35
	 * (full alphanumeric), guaranteeing
	 * that the length of the name will always be smaller than the 30
	 * character identifier restriction enforced by a few dialects.
	 *
	 * @param s The name to be hashed.
	 *
	 * @return String The hashed name.
	 */
public String hashedName(String s) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(s.getBytes());
        byte[] digest = md.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        // character identifier restriction enforced by a few dialects.
        return bigInt.toString(35);
    } catch (NoSuchAlgorithmException e) {
        throw new HibernateException("Unable to generate a hashed name!", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 35 with MessageDigest

use of java.security.MessageDigest in project iosched by google.

the class CloudFileManager method calulateHash.

public static byte[] calulateHash(JsonElement contents) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new InternalError("MD5 MessageDigest is not available");
    }
    OutputStream byteSink = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // ignore, since this is only used to calculate MD5
        }
    };
    DigestOutputStream dis = new DigestOutputStream(byteSink, md);
    new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
    return dis.getMessageDigest().digest();
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) 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