-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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