|
| 1 | +import java.security.MessageDigest; |
| 2 | +import org.apache.commons.codec.digest.DigestUtils; |
| 3 | +import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; |
| 4 | + |
| 5 | +public class Bad { |
| 6 | + public byte[] bad1(String password) { |
| 7 | + // ruleid: use-of-sha224 |
| 8 | + MessageDigest sha224Digest = MessageDigest.getInstance("SHA-224"); |
| 9 | + sha224Digest.update(password.getBytes()); |
| 10 | + byte[] hashValue = sha224Digest.digest(); |
| 11 | + return hashValue; |
| 12 | + } |
| 13 | + |
| 14 | + public byte[] bad2(String password) { |
| 15 | + // ruleid: use-of-sha224 |
| 16 | + byte[] hashValue = DigestUtils.getSha3_224Digest().digest(password.getBytes()); |
| 17 | + return hashValue; |
| 18 | + } |
| 19 | + |
| 20 | + public void bad3() { |
| 21 | + // ruleid: use-of-sha224 |
| 22 | + java.security.MessageDigest md = java.security.MessageDigest.getInstance("sha224", "SUN"); |
| 23 | + byte[] input = { (byte) '?' }; |
| 24 | + Object inputParam = bar; |
| 25 | + if (inputParam instanceof String) |
| 26 | + input = ((String) inputParam).getBytes(); |
| 27 | + if (inputParam instanceof java.io.InputStream) { |
| 28 | + byte[] strInput = new byte[1000]; |
| 29 | + int i = ((java.io.InputStream) inputParam).read(strInput); |
| 30 | + if (i == -1) { |
| 31 | + response.getWriter() |
| 32 | + .println( |
| 33 | + "This input source requires a POST, not a GET. Incompatible UI for the InputStream source."); |
| 34 | + return; |
| 35 | + } |
| 36 | + input = java.util.Arrays.copyOf(strInput, i); |
| 37 | + } |
| 38 | + md.update(input); |
| 39 | + byte[] result = md.digest(); |
| 40 | + java.io.File fileTarget = new java.io.File( |
| 41 | + new java.io.File(org.owasp.benchmark.helpers.Utils.TESTFILES_DIR), |
| 42 | + "passwordFile.txt"); |
| 43 | + java.io.FileWriter fw = new java.io.FileWriter(fileTarget, true); // the true will append the new data |
| 44 | + fw.write( |
| 45 | + "hash_value=" |
| 46 | + + org.owasp.esapi.ESAPI.encoder().encodeForBase64(result, true) |
| 47 | + + "\n"); |
| 48 | + fw.close(); |
| 49 | + } |
| 50 | + |
| 51 | + public byte[] bad4(String password) { |
| 52 | + // ruleid: use-of-sha224 |
| 53 | + byte [] hashValue = new DigestUtils(SHA_224).digest(password.getBytes()); |
| 54 | + return hashValue; |
| 55 | + } |
| 56 | +} |
0 commit comments