Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

31 rader
1.3 KiB

  1. // Perhaps use hashy as reference to implement bcrypt and needRehash action
  2. // https://github.com/JsCommunity/hashy
  3. // supports argon2i, argon2d and argon2id
  4. exports.passwordHash = async function(options) {
  5. const password = this.parseRequired(options.password, 'string', 'crypto.passwordHash: password is required.')
  6. const algo = this.parseOptional(options.algo, 'string', 'argon2i')
  7. const argon2 = require('argon2')
  8. const type = argon2[algo]
  9. return argon2.hash(password, { type })
  10. }
  11. exports.passwordVerify = async function(options) {
  12. const password = this.parseRequired(options.password, 'string', 'crypto.passwordVerify: password is required.')
  13. const hash = this.parseRequired(options.hash, 'string', 'crypto.passwordVerify: hash is required.')
  14. const argon2 = require('argon2')
  15. return argon2.verify(hash, password)
  16. }
  17. exports.passwordNeedsRehash = async function(options) {
  18. const hash = this.parseRequired(options.hash, 'string', 'crypto.passwordNeedsRehash: hash is required.')
  19. const algo = this.parseOptional(options.algo, 'string', 'argon2i')
  20. const argon2 = require('argon2')
  21. return argon2.needsRehash(hash, {})
  22. }
  23. exports.uuid = async function(options) {
  24. const { randomUUID } = require('crypto')
  25. const { v4: uuidv4 } = require('uuid')
  26. return randomUUID ? randomUUID() : uuidv4()
  27. }