-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.html
More file actions
174 lines (160 loc) · 4.94 KB
/
Copy pathassignment.html
File metadata and controls
174 lines (160 loc) · 4.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cryptographic Operations</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
color: #333;
}
.input-section {
margin-bottom: 20px;
text-align: center;
}
input {
padding: 8px;
margin: 5px;
width: 200px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#output {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#output p {
margin: 10px 0;
word-wrap: break-word;
}
.error {
color: red;
text-align: center;
}
</style>
</head>
<body>
<h1>Cryptographic Operations</h1>
<div class="input-section">
<input type="text" id="firstName" placeholder="First Name" required>
<input type="text" id="lastName" placeholder="Last Name" required>
<br>
<button onclick="runCrypto()">Generate and Verify</button>
</div>
<div id="output"></div>
<script>
// Function to convert ArrayBuffer to hexadecimal string
function bufferToHex(buffer) {
return Array.from(new Uint8Array(buffer))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
// Main async function to perform cryptographic operations
async function cryptoOperations(firstName, lastName) {
try {
// Step 1: Create a Private Key (and Public Key pair)
const keyPair = await crypto.subtle.generateKey(
{
name: 'ECDSA',
namedCurve: 'P-256',
},
true,
['sign', 'verify']
);
// Step 2: Derive Public Key
const publicKey = keyPair.publicKey;
const privateKey = keyPair.privateKey;
// Export public key to raw format
const publicKeyRaw = await crypto.subtle.exportKey('raw', publicKey);
const publicKeyHex = bufferToHex(publicKeyRaw);
// Step 3: Generate Address (simplified)
const publicKeyHash = await crypto.subtle.digest('SHA-256', publicKeyRaw);
const address = bufferToHex(publicKeyHash).slice(0, 40);
// Step 4: Create Message
const message = `My name is ${firstName} ${lastName}`;
// Step 5: Hash the Message
const msgBuffer = new TextEncoder().encode(message);
const msgHash = await crypto.subtle.digest('SHA-256', msgBuffer);
const msgHashHex = bufferToHex(msgHash);
// Step 6: Create Digital Signature
const signature = await crypto.subtle.sign(
{
name: 'ECDSA',
hash: { name: 'SHA-256' },
},
privateKey,
msgHash
);
const signatureHex = bufferToHex(signature);
// Step 7: Verify the Digital Signature
const isValid = await crypto.subtle.verify(
{
name: 'ECDSA',
hash: { name: 'SHA-256' },
},
publicKey,
signature,
msgHash
);
// Return results for display
return {
publicKey: publicKeyHex,
address,
message,
messageHash: msgHashHex,
signature: signatureHex,
isValid,
};
} catch (error) {
throw new Error('Cryptographic operation failed: ' + error.message);
}
}
// Function to run crypto operations and update UI
async function runCrypto() {
const firstName = document.getElementById('firstName').value.trim();
const lastName = document.getElementById('lastName').value.trim();
const outputDiv = document.getElementById('output');
// Input validation
if (!firstName || !lastName) {
outputDiv.innerHTML = '<p class="error">Please enter both first and last names.</p>';
return;
}
outputDiv.innerHTML = '<p>Processing...</p>';
try {
const result = await cryptoOperations(firstName, lastName);
outputDiv.innerHTML = `
<p><strong>Public Key (hex):</strong> ${result.publicKey}</p>
<p><strong>Address:</strong> ${result.address}</p>
<p><strong>Message:</strong> ${result.message}</p>
<p><strong>Message Hash (hex):</strong> ${result.messageHash}</p>
<p><strong>Digital Signature (hex):</strong> ${result.signature}</p>
<p><strong>Signature Verification:</strong> ${result.isValid ? 'Valid' : 'Invalid'}</p>
`;
} catch (error) {
outputDiv.innerHTML = `<p class="error">${error.message}</p>`;
}
}
</script>
</body>
</html>