可以使用Apache commons中的DigestUtils生成SHA1哈希值。
sha1 = org.apache.commons.codec.digest.DigestUtils.sha1Hex( value );
也可以使用jdk中的MessageDigest类直接生成SHA1哈希:
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(value.getBytes("utf8"));
sha1 = String.format("%040x", new BigInteger(1, digest.digest()));
这是创建字符串sha1的一个示例“这是一个测试”。 首先使用jdk中使用MessageDigest类的方法。 然后它使用Apache commons中的DigestUtils类。 生成的sha1被写入控制台。
import java.math.BigInteger;
import java.security.MessageDigest;
public class SHA1 {
public static void main(String[] argv){
String value = "this is a test";
String sha1 = "";
// With the java libraries
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(value.getBytes("utf8"));
sha1 = String.format("%040x", new BigInteger(1, digest.digest()));
} catch (Exception e){
e.printStackTrace();
}
System.out.println( "The sha1 of \""+ value + "\" is:");
System.out.println( sha1 );
System.out.println();
// With Apache commons
sha1 = org.apache.commons.codec.digest.DigestUtils.sha1Hex( value );
System.out.println( "The sha1 of \""+ value + "\" is:");
System.out.println( sha1 );
}
}
The sha1 of "this is a test" is: fa26be19de6bff93f70bc2308434e4a440bbad02 The sha1 of "this is a test" is: fa26be19de6bff93f70bc2308434e4a440bbad02
Java 8
Online tool generating SHA1
SHA1 Decoder