@@ -15,75 +15,13 @@ const webCrypto = nodeCrypto.webcrypto || globalThis.crypto
15
15
const subtleCrypto = webCrypto . subtle
16
16
const textEncoder = new TextEncoder ( )
17
17
18
- /**
19
- *
20
- * @param {* } length
21
- * @returns
22
- */
23
- function randomBytes ( length ) {
24
- return webCrypto . getRandomValues ( Buffer . alloc ( length ) )
25
- }
26
-
27
- async function md5 ( string ) {
28
- try {
29
- return nodeCrypto . createHash ( 'md5' ) . update ( string , 'utf-8' ) . digest ( 'hex' )
30
- } catch ( e ) {
31
- // `createHash()` failed so we are probably not in Node.js, use the WebCrypto API instead.
32
- // Note that the MD5 algorithm on WebCrypto is not available in Node.js.
33
- // This is why we cannot just use WebCrypto in all environments.
34
- const data = typeof string === 'string' ? textEncoder . encode ( string ) : string
35
- const hash = await subtleCrypto . digest ( 'MD5' , data )
36
- return Array . from ( new Uint8Array ( hash ) )
37
- . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) )
38
- . join ( '' )
39
- }
40
- }
41
-
42
- // See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
43
- async function postgresMd5PasswordHash ( user , password , salt ) {
44
- const inner = await md5 ( password + user ) ;
45
- const outer = await md5 ( Buffer . concat ( [ Buffer . from ( inner ) , salt ] ) ) ;
46
- return `md5${ outer } `
47
- }
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
- /**
58
- * Sign the message with the given key
59
- * @param {ArrayBuffer } keyBuffer
60
- * @param {string } msg
61
- */
62
- async function hmacSha256 ( keyBuffer , msg ) {
63
- const key = await subtleCrypto . importKey ( 'raw' , keyBuffer , { name : 'HMAC' , hash : 'SHA-256' } , false , [ 'sign' ] )
64
- return await subtleCrypto . sign ( 'HMAC' , key , textEncoder . encode ( msg ) )
65
- }
66
-
67
- /**
68
- * Derive a key from the password and salt
69
- * @param {string } password
70
- * @param {Uint8Array } salt
71
- * @param {number } iterations
72
- */
73
- async function deriveKey ( password , salt , iterations ) {
74
- const key = await subtleCrypto . importKey ( 'raw' , textEncoder . encode ( password ) , 'PBKDF2' , false , [ 'deriveBits' ] )
75
- const params = { name : 'PBKDF2' , hash : 'SHA-256' , salt : salt , iterations : iterations }
76
- return await subtleCrypto . deriveBits ( params , key , 32 * 8 , [ 'deriveBits' ] )
77
- }
78
-
79
18
function concatenateBuffers ( buffer1 , buffer2 ) {
80
19
const combined = new Uint8Array ( buffer1 . length + buffer2 . length ) ;
81
20
combined . set ( new Uint8Array ( buffer1 ) , 0 ) ;
82
21
combined . set ( new Uint8Array ( buffer2 ) , buffer1 . length ) ;
83
22
return combined ;
84
23
}
85
24
86
-
87
25
async function sha1 ( msg , msg1 , msg2 ) {
88
26
let concatenatedData = typeof msg === 'string' ? textEncoder . encode ( msg ) : msg ;
89
27
if ( msg1 ) {
@@ -97,11 +35,5 @@ async function sha1(msg,msg1,msg2) {
97
35
}
98
36
99
37
module . exports = {
100
- postgresMd5PasswordHash,
101
- randomBytes,
102
- deriveKey,
103
- sha256,
104
- hmacSha256,
105
- md5,
106
- sha1,
38
+ sha1
107
39
}
0 commit comments