utils-legacy.js 1022 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. // This file contains crypto utility functions for versions of Node.js < 15.0.0,
  3. // which does not support the WebCrypto.subtle API.
  4. const nodeCrypto = require('crypto')
  5. function md5(string) {
  6. return nodeCrypto.createHash('md5').update(string, 'utf-8').digest('hex')
  7. }
  8. // See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
  9. function postgresMd5PasswordHash(user, password, salt) {
  10. var inner = md5(password + user)
  11. var outer = md5(Buffer.concat([Buffer.from(inner), salt]))
  12. return 'md5' + outer
  13. }
  14. function sha256(text) {
  15. return nodeCrypto.createHash('sha256').update(text).digest()
  16. }
  17. function hmacSha256(key, msg) {
  18. return nodeCrypto.createHmac('sha256', key).update(msg).digest()
  19. }
  20. async function deriveKey(password, salt, iterations) {
  21. return nodeCrypto.pbkdf2Sync(password, salt, iterations, 32, 'sha256')
  22. }
  23. module.exports = {
  24. postgresMd5PasswordHash,
  25. randomBytes: nodeCrypto.randomBytes,
  26. deriveKey,
  27. sha256,
  28. hmacSha256,
  29. md5,
  30. }