forked from awslabs/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testKmsCrypto.js
123 lines (106 loc) · 3.02 KB
/
testKmsCrypto.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
var kmsCrypto = require('./kmsCrypto');
kmsCrypto.setRegion("eu-west-1");
var testSimple = function(callback) {
console.log("Simple Two Way String Encrypt/Decrypt");
kmsCrypto.encrypt("Testing KMS Crypto", function(err, encryptedCiphertext) {
if (err) {
console.log(err);
} else {
console.log("Got Encrypted Value " + kmsCrypto.toLambdaStringFormat(encryptedCiphertext));
kmsCrypto.decrypt(encryptedCiphertext, function(err, plaintext) {
if (err) {
console.log(err);
} else {
console.log("Simple Two Way Encryption Result: " + plaintext.toString());
if (callback) {
callback();
}
}
});
}
});
};
// blocking array based encryption
var testMap = function(callback) {
console.log("Test Map Encryption");
var plaintext = {
"a" : "value 1",
"b" : "value 2"
};
// blocking encryption
kmsCrypto.encryptMap(plaintext, function(err, encryptedMap) {
if (err) {
console.log(JSON.stringify(err));
} else {
kmsCrypto.decryptMap(encryptedMap, function(err, decryptedMap) {
if (err) {
console.log(err);
} else {
Object.keys(decryptedMap).forEach(function(key) {
if (plaintext[key] !== decryptedMap[key].toString()) {
throw new Error("Incorrect Decryption of " + plaintext[key] + " with Key " + key + ": " + decryptedMap[key]);
} else {
console.log("Key: " + key + " Value: " + decryptedMap[key].toString());
}
});
}
});
}
});
// invoke the callback if needed
if (callback) {
callback();
}
};
var testArray = function(callback) {
console.log("Test Blocking Array Decryption");
var plaintext = [ "value 1", "value 2" ];
// blocking encryption
kmsCrypto.encryptAll(plaintext, function(err, encryptedArray) {
// blocking decryption
console.log("Blocking Decrypt of " + encryptedArray.length + " values");
kmsCrypto.decryptAll(encryptedArray, function(err, decrypted) {
if (err) {
console.log(err);
} else {
for (var i = 0; i < decrypted.length; i++) {
if (decrypted[i]) {
console.log("Blocking Decryption Value " + i + ": " + decrypted[i].toString());
} else {
console.log("Value " + i + " returned undefined");
}
}
}
});
});
// invoke the callback if needed
if (callback) {
callback();
}
};
var testSeralisation = function(callback) {
console.log("Serialised Two Way Encrypt/Decrypt");
kmsCrypto.encrypt("Testing KMS Crypto", function(err, encryptedCiphertext) {
if (err) {
console.log(err);
} else {
// turn the encrypted Ciphertext into a String
var stringValue = JSON.stringify(encryptedCiphertext);
// turn the Ciphertext back into a buffer
var ciphertextBuffer = new Buffer(JSON.parse(stringValue));
kmsCrypto.decrypt(ciphertextBuffer, function(err, plaintext) {
if (err) {
console.log(err);
} else {
console.log("Simple Two Way Encryption Result: " + plaintext.toString());
if (callback) {
callback();
}
}
});
}
});
};
// run the tests
// testSimple(testArray(testSeralisation()));
testMap(function() {});