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);
}
}
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 "";
}
}
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())));
}
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).");
}
}
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;
}
Aggregations