-
Notifications
You must be signed in to change notification settings - Fork 24
Migrate from pbkdf2
Simone Primarosa edited this page Sep 7, 2017
·
5 revisions
Let's users
be your collection of registered users. Each of those users should
have a salt
, a secret
, a number of iterations
, a keylen
and a digest
algorithm stored in some manner. (It really depends on how you
have implemented it)
users.forEach(user => {
var hash = JSON.stringify({
hash: JSON.stringify({
salt: user.salt,
secret: user.secret, // Should be encoded in base64. If secret was saved with
// another base, convert it to base64.
iterations: user.iterations,
keylen: user.keylen,
digest: user.digest // 'sha1' or 'sha256' or 'sha512'
}),
func: 'pbkdf2'
});
user.hash = hash; // Now you can pass user.hash to the verify function of
// this package.
})
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null,
str.replace(/\r|\n/g, "")
.replace(/([\da-fA-F]{2}) ?/g, "0x$1 ")
.replace(/ +$/, "").split(" "))
);
}
function btoa(bin) {
var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var table = tableStr.split("");
for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
if ((a | b | c) > 255) throw new Error("String contains an invalid character");
base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
(isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
(isNaN(b + c) ? "=" : table[c & 63]);
}
return base64.join("");
}