fork download
  1. const crypto = require('crypto');
  2.  
  3.  
  4. function generateSalt(length) {
  5. return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
  6. }
  7.  
  8. function hash(password, salt) {
  9. const hash = crypto.createHmac('sha256', salt);
  10. hash.update(password);
  11. const hashedPassword = hash.digest('hex');
  12. return salt + hashedPassword;
  13. }
  14.  
  15. function compare(password, hashed) {
  16. const salt = hashed.substr(0, 16);
  17. const hashedPassword = hash(password, salt);
  18. return hashed === hashedPassword;
  19. }
  20.  
Success #stdin #stdout 0.09s 32944KB
stdin
Standard input is empty
stdout
Standard output is empty