Add more cases

This commit is contained in:
luchua-bc
2021-01-15 16:20:31 +00:00
parent 86c04e6971
commit 3af8773dd6
3 changed files with 48 additions and 3 deletions

View File

@@ -52,6 +52,23 @@ public class HashWithoutSalt {
return Base64.getEncoder().encodeToString(cipherBytes);
}
// GOOD - Hash with a given salt stored somewhere else.
public String getSHA256Hash(String password, String salt) throws NoSuchAlgorithmException {
return hash(password+salt);
}
// GOOD - Hash with a salt for a variable named passwordHash, whose value is a hash used as an input for a hashing function.
public String getSHA256Hash3(String passwordHash) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] messageDigest = md.digest(passwordHash.getBytes());
return Base64.getEncoder().encodeToString(messageDigest);
}
private String hash(String payload) {
MessageDigest alg = MessageDigest.getInstance("SHA-256");
return Base64.getEncoder().encodeToString(alg.digest(payload.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
}
public static byte[] getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];

View File

@@ -0,0 +1,20 @@
import java.security.MessageDigest;
public class SHA256 {
MessageDigest md;
public int getBlockSize() {return 32;}
public void init() throws Exception {
try { md = MessageDigest.getInstance("SHA-256"); }
catch (Exception e){
System.err.println(e);
}
}
public void update(byte[] foo, int start, int len) throws Exception {
md.update(foo, start, len);
}
public byte[] digest() throws Exception {
return md.digest();
}
}