-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.js
140 lines (121 loc) · 4.88 KB
/
security.js
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
const crypto = require("crypto")
const NodeRSA = require('node-rsa')
var sha256 = require('js-sha256')
// const sshpk = require('sshpk');
const window = require("window")
class CryptoSecurity{
constructor(){}
getKey(pass){
// crypto.generateKeyPair("rsa", {
// // The standard secure default length for RSA keys is 2048 bits
// modulusLength: 2048,
// },(err, publicKey, privateKey) => {
// if(!err) {
// console.log(typeof publicKey);
// }
// else {
// console.log(err);
// }
// } )
const {publicKey, privateKey} = crypto.generateKeyPairSync('rsa', {
modulusLength: 530, // options
publicExponent: 0x10101,
publicKeyEncoding: {type: 'pkcs1',format: 'der'},
privateKeyEncoding: {type: 'pkcs8',format: 'der', cipher: 'aes-192-cbc',passphrase: pass
}
});
return {privateKey, publicKey};
// crypto.generateKeyPair('rsa', {
// modulusLength: 530, // options
// publicExponent: 0x10101,
// publicKeyEncoding: {type: 'pkcs1',format: 'der'},
// privateKeyEncoding: {type: 'pkcs8',format: 'der',cipher: 'aes-192-cbc',passphrase: pass
// }
// }, async (err, publicKey, privateKey) => { // Callback function
// if(!err)
// {
// console.log("Private Key is: ", privateKey.toString('hex'));
// const privateJinish = await privateKey.toString('hex')
// return privateJinish;
// }
// else
// {
// // Prints error
// console.log("Errr is: ", err);
// }
// })
}
encryption(data,publicKey){
const encryptedData = crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
// We convert the data string to a buffer using `Buffer.from`
Buffer.from(data)
)
console.log("encypted data: ", encryptedData.toString("base64"))
return encryptedData
}
decryption(encryptedData,privatekey){
const decryptedData = crypto.privateDecrypt(
{
key: privatekey,
// In order to decrypt the data, we need to specify the
// same hashing function and padding scheme that we used to
// encrypt the data in the previous step
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
encryptedData
)
// The decrypted data is of the Buffer type, which we can convert to a
// string to reveal the original data
console.log("decrypted data: ", decryptedData.toString())
return decryptedData
}
signing(verifiableData,privateKey)
{
// The signature method takes the data we want to sign, the
// hashing algorithm, and the padding scheme, and generates
// a signature in the form of bytes
const signature = crypto.sign("sha256", Buffer.from(verifiableData), {
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
})
console.log(signature.toString("base64"))
return signature
}
verify(signature,verifiableData,publicKey)
{
const isVerified = crypto.verify("sha256", Buffer.from(verifiableData),
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
},
signature
)
console.log("signature verified: ", isVerified)
return isVerified
}
symmetricEncrypt(text,password) {
let key = crypto.scryptSync(password, 'salt', 24)
const iv = crypto.randomBytes(16)
let cipher = crypto.createCipheriv('aes-192-cbc', key, iv)
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
symmetricDecrypt(text,password) {
let key = crypto.scryptSync(password, 'salt', 24)
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-192-cbc', key, iv)
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
}
module.exports = {CryptoSecurity}