-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
109 lines (101 loc) · 3.34 KB
/
main.js
File metadata and controls
109 lines (101 loc) · 3.34 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
const { Command } = require('commander');
const hash = require('./src/hash');
const aes = require('./src/aes');
const rsa = require('./src/rsa');
const secp256k1 = require('./src/secp256k1');
const secp256r1 = require('./src/secp256r1');
const program = new Command();
program
.name('crypto-lib')
.description('Librería criptográfica CLI')
.version('1.0.0');
// Hash commands
program
.command('hash <type> <input>')
.description('Compute hash of data or file')
.action((type, input) => {
if (type === 'sha256') {
if (input.startsWith('file:')) {
console.log(hash.sha256File(input.slice(5)));
} else {
console.log(hash.sha256(input));
}
} else if (type === 'keccak256') {
if (input.startsWith('file:')) {
console.log(hash.keccak256File(input.slice(5)));
} else {
console.log(hash.keccak256(input));
}
}
});
// AES commands
program
.command('aes <action> <file> <key> [output]')
.description('Encrypt or decrypt file with AES')
.action((action, file, key, output) => {
const out = output || `${file}.${action === 'encrypt' ? 'enc' : 'dec'}`;
if (action === 'encrypt') {
aes.encryptFile(file, key, out);
console.log(`Encrypted file saved to ${out}`);
} else if (action === 'decrypt') {
aes.decryptFile(file, key, out);
console.log(`Decrypted file saved to ${out}`);
}
});
// RSA commands
program
.command('rsa <action> [args...]')
.description('RSA operations')
.action((action, args) => {
if (action === 'generate') {
const keys = rsa.generateKeys();
console.log('Public Key:', keys.publicKey);
console.log('Private Key:', keys.privateKey);
} else if (action === 'encrypt') {
const [data, pubKey] = args;
console.log(rsa.encrypt(data, pubKey));
} else if (action === 'decrypt') {
const [encrypted, privKey] = args;
console.log(rsa.decrypt(encrypted, privKey));
}
});
// secp256k1 commands
program
.command('secp256k1 <action> [args...]')
.description('secp256k1 operations')
.action((action, args) => {
if (action === 'generate') {
const keys = secp256k1.generateKeys();
console.log('Private Key:', keys.privateKey);
console.log('Public Key:', keys.publicKey);
} else if (action === 'sign') {
const [message, privKey] = args;
console.log(secp256k1.sign(message, privKey));
} else if (action === 'verify') {
const [signature, message, pubKey] = args;
console.log(secp256k1.verify(signature, message, pubKey));
} else if (action === 'address') {
const [pubKey] = args;
console.log(secp256k1.getAddress(pubKey));
}
});
// secp256r1 commands
program
.command('secp256r1 <action> [args...]')
.description('secp256r1 operations')
.action((action, args) => {
if (action === 'generate') {
const keys = secp256r1.generateKeys();
console.log('Private Key:', keys.privateKey);
console.log('Public Key:', keys.publicKey);
} else if (action === 'sign') {
const [message, privKey] = args;
const sig = secp256r1.sign(message, privKey);
console.log(JSON.stringify(sig));
} else if (action === 'verify') {
const [signature, message, pubKey] = args;
const sig = JSON.parse(signature);
console.log(secp256r1.verify(sig, message, pubKey));
}
});
program.parse();