Skip to content

Commit

Permalink
feat: ✨ enhance hash function and add comprehensive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 8, 2025
1 parent 2e1e443 commit f37fed2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
71 changes: 70 additions & 1 deletion packages/core/src/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict"
import test, { beforeEach, describe } from "node:test"
import { randomHex } from "./crypto"
import { hash, randomHex } from "./crypto"
import { TestHost } from "./testhost"

describe("randomHex function", () => {
test("should generate a hex string of the correct length", () => {
Expand Down Expand Up @@ -34,3 +35,71 @@ describe("randomHex function", () => {
assert.strictEqual(hexString, "")
})
})
describe("hash function", () => {
beforeEach(async () => {
TestHost.install()
})

test("should generate a SHA-256 hash by default", async () => {
const value = "test"
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "cf5b68d216bfdd52042ee1cf184e7d6da30db24ddeb50b058a4e2de4eec897d8")
})

test("should generate a hash with a specified algorithm", async () => {
const value = "test"
const hashedValue = await hash(value, { algorithm: "sha-1" })
assert.strictEqual(hashedValue, "9ffed885632cdb7cd0035741747950a42dd776ed")
})

test("should generate a hash with a specified length", async () => {
const value = "test"
const options = { length: 32 }
const hashedValue = await hash(value, options)
assert.strictEqual(hashedValue.length, 32)
})

test("should include version in the hash when specified", async () => {
const value = "test"
const options = { version: true }
const hashedValue = await hash(value, options)
assert.strictEqual(hashedValue.length, 64)
})

test("should handle null and undefined values correctly", async () => {
const value: any = null
const hashedValueNull = await hash(value)
const hashedValueUndefined = await hash(undefined)
assert.notStrictEqual(hashedValueNull, hashedValueUndefined)
})

test("should handle arrays correctly", async () => {
const value = [1, 2, 3]
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "479be8e5607ab54ac31c9f79820bdb8651df86d11392f2e4245e8b761c49c62a")
})

test("should handle objects correctly", async () => {
const value = { a: 1, b: 2 }
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "84f66fa8d14b5e4690961254ecdeb24cdae959448dde2148410dc49a14472625")
})

test("should handle buffers correctly", async () => {
const value = Buffer.from("test")
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "cf5b68d216bfdd52042ee1cf184e7d6da30db24ddeb50b058a4e2de4eec897d8")
})

test("should handle ArrayBuffer correctly", async () => {
const value = new ArrayBuffer(8)
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "58ae1766c3b851958bb1f1d0a15af1a3e1d3775dcf13b000d3005ddcb3a55c9b")
})

test("should handle Blobs correctly", async () => {
const value = new Blob(["test"])
const hashedValue = await hash(value)
assert.strictEqual(hashedValue, "cf5b68d216bfdd52042ee1cf184e7d6da30db24ddeb50b058a4e2de4eec897d8")
})
})
9 changes: 8 additions & 1 deletion packages/core/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ export async function hash(value: any, options?: HashOptions) {
const { algorithm = "sha-256", version, length, ...rest } = options || {}

const sep = utf8Encode("|")
const un = utf8Encode("undefined")
const nu = utf8Encode("null")

const h: Uint8Array[] = []
const append = async (v: any) => {
if (
if (v === null) h.push(nu)
else if (v === undefined) h.push(un)
else if (
typeof v == "string" ||
typeof v === "number" ||
typeof v === "boolean"
Expand All @@ -58,6 +63,7 @@ export async function hash(value: any, options?: HashOptions) {
await append(c)
}
else if (v instanceof Buffer) h.push(new Uint8Array(v))
else if (v instanceof ArrayBuffer) h.push(new Uint8Array(v))
else if (v instanceof Blob)
h.push(new Uint8Array(await v.arrayBuffer()))
else if (typeof v === "object")
Expand All @@ -67,6 +73,7 @@ export async function hash(value: any, options?: HashOptions) {
h.push(sep)
await append(v[c])
}
else if (typeof v === "function") h.push(utf8Encode(v.toString()))
else h.push(utf8Encode(JSON.stringify(v)))
}
if (version) {
Expand Down

0 comments on commit f37fed2

Please sign in to comment.