Skip to content

Commit

Permalink
removed import and usage of node:crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
ETHANTALJAFFE committed Aug 14, 2024
1 parent 54eb0fa commit 2f9316f
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions packages/pg/lib/crypto/utils-webcrypto.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const nodeCrypto = require('crypto')

module.exports = {
postgresMd5PasswordHash,
randomBytes,
Expand All @@ -13,7 +11,7 @@ module.exports = {
* The Web Crypto API - grabbed from the Node.js library or the global
* @type Crypto
*/
const webCrypto = nodeCrypto.webcrypto || globalThis.crypto
const webCrypto = globalThis.crypto ?? require('node:crypto').webcrypto
/**
* The SubtleCrypto API for low level crypto operations.
* @type SubtleCrypto
Expand All @@ -31,18 +29,11 @@ function randomBytes(length) {
}

async function md5(string) {
try {
return nodeCrypto.createHash('md5').update(string, 'utf-8').digest('hex')
} catch (e) {
// `createHash()` failed so we are probably not in Node.js, use the WebCrypto API instead.
// Note that the MD5 algorithm on WebCrypto is not available in Node.js.
// This is why we cannot just use WebCrypto in all environments.
const data = typeof string === 'string' ? textEncoder.encode(string) : string
const hash = await subtleCrypto.digest('MD5', data)
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
const data = typeof string === 'string' ? textEncoder.encode(string) : string
const hash = await subtleCrypto.digest('MD5', data)
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}

// See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
Expand Down

0 comments on commit 2f9316f

Please sign in to comment.