...
| Code Block | ||
|---|---|---|
| ||
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class Password {
private byte[] salt = "ia0942980234241sadfaewvo32".getBytes(); //Randomly generated
private void setPassword(byte[] pass) throws Exception {
byte[] input = appendArrays(pass, salt);
MessageDigest sha_1 = MessageDigest.getInstance("SHA-1");
byte[] hashVal = sha_1.digest(input); //encode the string and salt Â
clearArray(pass); Â
clearArray(input); Â
saveBytes(hashVal,"credentials.pw"); //save the hash value to credentials.pw
}
private boolean checkPassword(byte[] pass) throws Exception {
byte[] input = appendArrays(pass, salt);
MessageDigest sha_1 = MessageDigest.getInstance("SHA-1");
byte[] hashVal1 = sha_1.digest(input); //encode the string and salt
clearArray(pass);
clearArray(input);
byte[] hashVal2 = loadBytes("credentials.pw"); //load the hash value stored in credentials.pw
return Arrays.equals(hashVal1, hashVal2);
}
private byte[] generateSalt(int n) {
//Generate a random byte array of length n
}
private byte[] appendArrays(byte[] a, byte[] b) {
//Return a new array of a appended to b
}
private void clearArray(byte[] a) {
//set all of the elements in a to zero
}
}
|
...
| Wiki Markup |
|---|
\[[API 2006|AA. Bibliography#API 06]\] Class {{java.security.MessageDigest}} |
http://www.javapractices.com/topic/TopicAction.do?Id=216
Passwords never in clear text