-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocker.js
70 lines (59 loc) · 1.66 KB
/
locker.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
console.log('I am a locker');
const _crypto = require('cryptojs');
const crypto = _crypto.Crypto;
const AWS = require('aws-sdk');
let _passphrase;
const init = (passphrase) => {
_passphrase = passphrase;
};
const set = (key, value) => new Promise((resolve, reject) => {
console.log("want to set key " + key + " with value " + value);
const encrypted = crypto.AES.encrypt(value, _passphrase);
console.log(encrypted);
const b64encrypted = Buffer.from(encrypted).toString('base64');
const docClient = new AWS.DynamoDB.DocumentClient({
region: 'us-west-2'
});
const tableName = 'hg_locker';
const params = {
TableName: tableName,
Item: {
'user_id': 'joseph',
'key': 'testkey',
'value': b64encrypted
}
};
docClient.put(params, (err, data) => {
console.log(err);
console.log(data);
resolve();
});
});
const get = (key) => new Promise((resolve, reject) => {
console.log("want to get key " + key);
const docClient = new AWS.DynamoDB.DocumentClient({
region: 'us-west-2'
});
const tableName = 'hg_locker';
const params = {
TableName: tableName,
Key: {
'user_id': 'joseph',
'key': 'testkey'
}
};
docClient.get(params, (err, data) => {
console.log(err);
console.log(data);
const b64 = data.Item.value;
const ascii = Buffer.from(b64, 'base64').toString('ascii');
console.log(ascii);
const decrypted = crypto.AES.decrypt(ascii, _passphrase);
resolve(decrypted);
});
});
module.exports = {
init,
set,
get
};