@@ -4,89 +4,6 @@ const useLegacyCrypto = parseInt(process.versions && process.versions.node && pr
4
4
if ( useLegacyCrypto ) {
5
5
// We are on an old version of Node.js that requires legacy crypto utilities.
6
6
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' ) ;
92
9
}
0 commit comments