utils-webcrypto.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const nodeCrypto = require('crypto')
  2. module.exports = {
  3. postgresMd5PasswordHash,
  4. randomBytes,
  5. deriveKey,
  6. sha256,
  7. hmacSha256,
  8. md5,
  9. }
  10. /**
  11. * The Web Crypto API - grabbed from the Node.js library or the global
  12. * @type Crypto
  13. */
  14. const webCrypto = nodeCrypto.webcrypto || globalThis.crypto
  15. /**
  16. * The SubtleCrypto API for low level crypto operations.
  17. * @type SubtleCrypto
  18. */
  19. const subtleCrypto = webCrypto.subtle
  20. const textEncoder = new TextEncoder()
  21. /**
  22. *
  23. * @param {*} length
  24. * @returns
  25. */
  26. function randomBytes(length) {
  27. return webCrypto.getRandomValues(Buffer.alloc(length))
  28. }
  29. async function md5(string) {
  30. try {
  31. return nodeCrypto.createHash('md5').update(string, 'utf-8').digest('hex')
  32. } catch (e) {
  33. // `createHash()` failed so we are probably not in Node.js, use the WebCrypto API instead.
  34. // Note that the MD5 algorithm on WebCrypto is not available in Node.js.
  35. // This is why we cannot just use WebCrypto in all environments.
  36. const data = typeof string === 'string' ? textEncoder.encode(string) : string
  37. const hash = await subtleCrypto.digest('MD5', data)
  38. return Array.from(new Uint8Array(hash))
  39. .map((b) => b.toString(16).padStart(2, '0'))
  40. .join('')
  41. }
  42. }
  43. // See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
  44. async function postgresMd5PasswordHash(user, password, salt) {
  45. var inner = await md5(password + user)
  46. var outer = await md5(Buffer.concat([Buffer.from(inner), salt]))
  47. return 'md5' + outer
  48. }
  49. /**
  50. * Create a SHA-256 digest of the given data
  51. * @param {Buffer} data
  52. */
  53. async function sha256(text) {
  54. return await subtleCrypto.digest('SHA-256', text)
  55. }
  56. /**
  57. * Sign the message with the given key
  58. * @param {ArrayBuffer} keyBuffer
  59. * @param {string} msg
  60. */
  61. async function hmacSha256(keyBuffer, msg) {
  62. const key = await subtleCrypto.importKey('raw', keyBuffer, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'])
  63. return await subtleCrypto.sign('HMAC', key, textEncoder.encode(msg))
  64. }
  65. /**
  66. * Derive a key from the password and salt
  67. * @param {string} password
  68. * @param {Uint8Array} salt
  69. * @param {number} iterations
  70. */
  71. async function deriveKey(password, salt, iterations) {
  72. const key = await subtleCrypto.importKey('raw', textEncoder.encode(password), 'PBKDF2', false, ['deriveBits'])
  73. const params = { name: 'PBKDF2', hash: 'SHA-256', salt: salt, iterations: iterations }
  74. return await subtleCrypto.deriveBits(params, key, 32 * 8, ['deriveBits'])
  75. }