Java equivalent to MySql and PHP SHA1
The following code snippet demonstrates Java's equivalent to PHP and MySql's SHA1 function:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA1Test {
public static void main(String[] args){
String hashStr = null;
String password = "password";
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(password.getBytes());
BigInteger hash = new BigInteger(1, md.digest());
hashStr = hash.toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
System.out.println(hashStr);
//outputs: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
}
}mysql> select sha1('password');
+------------------------------------------+
| sha1('password') |
+------------------------------------------+
| 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 |
+------------------------------------------+
1 row in set (0.00 sec)
Comments
Post a Comment