1+ import java .io .FileInputStream ;
2+ import java .io .IOException ;
3+ import java .security .MessageDigest ;
4+ import java .security .SecureRandom ;
5+ import java .util .Arrays ;
6+ import java .util .Base64 ;
7+ import java .util .Properties ;
8+ import javax .crypto .Mac ;
9+ import javax .crypto .SecretKey ;
10+ import javax .crypto .SecretKeyFactory ;
11+ import javax .crypto .spec .PBEKeySpec ;
12+ import javax .crypto .spec .SecretKeySpec ;
13+
14+ public class Test {
15+
16+ public static byte [] generateSalt (int length ) {
17+ SecureRandom random = new SecureRandom ();
18+ byte [] salt = new byte [length ];
19+ random .nextBytes (salt );
20+ return salt ;
21+ }
22+
23+ /**
24+ * PBKDF2 derivation with a very low iteration count.
25+ *
26+ * SAST/CBOM: - Parent: PBKDF2. - Iteration count is only 10, which is far
27+ * below acceptable security standards. - Flagged as insecure.
28+ */
29+ public void pbkdf2LowIteration (String password ) throws Exception {
30+ byte [] salt = generateSalt (16 );
31+ int iterationCount = 10 ; // $Source
32+ PBEKeySpec spec = new PBEKeySpec (password .toCharArray (), salt , iterationCount , 256 ); // $Alert[java/quantum/weak-kdf-iteration-count]
33+ SecretKeyFactory factory = SecretKeyFactory .getInstance ("PBKDF2WithHmacSHA256" );
34+ byte [] key = factory .generateSecret (spec ).getEncoded ();
35+ }
36+
37+ /**
38+ * PBKDF2 derivation with a very low iteration count.
39+ *
40+ * SAST/CBOM: - Parent: PBKDF2. - Iteration count is only 10, which is far
41+ * below acceptable security standards. - Flagged as insecure.
42+ */
43+ public void pbkdf2LowIteration (String password , int iterationCount ) throws Exception { // $Source
44+ byte [] salt = generateSalt (16 );
45+ PBEKeySpec spec = new PBEKeySpec (password .toCharArray (), salt , iterationCount , 256 ); // $Alert[java/quantum/unknown-kdf-iteration-count]
46+ SecretKeyFactory factory = SecretKeyFactory .getInstance ("PBKDF2WithHmacSHA256" );
47+ byte [] key = factory .generateSecret (spec ).getEncoded ();
48+ }
49+
50+ /**
51+ * PBKDF2 derivation with a high iteration count.
52+ *
53+ * SAST/CBOM: - Parent: PBKDF2. - Uses 1,000,000 iterations; this is secure
54+ * but may impact performance.
55+ */
56+ public void pbkdf2HighIteration (String password ) throws Exception {
57+ byte [] salt = generateSalt (16 );
58+ int iterationCount = 1_000_000 ;
59+ PBEKeySpec spec = new PBEKeySpec (password .toCharArray (), salt , iterationCount , 256 );
60+ SecretKeyFactory factory = SecretKeyFactory .getInstance ("PBKDF2WithHmacSHA256" );
61+ byte [] key = factory .generateSecret (spec ).getEncoded ();
62+ }
63+ }
0 commit comments