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