Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
NodeJS + Bcrypt compare hash returning true with incorrect comparison string

I'm currently trying to implement a password reset service with NodeJS using Bcrypt. I've created a hash of an object like this:

const userData = {
    dateNow,
    userId: foundUser.rows[0].user_id,
    passwordHash: foundUser.rows[0].user_password,
    email: foundUser.rows[0].user_email
};

const userDataHash = await hashUserData(userData, 10);

Where userDataHash is:

const hashUserData = async (userData, saltRounds) => {
    let hashedData = await bcrypt.hash(JSON.stringify(userData), saltRounds);
    if (hashedData.includes('/') || hashedData.includes('.')) {
        hashedData = hashUserData(userData, saltRounds);
    }
    console.log('hashUserData', hashedData)
    return hashedData;
};

The hashing function is a bit hacky as because it forms part of the reset URL I don't want any hashes with / or .

The hash of the user data object is then added to a URL. When the user clicks the link and is taken to the password reset, when they submit the form the hash from the URL is sent along with the user ID of the account to reset, this ID is then used to create an object of the same data originally used to create the hash, so that if the password hash has already been changed then the hash will not match, stopping the person from updating their password.

My issue is that even when the password hash changes, the hash comparison returns true. If I change anything else in the object, the hash comparison returns false.

const compareHash =  async (toCompare, currentHash) => {
    return await bcrypt.compare(toCompare, currentHash)
};
const userDataToCheck = {
     dateNow: req.params.dateNow,
     userId,
     passwordHash: foundUser.rows[0].user_password,
     email: foundUser.rows[0].user_email
};
const hashesMatch = await compareHash(JSON.stringify(userDataToCheck), req.params.userDataHash)

Where req.params.userDataHash is the original hash using the original password hash in the object.

Any help appreciated!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda