mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 03:18:53 +00:00
36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
import { it, expect } from "bun:test";
|
|
|
|
import crypto from "node:crypto";
|
|
|
|
it("crypto.randomBytes should return a Buffer", () => {
|
|
expect(crypto.randomBytes(1) instanceof Buffer).toBe(true);
|
|
expect(Buffer.isBuffer(crypto.randomBytes(1))).toBe(true);
|
|
});
|
|
|
|
// https://github.com/oven-sh/bun/issues/1839
|
|
it("crypto.createHash ", () => {
|
|
function fn() {
|
|
crypto.createHash("sha1").update(Math.random(), "ascii").digest("base64");
|
|
}
|
|
|
|
for (let i = 0; i < 10; i++) fn();
|
|
});
|
|
|
|
it("crypto.createHmac", () => {
|
|
const result = crypto.createHmac("sha256", "key").update("message").digest("base64");
|
|
|
|
expect(result).toBe("bp7ym3X//Ft6uuUn1Y/a2y/kLnIZARl2kXNDBl9Y7Uo=");
|
|
});
|
|
|
|
it("web crypto", async () => {
|
|
let bytes = new Uint8Array(32);
|
|
crypto.getRandomValues(bytes);
|
|
await crypto.subtle.digest("SHA-256", bytes);
|
|
});
|
|
|
|
// https://github.com/oven-sh/bun/issues/2110
|
|
it("hash regression #2110", () => {
|
|
var s = "6fbf7e2948e0c2f29eaacac1733546a4af5ca482";
|
|
expect(crypto.createHash("sha1").update(s, "binary").digest("hex")).toBe("e7c8b3c6f114c523d07ee355c534ee9bef3c044b");
|
|
});
|