Skip to content

Commit f99f385

Browse files
benjreinhartthijs
authored and
thijs
committed
Remove early return for non commonjs environments (brianc#3033)
1 parent 49f55d3 commit f99f385

File tree

2 files changed

+85
-85
lines changed

2 files changed

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

packages/pg/lib/crypto/utils.js

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

0 commit comments

Comments
 (0)