Skip to content

Commit 09e6cb5

Browse files
a-robinsonporsager
authored andcommitted
Update Cloudflare createHash polyfill to support md5 and hex encoding
Since the md5 method in cf/src/connection.js expects to be able to call crypto.createHash('md5').update(x).digest('hex') This was causing md5 password auth to hang when used from a Cloudflare worker, but now I've confirmed md5 password auth works.
1 parent 0428b30 commit 09e6cb5

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

cf/polyfills.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,25 @@ export const crypto = {
4747
),
4848
createHash: type => ({
4949
update: x => ({
50-
digest: () => {
51-
if (type !== 'sha256')
52-
throw Error('createHash only supports sha256 in this environment.')
53-
if (!(x instanceof Uint8Array))
50+
digest: encoding => {
51+
if (!(x instanceof Uint8Array)) {
5452
x = textEncoder.encode(x)
55-
return Crypto.subtle.digest('SHA-256', x)
53+
}
54+
let prom
55+
if (type === 'sha256') {
56+
prom = Crypto.subtle.digest('SHA-256', x)
57+
} else if (type === 'md5') {
58+
prom = Crypto.subtle.digest('md5', x)
59+
} else {
60+
throw Error('createHash only supports sha256 or md5 in this environment, not ${type}.')
61+
}
62+
if (encoding === 'hex') {
63+
return prom.then((arrayBuf) => Buffer.from(arrayBuf).toString('hex'))
64+
} else if (encoding) {
65+
throw Error(`createHash only supports hex encoding or unencoded in this environment, not ${encoding}`)
66+
} else {
67+
return prom
68+
}
5669
}
5770
})
5871
}),

0 commit comments

Comments
 (0)