MessageDigest md = MessageDigest.getInstance("MD5");
md.update(input.getBytes(StandardCharsets.UTF_8));
byte[] hashBytes = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
// com.google.common.hash.Hashing.md5()
// If you must interoperate with a system that requires MD5, then use this method, despite its deprecation. But if you can choose your hash function, avoid MD5, which is neither fast nor secure. As of January 2017, we suggest:
// For security: Hashing.sha256() or a higher-level API.
// For speed: Hashing.goodFastHash(int), though see its docs for caveats.
HashFunction hashFunction = Hashing.md5();
HashCode hash = hashFunction.hashString(input, StandardCharsets.UTF_8);
return hash.toString();
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
String md5 = DigestUtils.md5Hex( input );
return md5;
原文:https://www.cnblogs.com/ants_double/p/11484432.html