From 2f9316fbcbb8632915157548f69c0a480a781ab2 Mon Sep 17 00:00:00 2001 From: Ethan <6400075+ETHANTALJAFFE@users.noreply.github.com> Date: Wed, 14 Aug 2024 17:23:00 +0300 Subject: [PATCH 1/2] removed import and usage of node:crypto --- packages/pg/lib/crypto/utils-webcrypto.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/pg/lib/crypto/utils-webcrypto.js b/packages/pg/lib/crypto/utils-webcrypto.js index 0433f010c..d03f08c3b 100644 --- a/packages/pg/lib/crypto/utils-webcrypto.js +++ b/packages/pg/lib/crypto/utils-webcrypto.js @@ -1,5 +1,3 @@ -const nodeCrypto = require('crypto') - module.exports = { postgresMd5PasswordHash, randomBytes, @@ -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 @@ -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 From 4cdde759ca9fe09a362c7a03a213dce2e7bd559b Mon Sep 17 00:00:00 2001 From: Ethan <6400075+ETHANTALJAFFE@users.noreply.github.com> Date: Sun, 18 Aug 2024 13:28:42 +0300 Subject: [PATCH 2/2] Updated node version check for crypto lib from 15 to 22 --- packages/pg/lib/crypto/utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/pg/lib/crypto/utils.js b/packages/pg/lib/crypto/utils.js index 9644b150f..83338ec8c 100644 --- a/packages/pg/lib/crypto/utils.js +++ b/packages/pg/lib/crypto/utils.js @@ -1,6 +1,5 @@ 'use strict' - -const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 15 +const useLegacyCrypto = parseInt(process.versions && process.versions.node && process.versions.node.split('.')[0]) < 22 if (useLegacyCrypto) { // We are on an old version of Node.js that requires legacy crypto utilities. module.exports = require('./utils-legacy')