12/20/21, 2:20 PM exercise4.
java
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8 import [Link];
9 import [Link];
10 import [Link];
11
12 public class exercise4 {
13 public static void main(String[] args) {
14 Console console = [Link]();
15
16 if (console == null) {
17 [Link]("Console unavailable");
18 return;
19 }
20
21 String password = [Link]("Enter password:");
22
23 try {
24 SecureRandom salt = new SecureRandom();
25 int salt_len = 32;
26 byte salt_bytes[] = new byte[salt_len];
27 [Link](salt_bytes);
28
29 ByteArrayOutputStream data_to_hash = new ByteArrayOutputStream();
30 data_to_hash.write(salt_bytes, 0, salt_len);
31 data_to_hash.write([Link]());
32
33 //#2#: Create an object of type SecretKeyFactory, by using
34 // [Link]() method. The getInstance() method
takes in a string,
35 //which represents the type of SecretKey that we’d like to
generate. Set it to
36 //"PBKDF2WithHmacSHA512". The result of
[Link]() is stored
37 //into a variable, called “skf”• and it is of data type
SecretKeyFactory.
38
39 SecretKeyFactory skf =
[Link]("PBKDF2WithHmacSHA512");
40
41 int keyLength = 256;
42 PBEKeySpec spec = new PBEKeySpec([Link](),
salt_bytes, 1000, keyLength);
43 SecretKey key = [Link](spec);
44 byte[] digest = [Link]();
45
46 String hash_pwd =
[Link](digest).toUpperCase();
47
48 String salt_str =
[Link](salt_bytes).toUpperCase();
49
50 [Link]("Storing into db hash:" + hash_pwd);
51 [Link]("\n");
localhost:4649/?mode=clike 1/2
12/20/21, 2:20 PM [Link]
52 [Link]("Storing into db salt:" + salt_str);
53 [Link]("\n");
54
55 } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
56 [Link]("MD5 not supported for some reason");
57 return;
58 } catch (IOException e) {
59 [Link]("Could not prepare data for hashing");
60 return;
61
62 }
63 }
64 }
65
localhost:4649/?mode=clike 2/2