-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.js
More file actions
46 lines (38 loc) · 1.4 KB
/
assignment.js
File metadata and controls
46 lines (38 loc) · 1.4 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
const crypto = require('crypto');
// Personal information
const firstName = "Cletus";
const lastName = "Gizzo";
// 1. Create a Private Key and derive Public Key
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
});
// 2. Generate an Address from the Public Key (simple hash of public key)
const publicKeyPem = publicKey.export({ type: 'pkcs1', format: 'pem' });
const address = crypto.createHash('sha256')
.update(publicKeyPem)
.digest('hex')
.slice(0, 40);
// 3. Create a Message
const message = `My name is ${firstName} ${lastName}`;
// 4. Hash the message
const messageHash = crypto.createHash('sha256')
.update(message)
.digest('hex');
// 5. Create Digital Signature
const signer = crypto.createSign('sha256');
signer.update(message);
signer.end();
const signature = signer.sign(privateKey, 'hex');
// 6. Verify the signature
const verifier = crypto.createVerify('sha256');
verifier.update(message);
verifier.end();
const isVerified = verifier.verify(publicKey, signature, 'hex');
// 7. Display results
console.log('Private Key:', privateKey.export({ type: 'pkcs1', format: 'pem' }));
console.log('Public Key:', publicKeyPem);
console.log('Address:', address);
console.log('Message:', message);
console.log('Message Hash:', messageHash);
console.log('Digital Signature:', signature);
console.log('Signature Verification:', isVerified ? 'Valid' : 'Invalid');