forked from SuprDewd/uva-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptoUtil.js
75 lines (62 loc) · 1.96 KB
/
cryptoUtil.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
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const util = require('./util');
const KEY_FILE_NAME = "uva-node.key";
const SSH_PATH = path.join(util.getUserHomePath(), ".ssh");
const KEY_PATH = path.join(SSH_PATH, KEY_FILE_NAME);
/** Key strength in bits. */
const KEY_STRENGTH = 128;
const ALGO = 'AES'+KEY_STRENGTH;
/**
* Creates, saves and returns a random password key.
* @return Buffer holding the password bytes.
*/
function createKey()
{
var buf = crypto.randomBytes(KEY_STRENGTH / 8);
var hex = buf.toString('hex');
var opts = {
encoding: 'ascii', // encoding cannot be hex
mode: 0600, // only user R/W
flag: 'w' // create if not exist, truncate otherwise
};
if (! fs.existsSync(SSH_PATH))
fs.mkdirSync(SSH_PATH, 0644); // user R/W, group & other R
fs.writeFileSync(KEY_PATH, hex, opts);
return buf;
}
/**
* Gets or creates the password key if necessary.
* @return Buffer holding the password bytes.
*/
function getKey()
{
var keyExists = fs.existsSync(KEY_PATH);
if (!keyExists) return createKey();
var contents = fs.readFileSync(KEY_PATH, {encoding: 'ascii'});
return new Buffer(contents.trim(), 'hex');
}
const KEY_BUF = getKey();
module.exports = {
/**
* Generates a random IV and encrypts a buffer.
* @param dataBuf Data buffer to encrypt.
* @return An object with the fields: iv and buf, both of which are buffers.
*/
encrypt: function(dataBuf){
var iv = crypto.randomBytes(KEY_STRENGTH/8);
var cipher = crypto.createCipheriv(ALGO, KEY_BUF, iv);
cipher.end(dataBuf);
return {buf: cipher.read(), iv: iv};
},
/**
* Decrypts a data buffer.
* @return Buffer holding the decrypted data.
*/
decrypt: function(dataBuf, iv){
var cipher = crypto.createDecipheriv(ALGO, KEY_BUF, iv);
cipher.end(dataBuf);
return cipher.read();
}
};