"Result: ": //#1#: Use MD'S Update Method. Use Data - To - Hash - Tobytearray As Input To MD'S Update Method
"Result: ": //#1#: Use MD'S Update Method. Use Data - To - Hash - Tobytearray As Input To MD'S Update Method
"Result: ": //#1#: Use MD'S Update Method. Use Data - To - Hash - Tobytearray As Input To MD'S Update Method
java
1 import javax.xml.bind.DatatypeConverter;
2 import java.io.ByteArrayOutputStream;
3 import java.io.Console;
4 import java.io.IOException;
5 import java.security.MessageDigest;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.SecureRandom;
8
9 public class exercise3 {
10 public static void main(String[] args) {
11 SecureRandom secureRandom = new SecureRandom();
12 byte[] byte_sal = new byte[16];
13 secureRandom.nextBytes(byte_sal);
14 String sal = DatatypeConverter.printHexBinary(byte_sal);
15 System.out.println("Result: "+sal);
16
17 Console console = System.console();
18 if (console == null) {
19 System.out.print("Console unavailable");
20 return;
21 }
22
23 String password = console.readLine("Enter password:");
24
25 try {
26 SecureRandom salt = new SecureRandom();
27 int salt_len = 16;
28 byte salt_bytes[] = new byte[salt_len];
29 salt.nextBytes(salt_bytes);
30
31 ByteArrayOutputStream data_to_hash = new ByteArrayOutputStream();
32 data_to_hash.write(salt_bytes, 0, salt_len);
33 data_to_hash.write(password.getBytes());
34
35 MessageDigest md = MessageDigest.getInstance("MD5");
36
37
38 //#1#: Use md’s update() method. Use data_to_hash.toByteArray() as
input to md’s update() method
39
40 md.update(data_to_hash.toByteArray());
41
42 byte[] digest = md.digest();
43 String hash_pwd =
DatatypeConverter.printHexBinary(digest).toUpperCase();
44
45 String salt_str =
DatatypeConverter.printHexBinary(salt_bytes).toUpperCase();
46
47 console.printf("Storing into db hash:" + hash_pwd);
48 console.printf("\n");
49 console.printf("Storing into db salt:" + salt_str);
50 console.printf("\n");
51
52 } catch (NoSuchAlgorithmException e) {
53 System.out.print("MD5 not supported for some reason");
54 return;
55 } catch (IOException e) {
56 System.out.print("Could not prepare data for hashing");
localhost:4649/?mode=clike 1/2
12/20/21, 2:20 PM exercise3.java
57 return;
58 }
59 }
60
61 }
62
localhost:4649/?mode=clike 2/2