Search in sources :

Example 46 with MessageDigest

use of java.security.MessageDigest in project siena by mandubian.

the class Util method sha1.

public static String sha1(String message) {
    try {
        byte[] buffer = message.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(buffer);
        byte[] digest = md.digest();
        char[] hash = new char[40];
        for (int i = 0, n = 0; i < digest.length; i++) {
            byte aux = digest[i];
            int b = aux & 0xff;
            String hex = Integer.toHexString(b);
            if (hex.length() == 1) {
                hash[n++] = '0';
                hash[n++] = hex.charAt(0);
            } else {
                hash[n++] = hex.charAt(0);
                hash[n++] = hex.charAt(1);
            }
        }
        return new String(hash);
    } catch (Exception e) {
        // should never happen. UTF-8 and SHA1 are constants
        throw new RuntimeException(e);
    }
}
Also used : MessageDigest(java.security.MessageDigest) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 47 with MessageDigest

use of java.security.MessageDigest in project databus by linkedin.

the class RegistrationIdGenerator method generateByteHash.

/**
	 * Generate a hash out of the String id
	 *
	 * @param id
	 * @return
	 */
private static String generateByteHash(String id) {
    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(id.getBytes(Charset.forName("UTF8")));
        final byte[] resultsByte = messageDigest.digest();
        String hash = new String(Hex.encodeHex(resultsByte));
        final int length = 8;
        if (hash.length() > length)
            hash = hash.substring(0, length);
        return hash;
    } catch (NoSuchAlgorithmException nse) {
        LOG.error("Unexpected error : Got NoSuchAlgorithm exception for MD5");
        return "";
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 48 with MessageDigest

use of java.security.MessageDigest in project spring-security by spring-projects.

the class TokenBasedRememberMeServices method makeTokenSignature.

/**
	 * Calculates the digital signature to be put in the cookie. Default value is MD5
	 * ("username:tokenExpiryTime:password:key")
	 */
protected String makeTokenSignature(long tokenExpiryTime, String username, String password) {
    String data = username + ":" + tokenExpiryTime + ":" + password + ":" + getKey();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("No MD5 algorithm available!");
    }
    return new String(Hex.encode(digest.digest(data.getBytes())));
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 49 with MessageDigest

use of java.security.MessageDigest in project spring-security-oauth by spring-projects.

the class DefaultClientKeyGenerator method extractKey.

public String extractKey(OAuth2ProtectedResourceDetails resource, Authentication authentication) {
    Map<String, String> values = new LinkedHashMap<String, String>();
    if (authentication != null) {
        values.put(USERNAME, authentication.getName());
    }
    values.put(CLIENT_ID, resource.getClientId());
    if (resource.getScope() != null) {
        values.put(SCOPE, OAuth2Utils.formatParameterList(resource.getScope()));
    }
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("MD5 algorithm not available.  Fatal (should be in the JDK).");
    }
    try {
        byte[] bytes = digest.digest(values.toString().getBytes("UTF-8"));
        return String.format("%032x", new BigInteger(1, bytes));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("UTF-8 encoding not available.  Fatal (should be in the JDK).");
    }
}
Also used : BigInteger(java.math.BigInteger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with MessageDigest

use of java.security.MessageDigest in project springside4 by springside.

the class HashUtil method get.

private static MessageDigest get(ThreadLocal<MessageDigest> messageDigest) {
    MessageDigest instance = messageDigest.get();
    instance.reset();
    return instance;
}
Also used : 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