-
-
Notifications
You must be signed in to change notification settings - Fork 98
Description
According to your #469 issue, this library will be more streamlined towards password hashing moving forward.
The current API is pretty similar to the PHP password functions, with the hash, verify and needsRehash functions.
I would like to propose a new API combining the verify and needsRehash functions to increase the adoption of password re-hashing when parameters change or a new version is released. These could then be the recommended methods for developers who just want to hash passwords and use the defaults provided by the library.
My proposal is something like the ASP.NET Core Identity PasswordHasher.VerifyHashedPassword method:
// Always uses the default options
async hashPassword(password: string): string {
return await argon2.hash(password);
}
async verifyPassword(digest: string, password: string): "success" | "success-rehash-needed" | false {
const result = await argon2.verify(digest, password);
if (!result) {
return false;
}
// Always compares against the default options
if (argon2.needsRehash(digest)) {
return "success-needs-rehash";
}
return "success";
}Returning false instead of something like fail (like the .NET implementation) when verification does not succeed ensures that code checking for truthy values won't bypass verification:
const verified = await verifyPassword("...");
if (verified) {
// success
} else {
// failed
}I'm not sure if this only introduces clutter, but a quick search on github seems to reveal that needsRehash is not widely used:
argon2.verify language:TypeScript : 10.3k results
argon2.verify language:JavaScript: 5.4k results
argon2.needsRehash language:TypeScript: 59 results
argon2.needsRehash language:JavaScript: 23 results