Skip to content

Commit 8419ea7

Browse files
committed
test in progress
1 parent f83c9fe commit 8419ea7

File tree

4 files changed

+44
-49
lines changed

4 files changed

+44
-49
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Then make your script connected. Example:
2424

2525
var crypter = require("./basic256.js");
2626
27-
var blob = crypter.enc.run("FOO"); // This encrypts the string "FOO".
27+
var blob = crypter.encrypt("FOO").catch(console.error); // This encrypts the string "FOO".
2828
console.log(blob); // This will show the encrypted string.
2929
30-
var unblob = crypter.dec.run(blob); // This decrypts the encrypted string.
30+
var unblob = crypter.decrypt(blob).catch(console.error); // This decrypts the encrypted string.
3131
console.log(unblob); // This will show the decrypted string. (Which in this case, it is "FOO")

basic256.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
let crypto = require('crypto');
44

5-
function UserException(message) {
6-
this.message = message;
7-
this.name = 'UserException';
8-
}
9-
105
try {
116
var savedKeys = require("./config.js").k;
127
} catch (e) {
13-
throw new UserException('No Configuration Exists');
8+
Promise.reject('No Configuration Exists!');
149
}
1510

1611
var ALGORITHM, KEY, HMAC_ALGORITHM, HMAC_KEY;
@@ -43,48 +38,47 @@ var constant_time_compare = function (val1, val2) {
4338

4439
module.exports = {
4540

46-
"enc": {
47-
run : function (plain_text) {
41+
"encrypt": function (plain_text) {
42+
if (!plain_text || typeof(plain_text) !== "string") Promise.reject("Plain text not found.");
4843

49-
var IV = Buffer.from(randomValueHex(16)); // ensure that the IV (initialization vector) is random
50-
var encryptor, cipher_text, hmac;
44+
var IV = Buffer.from(randomValueHex(16)); // ensure that the IV (initialization vector) is random
45+
var encryptor, cipher_text, hmac;
5146

52-
encryptor = crypto.createCipheriv(ALGORITHM, KEY, IV);
53-
encryptor.setEncoding('hex');
54-
encryptor.write(plain_text);
55-
encryptor.end();
47+
encryptor = crypto.createCipheriv(ALGORITHM, KEY, IV);
48+
encryptor.setEncoding('hex');
49+
encryptor.write(plain_text);
50+
encryptor.end();
5651

57-
cipher_text = encryptor.read();
52+
cipher_text = encryptor.read();
5853

59-
hmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY);
60-
hmac.update(cipher_text);
61-
hmac.update(IV.toString('hex')); // ensure that both the IV and the cipher-text is protected by the HMAC
54+
hmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY);
55+
hmac.update(cipher_text);
56+
hmac.update(IV.toString('hex')); // ensure that both the IV and the cipher-text is protected by the HMAC
6257

63-
// The IV isn't a secret so it can be stored along side everything else
64-
return cipher_text + "$" + IV.toString('hex') + "$" + hmac.digest('hex')
65-
}
58+
// The IV isn't a secret so it can be stored along side everything else
59+
return cipher_text + "$" + IV.toString('hex') + "$" + hmac.digest('hex')
6660
},
6761

68-
"dec": {
69-
run : function (cipher_text) {
70-
var cipher_blob = cipher_text.split("$");
71-
var ct = cipher_blob[0];
72-
var IV = Buffer.from(cipher_blob[1], 'hex');
73-
var hmac = cipher_blob[2];
74-
var chmac, decryptor;
75-
76-
chmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY);
77-
chmac.update(ct);
78-
chmac.update(IV.toString('hex'));
79-
80-
if (!constant_time_compare(chmac.digest('hex'), hmac)) {
81-
Promise.reject("Encrypted Blob has been tampered with...");
82-
return null;
83-
}
84-
85-
decryptor = crypto.createDecipheriv(ALGORITHM, KEY, IV);
86-
var decryptedText = decryptor.update(ct, 'hex', 'utf-8');
87-
return decryptedText + decryptor.final('utf-8');
62+
"decrypt": function (cipher_text) {
63+
if (!cipher_text || typeof(cipher_text) !== "string" || !cipher_text.match("$")) Promise.reject("A valid cipher text not found.");
64+
65+
var cipher_blob = cipher_text.split("$");
66+
var ct = cipher_blob[0];
67+
var IV = Buffer.from(cipher_blob[1], 'hex');
68+
var hmac = cipher_blob[2];
69+
var chmac, decryptor;
70+
71+
chmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY);
72+
chmac.update(ct);
73+
chmac.update(IV.toString('hex'));
74+
75+
if (!constant_time_compare(chmac.digest('hex'), hmac)) {
76+
Promise.reject("Encrypted Blob has been tampered with.");
8877
}
78+
79+
decryptor = crypto.createDecipheriv(ALGORITHM, KEY, IV);
80+
var decryptedText = decryptor.update(ct, 'hex', 'utf-8');
81+
return decryptedText + decryptor.final('utf-8');
8982
}
83+
9084
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"scripts": {
1616
"install": "node ./DontRunMe.js",
1717
"test": "node ./test.js"
18-
}
18+
},
19+
"main": "./basic256"
1920
}

test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
var m = require("./basic256");
44

55
console.log("Encrypting string \"foo\"...");
6-
var encStr = m.enc.run("foo");
6+
var encStr = m.encrypt("foo");
77
console.log("\nDecrypting the string below...\n" + encStr);
8-
var decStr = m.dec.run(encStr);
9-
console.log("\n\nResult: " + decStr);
8+
var decStr = m.decrypt(encStr);
9+
console.log("\nResult: " + decStr);
1010

1111
if (decStr === "foo") {
1212
console.log("\nSUCCESS!");
13-
setTimeout(function(){process.exit(0);},853);
13+
setTimeout(() => {process.exit(0);},853);
1414
} else {
1515
console.error("\nFAILURE!");
16-
setTimeout(function(){process.exit(1);},853);
16+
setTimeout(() => {process.exit(1);},853);
1717
}

0 commit comments

Comments
 (0)