Java MD5 哈希计算示例:使用 Google Guava 库
这段代码使用了 Google Guava 库中的 Hashing 类来进行 MD5 哈希计算。首先,我们指定要哈希的字符串为'example',然后使用 UTF-8 字符集将其转换为字节数组。接下来,使用 md5() 方法创建一个 MD5 哈希函数,并使用 hashString() 方法计算哈希值。最后,使用 toString() 方法将哈希值转换为字符串形式,并打印出来。
import com.google.common.hash.Hashing;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) {
String str = "example";
String hashedString = Hashing.md5().hashString(str, Charset.forName("UTF-8")).toString();
System.out.println(hashedString);
}
}
原文地址: https://www.cveoy.top/t/topic/v6v 著作权归作者所有。请勿转载和采集!