Skip to content

Commit

Permalink
Update Cloudflare createHash polyfill to support md5 and hex encoding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
a-robinson authored and porsager committed Oct 23, 2023
1 parent 0428b30 commit 09e6cb5
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions cf/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,25 @@ export const crypto = {
),
createHash: type => ({
update: x => ({
digest: () => {
if (type !== 'sha256')
throw Error('createHash only supports sha256 in this environment.')
if (!(x instanceof Uint8Array))
digest: encoding => {
if (!(x instanceof Uint8Array)) {
x = textEncoder.encode(x)
return Crypto.subtle.digest('SHA-256', x)
}
let prom
if (type === 'sha256') {
prom = Crypto.subtle.digest('SHA-256', x)
} else if (type === 'md5') {
prom = Crypto.subtle.digest('md5', x)
} else {
throw Error('createHash only supports sha256 or md5 in this environment, not ${type}.')
}
if (encoding === 'hex') {
return prom.then((arrayBuf) => Buffer.from(arrayBuf).toString('hex'))
} else if (encoding) {
throw Error(`createHash only supports hex encoding or unencoded in this environment, not ${encoding}`)
} else {
return prom
}
}
})
}),
Expand Down

0 comments on commit 09e6cb5

Please sign in to comment.