generated from denorg/starter
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: write a simple crypto.subtle based hmac implementation to stop …
…relying on old std
- Loading branch information
Showing
4 changed files
with
142 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* A very primitive crypto.subtle.digestSync-based HMAC-SHA256 synchronous implementation. | ||
*/ | ||
import { crypto } from "std/crypto/mod.ts"; | ||
|
||
function mergeArrays(a: Uint8Array, b: Uint8Array): Uint8Array { | ||
const result = new Uint8Array(a.length + b.length); | ||
result.set(a); | ||
result.set(b, a.length); | ||
return result; | ||
} | ||
function blockSizedKey(key: Uint8Array, blockSize: number): Uint8Array { | ||
if (key.length > blockSize) { | ||
return new Uint8Array(crypto.subtle.digestSync("SHA-256", key)); | ||
} else if (key.length < blockSize) { | ||
const result = new Uint8Array(blockSize); | ||
result.set(key); | ||
return result; | ||
} | ||
return key; | ||
} | ||
|
||
export function hmacSHA256(key: Uint8Array, data: Uint8Array): Uint8Array { | ||
const b_key = blockSizedKey(key, 64); | ||
const o_pad = new Uint8Array(64); | ||
const i_pad = new Uint8Array(64); | ||
for (let i = 0; i < 64; i++) { | ||
o_pad[i] = b_key[i] ^ 0x5c; | ||
i_pad[i] = b_key[i] ^ 0x36; | ||
} | ||
return new Uint8Array(crypto.subtle.digestSync( | ||
"SHA-256", | ||
mergeArrays( | ||
o_pad, | ||
new Uint8Array( | ||
crypto.subtle.digestSync("SHA-256", mergeArrays(i_pad, data)), | ||
), | ||
), | ||
)); | ||
} |
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,66 @@ | ||
import { decodeHex } from "std/encoding/hex.ts"; | ||
import { hmacSHA256 } from "./hmac.ts"; | ||
import { HmacSha256 } from "https://deno.land/std@0.160.0/hash/sha256.ts"; | ||
|
||
Deno.bench("hmac - subtle", { group: "small hmac", baseline: true }, () => { | ||
hmacSHA256( | ||
decodeHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), | ||
decodeHex("4869205468657265"), | ||
); | ||
}); | ||
Deno.bench("hmac - old", { group: "small hmac" }, () => { | ||
const hmac = new HmacSha256( | ||
decodeHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), | ||
); | ||
hmac.update(decodeHex("4869205468657265")); | ||
hmac.digest(); | ||
}); | ||
|
||
Deno.bench("hmac - subtle", { group: "larger hmac", baseline: true }, () => { | ||
hmacSHA256( | ||
decodeHex( | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
), | ||
decodeHex( | ||
"5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e", | ||
), | ||
); | ||
}); | ||
Deno.bench("hmac - old", { group: "larger hmac" }, () => { | ||
const hmac = new HmacSha256( | ||
decodeHex( | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
), | ||
); | ||
hmac.update( | ||
decodeHex( | ||
"5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e", | ||
), | ||
); | ||
hmac.digest(); | ||
}); | ||
|
||
// appears to be comparable to the old implementation in practice | ||
Deno.bench("hmac - subtle", { group: "hmac as used", baseline: true }, () => { | ||
hmacSHA256( | ||
decodeHex( | ||
"834680896aab19cf86a4c0edf4cef4db8af5bd05a40d42e768e658057ee521e63acadefa59fb2f3133def01d2c3dd5d1", | ||
).subarray(0, 48), | ||
decodeHex( | ||
"024b5b12a8b3d622c289ad69536a30cda848074c82d06ff05775d653bf0fc48033ddafba8071b7f119810dd57619553e87aff3bc8c237669523dc6530b8ee267", | ||
).subarray(0, 64), | ||
); | ||
}); | ||
Deno.bench("hmac - old", { group: "hmac as used" }, () => { | ||
const hmac = new HmacSha256( | ||
decodeHex( | ||
"834680896aab19cf86a4c0edf4cef4db8af5bd05a40d42e768e658057ee521e63acadefa59fb2f3133def01d2c3dd5d1", | ||
).subarray(0, 48), | ||
); | ||
hmac.update( | ||
decodeHex( | ||
"024b5b12a8b3d622c289ad69536a30cda848074c82d06ff05775d653bf0fc48033ddafba8071b7f119810dd57619553e87aff3bc8c237669523dc6530b8ee267", | ||
).subarray(0, 64), | ||
); | ||
hmac.digest(); | ||
}); |
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,18 @@ | ||
import { hmacSHA256 } from "./hmac.ts"; | ||
import { decodeHex } from "std/encoding/hex.ts"; | ||
import { assertEquals } from "std/assert/assert_equals.ts"; | ||
|
||
const encoder = new TextEncoder(); | ||
|
||
Deno.test("basic hmacSHA256", (): void => { | ||
const result = hmacSHA256( | ||
decodeHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), | ||
decodeHex("4869205468657265"), | ||
); | ||
assertEquals( | ||
result, | ||
decodeHex( | ||
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", | ||
), | ||
); | ||
}); |