|
1 | 1 | import java.security.MessageDigest;
|
2 | 2 | import java.security.NoSuchAlgorithmException;
|
3 | 3 | import java.security.SecureRandom;
|
| 4 | +import java.util.Base64; |
4 | 5 |
|
5 | 6 | public class HashWithoutSalt {
|
6 | 7 | // BAD - Hash without a salt.
|
7 |
| - public void getSHA256Hash(String password) throws NoSuchAlgorithmException { |
| 8 | + public String getSHA256Hash(String password) throws NoSuchAlgorithmException { |
8 | 9 | MessageDigest md = MessageDigest.getInstance("SHA-256");
|
9 | 10 | byte[] messageDigest = md.digest(password.getBytes());
|
| 11 | + return Base64.getEncoder().encodeToString(messageDigest); |
10 | 12 | }
|
11 | 13 |
|
12 | 14 | // BAD - Hash without a salt.
|
13 |
| - public void getSHA256Hash2(String password) throws NoSuchAlgorithmException { |
| 15 | + public String getSHA256Hash2(String password) throws NoSuchAlgorithmException { |
14 | 16 | MessageDigest md = MessageDigest.getInstance("SHA-256");
|
15 | 17 | md.update(password.getBytes());
|
16 | 18 | byte[] messageDigest = md.digest();
|
| 19 | + return Base64.getEncoder().encodeToString(messageDigest); |
17 | 20 | }
|
18 | 21 |
|
19 | 22 | // GOOD - Hash with a salt.
|
20 |
| - public void getSHA256Hash(String password, byte[] salt) throws NoSuchAlgorithmException { |
| 23 | + public String getSHA256Hash(String password, byte[] salt) throws NoSuchAlgorithmException { |
21 | 24 | MessageDigest md = MessageDigest.getInstance("SHA-256");
|
22 | 25 | md.update(salt);
|
23 | 26 | byte[] messageDigest = md.digest(password.getBytes());
|
| 27 | + return Base64.getEncoder().encodeToString(messageDigest); |
24 | 28 | }
|
25 | 29 |
|
26 | 30 | // GOOD - Hash with a salt.
|
27 |
| - public void getSHA256Hash2(String password, byte[] salt) throws NoSuchAlgorithmException { |
| 31 | + public String getSHA256Hash2(String password, byte[] salt) throws NoSuchAlgorithmException { |
28 | 32 | MessageDigest md = MessageDigest.getInstance("SHA-256");
|
29 | 33 | md.update(salt);
|
30 | 34 | md.update(password.getBytes());
|
31 | 35 | byte[] messageDigest = md.digest();
|
| 36 | + return Base64.getEncoder().encodeToString(messageDigest); |
| 37 | + } |
| 38 | + |
| 39 | + // GOOD - Hash with a salt concatenated with the password. |
| 40 | + public String getSHA256Hash3(String password, byte[] salt) throws NoSuchAlgorithmException { |
| 41 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 42 | + |
| 43 | + byte[] passBytes = password.getBytes(); |
| 44 | + byte[] allBytes = new byte[passBytes.length + salt.length]; |
| 45 | + System.arraycopy(passBytes, 0, allBytes, 0, passBytes.length); |
| 46 | + System.arraycopy(salt, 0, allBytes, passBytes.length, salt.length); |
| 47 | + byte[] messageDigest = md.digest(allBytes); |
| 48 | + |
| 49 | + byte[] cipherBytes = new byte[32 + salt.length]; // SHA-256 is 32 bytes long |
| 50 | + System.arraycopy(messageDigest, 0, cipherBytes, 0, 32); |
| 51 | + System.arraycopy(salt, 0, cipherBytes, 32, salt.length); |
| 52 | + return Base64.getEncoder().encodeToString(cipherBytes); |
32 | 53 | }
|
33 | 54 |
|
34 | 55 | public static byte[] getSalt() throws NoSuchAlgorithmException {
|
35 |
| - SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); |
36 |
| - byte[] salt = new byte[16]; |
37 |
| - sr.nextBytes(salt); |
38 |
| - return salt; |
39 |
| - } |
| 56 | + SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); |
| 57 | + byte[] salt = new byte[16]; |
| 58 | + sr.nextBytes(salt); |
| 59 | + return salt; |
| 60 | + } |
40 | 61 | }
|
0 commit comments