import { sha, MD5, MD4, SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_256, gc, CryptoHasher } from "bun"; import { it, expect, describe } from "bun:test"; import crypto from "crypto"; const HashClasses = [MD5, MD4, SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_256]; describe("CryptoHasher", () => { it("CryptoHasher.algorithms", () => { expect(CryptoHasher.algorithms).toEqual([ "blake2b256", "md4", "md5", "ripemd160", "sha1", "sha224", "sha256", "sha384", "sha512", "sha512-256", ]); }); it("CryptoHasher md5", () => { var hasher = new CryptoHasher("md5"); hasher.update("hello world"); expect(hasher.digest("hex")).toBe("5eb63bbbe01eeed093cb22bb8f5acdc3"); expect(hasher.algorithm).toBe("md5"); }); it("CryptoHasher blake2b256", () => { var hasher = new CryptoHasher("blake2b256"); hasher.update("hello world"); expect(hasher.algorithm).toBe("blake2b256"); expect(hasher.digest("hex")).toBe( // b2sum --length=256 "256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610", ); }); it("CryptoHasher sha512", () => { var hasher = new CryptoHasher("sha512"); hasher.update("hello world"); expect(hasher.digest("hex")).toBe( "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f", ); expect(hasher.algorithm).toBe("sha512"); }); it("CryptoHasher sha256", () => { var hasher = new CryptoHasher("sha256"); hasher.update("hello world"); expect(hasher.digest("hex")).toBe("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); expect(hasher.algorithm).toBe("sha256"); }); it("CryptoHasher sha256 multi-part", () => { var hasher = new CryptoHasher("sha256"); hasher.update("hello "); hasher.update("world"); expect(hasher.digest("hex")).toBe("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); expect(hasher.algorithm).toBe("sha256"); }); it("CryptoHasher resets when digest is called", () => { var hasher = new CryptoHasher("sha256"); hasher.update("hello"); expect(hasher.digest("hex")).toBe("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"); hasher.update("world"); expect(hasher.digest("hex")).toBe("486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7"); }); for (let alg of CryptoHasher.algorithms) { it(`CryptoHasher ${alg} copy is the same`, () => { const orig = new CryptoHasher(alg); orig.update("hello"); const copy = orig.copy(); expect(copy.digest("hex")).toBe(orig.digest("hex")); expect(copy.algorithm).toBe(orig.algorithm); }); it(`CryptoHasher ${alg} copy is not linked`, () => { const orig = new CryptoHasher(alg); orig.update("hello"); const copy = orig.copy(); orig.update("world"); expect(copy.digest("hex")).not.toBe(orig.digest("hex")); }); it(`CryptoHasher ${alg} copy can be used after digest()`, () => { const orig = new CryptoHasher(alg); orig.update("hello"); orig.digest("hex"); const copy = orig.copy(); expect(() => copy.digest("hex")).not.toThrow(); }); it(`CryptoHasher ${alg} copy updates the same`, () => { const orig = new CryptoHasher(alg); orig.update("hello"); const copy = orig.copy(); orig.update("world"); copy.update("world"); expect(copy.digest("hex")).toBe(orig.digest("hex")); }); } }); describe("crypto.getCurves", () => { it("should return an array of strings", () => { expect(Array.isArray(crypto.getCurves())).toBe(true); expect(typeof crypto.getCurves()[0]).toBe("string"); }); }); describe("crypto", () => { for (let Hash of HashClasses) { for (let [input, label] of [ ["hello world", '"hello world"'], ["hello world".repeat(20).slice(), '"hello world" x 20'], ["", "empty string"], ["a", '"a"'], ]) { describe(label, () => { 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); }); }); } } }); on value='got'>got Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/macro-check.js (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-05-02[bun.js] Add `Bun.nanoseconds()` to report time in nanosGravatar Jarred Sumner 4-146/+145
2022-05-02[bun:ffi] ~20% faster FFI bindings for functions with argumentsGravatar Jarred Sumner 10-13/+220
2022-05-02Automatic CString supportGravatar Jarred Sumner 1-23/+78
2022-05-01[bun:ffi] Add wrapper for type coercionGravatar Jarred Sumner 1-2/+172
2022-05-01wip Buffer.fillGravatar Jarred Sumner 3-95/+167
2022-05-01Buffer.compare & Buffer.equalGravatar Jarred Sumner 2-5/+244
2022-05-01[bun.js] Improve `Buffer` creation perf a littleGravatar Jarred Sumner 2-8/+45
2022-05-01[bun.js] Implement `Buffer.concat`Gravatar Jarred Sumner 2-1/+78
2022-04-30Bump WebKitGravatar Jarred Sumner 1-0/+0
2022-04-30cleanupGravatar Jarred Sumner 5-2/+132
2022-04-30[bun.js] Implement `Buffer.from` and `Buffer.copy`Gravatar Jarred Sumner 12-520/+952
2022-04-30[bun ffi] Fix missing `"void"`Gravatar Jarred Sumner 1-0/+1
2022-04-30[bun ffi] Remove dependency on libtcc1.a and improve error messagesGravatar Jarred Sumner 3-36/+139
2022-04-30wipGravatar Jarred Sumner 9-272/+883
2022-04-30Update ffi-test.cGravatar Jarred Sumner 1-24/+35
2022-04-30aGravatar Jarred Sumner 1-0/+27
2022-04-29[bun:ffi] it worksGravatar Jarred Sumner 15-221/+1277
2022-04-29[bun.js] Implement unsafe.{`arrayBufferToPtr`, `arrayBufferFromPtr`, `bufferF...Gravatar Jarred Sumner 6-82/+187
2022-04-29[bun ffi] Support pointersGravatar Jarred Sumner 4-238/+179
2022-04-29[bun ffi] support `i32`, `i8`, `u8`, `u16`, `i16`, `u32`, `bool`Gravatar Jarred Sumner 5-159/+260
2022-04-29more tests for bufferGravatar Jarred Sumner 1-1/+165
2022-04-29add more to buffer implementationGravatar Jarred Sumner 14-157/+783
2022-04-29ffi test codeGravatar Jarred Sumner 2-26/+247
2022-04-29wipGravatar Jarred Sumner 10-16/+312
2022-04-29commit moreGravatar Jarred Sumner 3-0/+81