-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Faster SCRAM when using browserCrypto (#1063)
We use browser/globalThis crypto when it's available, but the implementation of HMAC is slower using globalThis.crypto.subtle.sign. This speeds that up by about 2x, but it's still 10x slower than Node's `createHmac`.
- Loading branch information
1 parent
0cbd195
commit eb92923
Showing
12 changed files
with
149 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1 @@ | ||
import { crypto } from "https://deno.land/[email protected]/crypto/mod.ts"; | ||
|
||
import type { CryptoUtils } from "./utils.ts"; | ||
|
||
const cryptoUtils: CryptoUtils = { | ||
async randomBytes(size: number): Promise<Uint8Array> { | ||
const buf = new Uint8Array(size); | ||
return crypto.getRandomValues(buf); | ||
}, | ||
|
||
async H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
}, | ||
|
||
async HMAC(key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array( | ||
await crypto.subtle.sign( | ||
"HMAC", | ||
await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"], | ||
), | ||
msg, | ||
), | ||
); | ||
}, | ||
}; | ||
|
||
export default cryptoUtils; | ||
export { cryptoUtils as default } from "./browserCrypto.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import type { CryptoUtils } from "./utils"; | ||
|
||
const cryptoUtils: CryptoUtils = { | ||
async randomBytes(size: number): Promise<Uint8Array> { | ||
return crypto.getRandomValues(new Uint8Array(size)); | ||
}, | ||
async function makeKey(key: Uint8Array): Promise<CryptoKey> { | ||
return await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"], | ||
); | ||
} | ||
|
||
async H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
}, | ||
function randomBytes(size: number): Uint8Array { | ||
return crypto.getRandomValues(new Uint8Array(size)); | ||
} | ||
|
||
async HMAC(key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array( | ||
await crypto.subtle.sign( | ||
"HMAC", | ||
await crypto.subtle.importKey( | ||
"raw", | ||
key, | ||
{ | ||
name: "HMAC", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
false, | ||
["sign"], | ||
), | ||
msg, | ||
), | ||
); | ||
}, | ||
}; | ||
async function H(msg: Uint8Array): Promise<Uint8Array> { | ||
return new Uint8Array(await crypto.subtle.digest("SHA-256", msg)); | ||
} | ||
|
||
async function HMAC( | ||
key: Uint8Array | CryptoKey, | ||
msg: Uint8Array, | ||
): Promise<Uint8Array> { | ||
const cryptoKey = | ||
key instanceof Uint8Array ? ((await makeKey(key)) as CryptoKey) : key; | ||
return new Uint8Array(await crypto.subtle.sign("HMAC", cryptoKey, msg)); | ||
} | ||
|
||
export default cryptoUtils; | ||
export const cryptoUtils: CryptoUtils = { | ||
makeKey, | ||
randomBytes, | ||
H, | ||
HMAC, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import crypto from "node:crypto"; | ||
import type { CryptoUtils } from "./utils"; | ||
|
||
function makeKey(keyBytes: Uint8Array): Promise<Uint8Array> { | ||
return Promise.resolve(keyBytes); | ||
} | ||
|
||
function randomBytes(size: number): Buffer { | ||
return crypto.randomBytes(size); | ||
} | ||
|
||
async function H(msg: Uint8Array): Promise<Buffer> { | ||
const sign = crypto.createHash("sha256"); | ||
sign.update(msg); | ||
return sign.digest(); | ||
} | ||
|
||
async function HMAC( | ||
key: Uint8Array | CryptoKey, | ||
msg: Uint8Array, | ||
): Promise<Buffer> { | ||
const cryptoKey: Uint8Array | crypto.KeyObject = | ||
key instanceof Uint8Array ? key : crypto.KeyObject.from(key); | ||
const hm = crypto.createHmac("sha256", cryptoKey); | ||
hm.update(msg); | ||
return hm.digest(); | ||
} | ||
|
||
export const cryptoUtils: CryptoUtils = { | ||
makeKey, | ||
randomBytes, | ||
H, | ||
HMAC, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.