bcrypt confronta altre due stringhe e restituisce true

Nov 15 2020

Sto programmando un token di aggiornamento jwt in Node.js. Dopo il login, li sottopone ad hashing e li salva nel database.

  1. Primo gettone:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1dWlkIjoiOWRiMTcwMmMtNDkyYy00MTJmLTkyM2QtM2Y5MWQyYzk0NTNjIiwicm9sZSI6IkNVU1RPTUVSX1JPTEUiLCJpYXQiOjE2MDU0NjA4NTksImV4cCI6MTYwNTQ2NDQ1OX0.p6LTPotLugZ-JWp4Xmt4chwWxtqqZRXuXDBhDdqLsLI

  1. Token Secound:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1dWlkIjoiOWRiMTcwMmMtNDkyYy00MTJmLTkyM2QtM2Y5MWQyYzk0NTNjIiwicm9sZSI6IkNVU1RPTUVSX1JPTEUiLCJpYXQiOjE2MDU0NjEzMjIsImV4cCI6MTYwNTQ2NDkyMn0.qMzg99DE1DvDjoMXeK08l12SlfbQd7GUSHEF5X8Mt4w

Hash Bcrypt:

$2b$10 $ RTZbH4UONRaBnam.oALmhuIs / veX.h7e3CeMlmQjk9GVC4kKPGsBO

I due token sono diversi e bcrypt mostra, in due casi, che corrispondono all'hash codificato.

Ho anche eseguito il debug di questo tramite il sito e la stessa cosa - entrambe le stringhe diverse mostrano che corrispondono all'hash - perché?

Questi sono i miei metodi di hashing:

  /**
   * generate hash from password or string
   * @param {string} password
   * @returns {Promise<string>}
   */
  static async generateHash(password: string): Promise<string> {
    return bcrypt.hash(password, 10);
  }

  /**
   * validate text with hash
   * @param {string} password
   * @param {string} hash
   * @returns {Promise<boolean>}
   */
  static async validateHash(password: string, hash?: string): Promise<boolean> {
    return bcrypt.compare(password, hash || '');
  }

Risposte

1 Bravo Nov 16 2020 at 00:38

bcrypt è limitato a 72 caratteri "password"

Le tue "password" sono lunghe 220 caratteri e le prime 147 sono identiche (poiché è solo l'intestazione e la prima parte del contenuto del JWT che è identica in entrambi i casi)

Quindi, il tuo codice "hashing" solo i primi 72 caratteri identici