-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign.js
More file actions
371 lines (308 loc) · 13.3 KB
/
Copy pathsign.js
File metadata and controls
371 lines (308 loc) · 13.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Digital File Signer - Philip Rosedale
// Handles file upload, hashing, signing, and verification
class DigitalSigner {
constructor() {
this.initializeElements();
this.bindEvents();
}
initializeElements() {
// File upload elements
this.fileInput = document.getElementById('fileInput');
this.fileInfo = document.getElementById('fileInfo');
this.privateKeyInput = document.getElementById('privateKey');
this.signButton = document.getElementById('signButton');
// Results elements
this.results = document.getElementById('results');
this.fileHash = document.getElementById('fileHash');
this.signature = document.getElementById('signature');
this.publicKey = document.getElementById('publicKey');
this.copyResults = document.getElementById('copyResults');
this.downloadResults = document.getElementById('downloadResults');
// Verification elements
this.verifyFile = document.getElementById('verifyFile');
this.verifySignature = document.getElementById('verifySignature');
this.verifyPublicKey = document.getElementById('verifyPublicKey');
this.verifyButton = document.getElementById('verifyButton');
this.verificationResult = document.getElementById('verificationResult');
this.resultStatus = document.getElementById('resultStatus');
}
bindEvents() {
// File upload events
this.fileInput.addEventListener('change', (e) => this.handleFileSelect(e));
this.privateKeyInput.addEventListener('input', () => this.updateSignButton());
this.signButton.addEventListener('click', () => this.signFile());
// Results events
this.copyResults.addEventListener('click', () => this.copyResultsToClipboard());
this.downloadResults.addEventListener('click', () => this.downloadResults());
// Verification events
console.log('Binding verify button event...');
this.verifyButton.addEventListener('click', () => {
console.log('Verify button clicked!');
this.verifyFileSignature();
});
}
handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
this.fileInfo.innerHTML = `
<div class="file-details">
<strong>${file.name}</strong><br>
Size: ${this.formatFileSize(file.size)}<br>
Type: ${file.type || 'Unknown'}
</div>
`;
this.updateSignButton();
} else {
this.fileInfo.innerHTML = '';
this.updateSignButton();
}
}
updateSignButton() {
const hasFile = this.fileInput.files.length > 0;
const hasPrivateKey = this.privateKeyInput.value.trim().length > 0;
this.signButton.disabled = !(hasFile && hasPrivateKey);
}
async signFile() {
const file = this.fileInput.files[0];
const privateKeyPem = this.privateKeyInput.value.trim();
if (!file || !privateKeyPem) {
this.showError('Please select a file and provide a private key.');
return;
}
try {
this.signButton.disabled = true;
this.signButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Signing...';
// Generate file hash
const fileHash = await this.generateFileHash(file);
// Parse private key
const privateKey = this.parsePrivateKey(privateKeyPem);
// Sign the hash
const signature = this.signHash(fileHash, privateKey);
// Extract public key
const publicKeyPem = this.extractPublicKey(privateKey);
// Display results
this.displayResults(fileHash, signature, publicKeyPem);
} catch (error) {
console.error('Signing error:', error);
this.showError('Error signing file: ' + error.message);
} finally {
this.signButton.disabled = false;
this.signButton.innerHTML = '<i class="fas fa-signature"></i> Sign File';
}
}
async generateFileHash(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
const arrayBuffer = e.target.result;
const wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
const hash = CryptoJS.SHA256(wordArray);
resolve(hash.toString(CryptoJS.enc.Hex));
} catch (error) {
reject(error);
}
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
parsePrivateKey(privateKeyPem) {
try {
// Remove headers and footers
const keyContent = privateKeyPem
.replace(/-----BEGIN PRIVATE KEY-----/, '')
.replace(/-----END PRIVATE KEY-----/, '')
.replace(/-----BEGIN RSA PRIVATE KEY-----/, '')
.replace(/-----END RSA PRIVATE KEY-----/, '')
.replace(/\s/g, '');
// Decode base64
const keyBytes = forge.util.decode64(keyContent);
// Parse as ASN.1
const asn1 = forge.asn1.fromDer(keyBytes);
return forge.pki.privateKeyFromAsn1(asn1);
} catch (error) {
throw new Error('Invalid private key format. Please provide a valid PEM-encoded private key.');
}
}
signHash(hash, privateKey) {
try {
// Convert hex hash to bytes using forge
const hashBytes = forge.util.hexToBytes(hash);
// Create signature using the raw hash bytes with no digest algorithm
const signature = privateKey.sign(hashBytes, 'NONE');
// Return base64 encoded signature
return forge.util.encode64(signature);
} catch (error) {
throw new Error('Error creating signature: ' + error.message);
}
}
extractPublicKey(privateKey) {
try {
// For RSA keys, we need to create a public key from the private key components
const publicKey = forge.pki.setRsaPublicKey(privateKey.n, privateKey.e);
const publicKeyPem = forge.pki.publicKeyToPem(publicKey);
return publicKeyPem;
} catch (error) {
throw new Error('Error extracting public key: ' + error.message);
}
}
displayResults(fileHash, signature, publicKeyPem) {
this.fileHash.textContent = fileHash;
this.signature.textContent = signature;
this.publicKey.textContent = publicKeyPem;
this.results.style.display = 'block';
// Scroll to results
this.results.scrollIntoView({ behavior: 'smooth' });
}
async verifyFileSignature() {
const file = this.verifyFile.files[0];
const signatureText = this.verifySignature.value.trim();
const publicKeyPem = this.verifyPublicKey.value.trim();
console.log('Verification started:', {
hasFile: !!file,
hasSignature: !!signatureText,
hasPublicKey: !!publicKeyPem
});
if (!file || !signatureText || !publicKeyPem) {
this.showError('Please provide a file, signature, and public key for verification.');
return;
}
try {
this.verifyButton.disabled = true;
this.verifyButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Verifying...';
console.log('Generating file hash...');
// Generate file hash
const fileHash = await this.generateFileHash(file);
console.log('File hash generated:', fileHash);
console.log('Parsing public key...');
// Parse public key
const publicKey = this.parsePublicKey(publicKeyPem);
console.log('Public key parsed successfully');
console.log('Verifying signature...');
// Verify signature
const isValid = this.verifyHash(fileHash, signatureText, publicKey);
console.log('Verification result:', isValid);
// Display result
this.displayVerificationResult(isValid, fileHash);
} catch (error) {
console.error('Verification error:', error);
this.showError('Error verifying signature: ' + error.message);
} finally {
this.verifyButton.disabled = false;
this.verifyButton.innerHTML = '<i class="fas fa-check-circle"></i> Verify Signature';
}
}
parsePublicKey(publicKeyPem) {
try {
return forge.pki.publicKeyFromPem(publicKeyPem);
} catch (error) {
throw new Error('Invalid public key format. Please provide a valid PEM-encoded public key.');
}
}
verifyHash(hash, signatureBase64, publicKey) {
try {
// Convert hex hash to bytes
const hashBytes = forge.util.hexToBytes(hash);
// Decode signature
const signature = forge.util.decode64(signatureBase64);
// Verify signature using the raw hash bytes with no digest algorithm
return publicKey.verify(hashBytes, signature, 'NONE');
} catch (error) {
throw new Error('Error verifying signature: ' + error.message);
}
}
displayVerificationResult(isValid, fileHash) {
const statusHtml = `
<div class="verification-status ${isValid ? 'valid' : 'invalid'}">
<i class="fas fa-${isValid ? 'check-circle' : 'times-circle'}"></i>
<strong>${isValid ? 'Signature Valid' : 'Signature Invalid'}</strong>
<p>File Hash: ${fileHash}</p>
<p>${isValid ? 'This file was authentically signed by the private key owner.' : 'This file was not signed by the provided public key, or has been modified.'}</p>
</div>
`;
this.resultStatus.innerHTML = statusHtml;
this.verificationResult.style.display = 'block';
// Scroll to result
this.verificationResult.scrollIntoView({ behavior: 'smooth' });
}
async copyResultsToClipboard() {
const results = {
fileHash: this.fileHash.textContent,
signature: this.signature.textContent,
publicKey: this.publicKey.textContent,
timestamp: new Date().toISOString()
};
const resultsText = `Digital Signature Results
Generated: ${results.timestamp}
File Hash (SHA-256):
${results.fileHash}
Digital Signature:
${results.signature}
Public Key:
${results.publicKey}`;
try {
await navigator.clipboard.writeText(resultsText);
this.showSuccess('Results copied to clipboard!');
} catch (error) {
this.showError('Failed to copy to clipboard: ' + error.message);
}
}
downloadResults() {
const results = {
fileHash: this.fileHash.textContent,
signature: this.signature.textContent,
publicKey: this.publicKey.textContent,
timestamp: new Date().toISOString()
};
const resultsText = `Digital Signature Results
Generated: ${results.timestamp}
File Hash (SHA-256):
${results.fileHash}
Digital Signature:
${results.signature}
Public Key:
${results.publicKey}`;
const blob = new Blob([resultsText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'digital-signature-results.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
this.showSuccess('Results downloaded!');
}
formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
showError(message) {
// Create a simple error notification
const notification = document.createElement('div');
notification.className = 'notification error';
notification.innerHTML = `<i class="fas fa-exclamation-triangle"></i> ${message}`;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 5000);
}
showSuccess(message) {
// Create a simple success notification
const notification = document.createElement('div');
notification.className = 'notification success';
notification.innerHTML = `<i class="fas fa-check-circle"></i> ${message}`;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 3000);
}
}
// Initialize the application when the page loads
document.addEventListener('DOMContentLoaded', () => {
new DigitalSigner();
});