-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSslServerApplication.java
More file actions
55 lines (45 loc) · 1.97 KB
/
Copy pathSslServerApplication.java
File metadata and controls
55 lines (45 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.snhu.sslserver;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class SslServerApplication {
public static void main(String[] args) {
SpringApplication.run(SslServerApplication.class, args);
}
}
@RestController
class ServerController {
// Function to calculate the SHA-256 checksum
public String calculateHash(String data) throws NoSuchAlgorithmException {
// Instantiate the SHA-256 MessageDigest
MessageDigest digest = MessageDigest.getInstance("SHA-256");
// Hash the data string into a byte array
byte[] hash = digest.digest(data.getBytes(StandardCharsets.UTF_8));
// Convert the byte array into a readable Hex string format
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
@RequestMapping("/hash")
public String myHash() throws NoSuchAlgorithmException {
// Your unique data string combining the static requirement and validation details
String data = "Rebecca Scranton - Hello World Check Sum!";
// Generate the checksum
String checksum = calculateHash(data);
// Return the formatted HTML output for the browser
return "<p><strong>Data:</strong> " + data + "</p>" +
"<p><strong>Name:</strong> Rebecca Scranton</p>" +
"<p><strong>Checksum Value (SHA-256):</strong> " + checksum + "</p>";
}
}