导入依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
示例代码
package com.study.utils;
import org.apache.commons.codec.digest.DigestUtils;
import java.util.UUID;
public class HashAlgorithm {
public static void main(String[] args) {
String password = "admin@123456";
String md5Hex = DigestUtils.md5Hex(password);
String sha1Hex = DigestUtils.sha1Hex(password);
String sha256Hex = DigestUtils.sha256Hex(password);
String sha512Hex = DigestUtils.sha512Hex(password);
System.out.println("password = " + password);
System.out.println("md5 length = " +md5Hex.length() + " , md5Hex = " + md5Hex);
System.out.println("sha1 length = " + sha1Hex.length() + " , sha1Hex = " + sha1Hex);
System.out.println("sha256 length = " + sha256Hex.length() + " , sha256Hex = " + sha256Hex);
System.out.println("sha512 length = " + sha512Hex.length() + " , sha512Hex = " + sha512Hex);
System.out.println("=====================================================");
String UUIDStr = UUID.randomUUID().toString();
double randomNumber = Math.random();
String randomNumberStr = String.valueOf(randomNumber).substring(2,12);
String md5HexWithUUID = DigestUtils.md5Hex(UUIDStr+password);
String md5HexWithRandomNumber = DigestUtils.md5Hex(randomNumberStr+password);
String sha1HexWithUUID = DigestUtils.sha1Hex(UUIDStr+password);
String sha1HexWithRandomNumber = DigestUtils.sha1Hex(randomNumberStr+password);
System.out.println("password = " + password);
System.out.println("UUIDStr = " + UUIDStr);
System.out.println("randomNumberStr = " + randomNumberStr);
System.out.println("md5HexWithUUID length = " +md5HexWithUUID.length() + " , md5HexWithUUID = " + md5HexWithUUID);
System.out.println("md5HexWithRandomNumber length = " + md5HexWithRandomNumber.length() + " , md5HexWithRandomNumber = " + md5HexWithRandomNumber);
System.out.println("sha1HexWithUUID length = " +sha1HexWithUUID.length() + " , sha1HexWithUUID = " + sha1HexWithUUID);
System.out.println("sha1HexWithRandomNumber length = " + sha1HexWithRandomNumber.length() + " , sha1HexWithRandomNumber = " + sha1HexWithRandomNumber);
}
}