-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.js
42 lines (32 loc) · 1.16 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
const {getFile} = require('./fsWatcher');
let crypto;
let os;
const getCrypto = () => {
if (!crypto) {
crypto = require("crypto");
}
return crypto;
};
const getOS = () => os ? os : os = require('os');
const userInfo = () => getOS().userInfo();
const homedir = () => getOS().homedir();
const getSshKeyPath = (sshKeyName) => {
const homeDir = homedir();
const key = sshKeyName || 'id_rsa';
return `${homeDir}/.ssh/${key}`;
};
const getAuthorizedKeys = () => `${homedir()}/.ssh/authorized_keys`;
const getPrivateKey = async (sshKeyPath) => {
const file = await getFile(sshKeyPath);
return file.toString();
};
const getPublicKey = async (sshKeyPath) => {
const file = await getFile(`${sshKeyPath}.pub`);
return file.toString();
};
const publicDecrypt = (publicKey, signature) => getCrypto().publicDecrypt(publicKey, Buffer.from(signature, 'base64')).toString();
const privateEncrypt = (privateKey, data) => getCrypto().privateEncrypt(privateKey, Buffer.from(data)).toString("base64");
module.exports = {
publicDecrypt, getCrypto, userInfo, homedir, getSshKeyPath, getAuthorizedKeys, privateEncrypt,
getPrivateKey, getPublicKey
};