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

400
Visualizações
How to verify long passwords using bcrypt?

So I came across this issue and couldn't figure out what was causing it. I have implemented an authentication flow using JWT with access- and refresh tokens. I made the refresh tokens expire after a long period of time, and they can be forcefully reset to prevent stolen refresh tokens from being used again.

To do this, I hash the jwt refresh token using bcrypt and store it in my database. However I always get a true result when comparing the raw jwt to the hash and I'm not sure why.

I searched for some bit and I think this is happening because bcrypt doesn't like the 191 char tokens, and trims them to a max length, and since the first part of the jwt is similar, I get a valid result when comparing the hash. Is this true? If so, how can I extend the max length to fit my tokens or should I pre-hash the tokens before passing them to the original hash function?

await bcrypt.compare('loooooooooooooooooooooooooooooooooooooooooooooooooooooooong', hash);
// true

await bcrypt.compare('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong', hash);
// also true

Any help is appreciated :)

Edit:

Okay so I've been thinking about this for a bit and think I've come up with an okay solution.

When signing the refresh token, I also generate a random password and store it in the payload. Instead of storing the hash of the jwt itself, I store the hash of this password in my database. So whenever I want to verify the refresh token hasn't been blocked, I first verify the token itself, and then verify the password in the payload to my stored hash. If the refresh token has already been updated the password in the payload will no longer match the hash from the newly signed refreh token, thus making it invalid.

Opinions from future readers?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

and trims them to a max length, and since the first part of the jwt is similar, I get a valid result when comparing the hash. Is this true?

Yes that true.

There is no way to "extend the max length" of the algorithm. bcrypt has a maximum password length. Depend on the implementation of the algorithm, the actual limit might be a little different. If you really want to use more than that limit to hash the password, you will have to look for a different encryption algorithm, which may or may not be better than bcrypt even with a higher character limit.

over 4 years ago · Santiago Trujillo Relatório
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