...
Code Block | ||
---|---|---|
| ||
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class Password { private void setPassword(byte[] saltpass) = "ia0942980234241sadfaewvo32".getBytes(); //Randomly generated throws Exception { private void setPassword(byte[] pass)salt throws Exception {= generateSalt(12); 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(salt, "salt.bin");  saveBytes(hashVal,"credentialspassword.pwbin"); //save the hash value to credentials.pw } private boolean checkPassword(byte[] pass) throws Exception { byte[] salt = loadBytes("salt.bin"); 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 } } |
...