Search code examples
node.jspasswords

How to resolve this "TypeError: pchstr must contain a $ as first char" from node-argon2?


I got that TypeError when I'm trying to verify the password. The hash function works fine, but the verify not! this is the hash result: $argon2i$v=19$m=4096,t=3,p=1$8rKV3QWX0Y8GQ7ChOgVIRw$u+UEaGhG8Rvge4TvG17gnx/6jhdmePh9s7V3aK/asXA this verify function I use:

function comparePassword(candidatePassword){
    try {
        return argon2.verify candidatePassword, user.password
    }
    catch (err) {
        throw err
    }
}

And this is the hash function:

try {
    hash = await argon2.hash(password)
    user.password = hash
    next()
}
catch (err) {
    next(err)
}

My enviremt

Archlinux GNOME version 3.34.3
NodeJS v11.15.0
Argon2 v0.25.0


Solution

  • The error message gives you a hint: pchstr must contain a $ as first char. Every encoded representation of a hash generated by argon2 has its first part to be the variant of Argon2 being used, which in your case is argon2i. Your candidatePassword likely does not have '$' as its first character, and even if it did, you will have other problems. This is a start though!

    If you pay attention to the code example in npm package more closely, you will notice it is

    argon2.verify("<big long hash>", "password") and not

    argon2.verify("password","<big long hash>") as you have done.

    TLDR: Swap your arguments to match the parameters for argon2.verify.