<?php
echo sha1('test');
//a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
?>
package demo;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author gan068
*/
public class Sha1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(sha1("test"));
//a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
}
public static String sha1(String str) {
String hashStr = "";
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
BigInteger hash = new BigInteger(1, md.digest());
hashStr = hash.toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hashStr;
}
}
留言