diff options
author | 2022-04-13 04:20:05 -0700 | |
---|---|---|
committer | 2022-04-13 04:20:05 -0700 | |
commit | 3db3057d421a145743d63ebb5a487fb73e0e9f0b (patch) | |
tree | ece61bf4b75fb7f108c038082a6b273a234f27bf /integration/bunjs-only-snippets | |
parent | f6d73cb06ec5fe217a4d2d03808f68ecbc2d3461 (diff) | |
download | bun-3db3057d421a145743d63ebb5a487fb73e0e9f0b.tar.gz bun-3db3057d421a145743d63ebb5a487fb73e0e9f0b.tar.zst bun-3db3057d421a145743d63ebb5a487fb73e0e9f0b.zip |
Support digest("base64" | "hex") in the hashings
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r-- | integration/bunjs-only-snippets/crypto.test.js | 98 | ||||
-rw-r--r-- | integration/bunjs-only-snippets/tsconfig.json | 3 |
2 files changed, 73 insertions, 28 deletions
diff --git a/integration/bunjs-only-snippets/crypto.test.js b/integration/bunjs-only-snippets/crypto.test.js index 344d935d5..2c784e496 100644 --- a/integration/bunjs-only-snippets/crypto.test.js +++ b/integration/bunjs-only-snippets/crypto.test.js @@ -1,28 +1,70 @@ -import { it, expect } from "bun:test"; - -for (let Hasher of [ - Bun.SHA1, - Bun.SHA256, - Bun.SHA384, - Bun.SHA512, - Bun.SHA512_256, -]) { - it(`${Hasher.name} instance`, () => { - var buf = new Uint8Array(256); - const result = new Hasher(); - result.update("hello world"); - result.final(buf); - }); -} - -for (let HashFn of [ - Bun.sha1, - Bun.sha256, - Bun.sha384, - Bun.sha512, - Bun.sha512_256, -]) { - it(`${HashFn.name} instance`, () => { - HashFn("hello world"); - }); -} +import { + sha, + MD5, + MD4, + SHA1, + SHA256, + SHA384, + SHA512, + SHA512_256, + gc, +} from "bun"; +import { it, expect, describe } from "bun:test"; +import { readFileSync } from "fs"; + +describe("crypto", () => { + for (let Hash of [MD5, MD4, SHA1, SHA256, SHA384, SHA512, SHA512_256]) { + for (let input of [ + "hello world", + "hello world".repeat(20).slice(), + "", + "a", + ]) { + describe(input, () => { + gc(true); + + it(`${Hash.name} base64`, () => { + gc(true); + const result = new Hash(); + result.update(input); + expect(typeof result.digest("base64")).toBe("string"); + gc(true); + }); + + it(`${Hash.name} hash base64`, () => { + Hash.hash(input, "base64"); + gc(true); + }); + + it(`${Hash.name} hex`, () => { + const result = new Hash(); + result.update(input); + expect(typeof result.digest("hex")).toBe("string"); + gc(true); + }); + + it(`${Hash.name} hash hex`, () => { + expect(typeof Hash.hash(input, "hex")).toBe("string"); + gc(true); + }); + + it(`${Hash.name} buffer`, () => { + var buf = new Uint8Array(256); + const result = new Hash(); + + result.update(input); + expect(result.digest(buf)).toBe(buf); + expect(buf[0] != 0).toBe(true); + gc(true); + }); + + it(`${Hash.name} buffer`, () => { + var buf = new Uint8Array(256); + + expect(Hash.hash(input, buf) instanceof Uint8Array).toBe(true); + gc(true); + }); + }); + } + } +}); diff --git a/integration/bunjs-only-snippets/tsconfig.json b/integration/bunjs-only-snippets/tsconfig.json index 05df31d44..9a6c36e06 100644 --- a/integration/bunjs-only-snippets/tsconfig.json +++ b/integration/bunjs-only-snippets/tsconfig.json @@ -3,8 +3,11 @@ "lib": ["ESNext"], "module": "esnext", "target": "esnext", + "noEmit": true, + "allowJs": true, "typeRoots": ["../../types"], "types": ["bun"], + "allowSyntheticDefaultImports": true, "baseUrl": ".", "paths": { "foo/bar": ["baz.js"] |