Skip to content

Instantly share code, notes, and snippets.

@designeng
Forked from ryasmi/passwordSealer.js
Created October 5, 2015 17:43
Show Gist options
  • Save designeng/5751527d2ebc8028ffc7 to your computer and use it in GitHub Desktop.
Save designeng/5751527d2ebc8028ffc7 to your computer and use it in GitHub Desktop.
An example of using Douglas Crockford's JavaScript Sealer/Unsealer method for securely authenticating users. http://www.yuiblog.com/blog/2010/02/24/video-crockonjs-3/
var makeSealer = function () {
var users = [], passwords = [];
return {
sealer: function (username, password) {
var i = users.length,
user = {username: username};
users[i] = user;
passwords[i] = password;
return user;
},
unsealer: function (user, password) {
return passwords[users.indexOf(user)] === password;
}
};
}
// Testing code.
var testUnsealer = function () {
var testUnsealerValue = function (user, password, value, id) {
console.log(ms.unsealer(user, password) === value ? "Test " + id + " passed." : "Test " + id + " failed.");
};
var ms = makeSealer();
var adminUser = ms.sealer("Admin", "MyAdminPassword");
testUnsealerValue(adminUser, "MyAdminPassw0rd", false, 1);
testUnsealerValue(adminUser, "MyAdminPassword", true, 2);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment