Skip to content

Commit

Permalink
SecureSerializer class for conveniant use.
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuo committed Dec 29, 2010
1 parent d3c66a3 commit 31b4c05
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/nodetk/serializer.js
Expand Up @@ -9,7 +9,8 @@ var crypto = require('crypto')
;


exports.dump_str = function(obj) {
var SERIALIZER = exports;
SERIALIZER.dump_str = function(obj) {
/* Returns dump of the given JSON obj as a str.
* There is no encryption, and it might not be safe.
* Might throw an error.
Expand All @@ -22,7 +23,7 @@ exports.dump_str = function(obj) {
};


exports.load_str = function(str) {
SERIALIZER.load_str = function(str) {
/* Returns obj loaded from given string.
* Might throw an error.
*
Expand All @@ -46,7 +47,7 @@ var CYPHER = 'aes256';
var CODE_ENCODING = "hex";
var DATA_ENCODING = "utf8";

exports.dump_secure_str = function(obj, encrypt_key, validate_key) {
SERIALIZER.dump_secure_str = function(obj, encrypt_key, validate_key) {
/* Return str representing the given obj. It is signed and encrypted using the
* given keys.
*/
Expand All @@ -63,7 +64,7 @@ exports.dump_secure_str = function(obj, encrypt_key, validate_key) {
return digest + nonce_crypt + res;
};

exports.load_secure_str = function(str, encrypt_key, validate_key) {
SERIALIZER.load_secure_str = function(str, encrypt_key, validate_key) {
/* Given a string resulting from dump_secure_str, load corresponding JSON.
*/
var expected_digest = str.substring(0, 28);
Expand All @@ -79,3 +80,19 @@ exports.load_secure_str = function(str, encrypt_key, validate_key) {
return JSON.parse(data);
};


SERIALIZER.SecureSerializer = function(encrypt_key, validate_key) {
/* Class to store encryption/validation keys in a more convenient way. */
this.encrypt_key = encrypt_key;
this.validate_key = validate_key;
};
SERIALIZER.SecureSerializer.prototype = {
dump_str: function(obj) {
return SERIALIZER.dump_secure_str(obj, this.encrypt_key, this.validate_key);
}
, load_str: function(str) {
return SERIALIZER.load_secure_str(str, this.encrypt_key, this.validate_key);
}
};


46 changes: 44 additions & 2 deletions src/nodetk/tests/test_serializer.js
Expand Up @@ -22,20 +22,36 @@ var VALS = [
];


var dont_call = function() {
assert.ok(false, "must no be called");
};

var to_restore = ['dump_secure_str', 'load_secure_str'];
var originals = {};
to_restore.forEach(function(original) {
originals[original] = serializer[original];
});
exports.module_clode = function() {
to_restore.forEach(function(original) {
serializer[original] = originals[original];
});
};


exports.tests = [

['dump_str & laod_str', VALS.length * 2, function() {
VALS.forEach(function(val) {
var dump = serializer.dump_str(val);
var original = serializer.load_str(dump);
assert.deepEqual(original, val);
// Algorithm determinist:
// Algorithm must be determinist:
var dump2 = serializer.dump_str(val);
assert.equal(dump, dump2);
});
}],

['dump_secure_str', VALS.length * 2, function() {
['dump_secure_str load_secure_str', VALS.length * 2, function() {
var encrypt_key = 'somesecretkey';
var validate_key = 'anothersecretstring';
VALS.forEach(function(val) {
Expand All @@ -48,5 +64,31 @@ exports.tests = [
});
}],

['SecureSerializer.dump_str', 3, function() {
var ss = new serializer.SecureSerializer('crypt', 'valid');
var expected_obj = {}
serializer.load_secure_str = dont_call;
serializer.dump_secure_str = function(obj, ck, vk) {
assert.equal(obj, expected_obj);
assert.equal(ck, 'crypt');
assert.equal(vk, 'valid');
serializer.dump_secure_str = dont_call;
};
ss.dump_str(expected_obj)
}],

['SecureSerializer.load_str', 3, function() {
var ss = new serializer.SecureSerializer('crypt', 'valid');
var expected_str = "secret"
serializer.dump_secure_str = dont_call;
serializer.load_secure_str = function(str, ck, vk) {
assert.equal(str, expected_str);
assert.equal(ck, 'crypt');
assert.equal(vk, 'valid');
serializer.load_secure_str = dont_call;
};
ss.load_str(expected_str)
}],

];

0 comments on commit 31b4c05

Please sign in to comment.