Skip to content

Commit

Permalink
fix: 🔒 update hash algorithm names to include hyphens
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 22, 2024
1 parent d8cedd6 commit 1257c90
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/scripts/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Utility to hash an object, array into a string that is appropriate for hashing p
const h = parsers.hash({ obj, other }, { length: 12 })
```

By default, uses `sha1`, but `sha256` can also be used. The hash packing logic may change between versions of genaiscript.
By default, uses `sha-1`, but `sha-256` can also be used. The hash packing logic may change between versions of genaiscript.

## Command line

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,5 @@ export class JSONLineCache<K, V> extends MemoryCache<K, V> {
* @returns A promise resolving to the SHA256 hash string
*/
async function keySHA(key: any) {
return await hash(key, { algorithm: "sha256", version: true }) // Compute SHA256 hash
return await hash(key, { algorithm: "sha-256", version: true }) // Compute SHA256 hash
}
45 changes: 29 additions & 16 deletions packages/core/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
// crypto.ts - Provides cryptographic functions for secure operations

// Importing the toHex function from the util module to convert byte arrays to hexadecimal strings
import { toHex, utf8Encode } from "./util"
import { getRandomValues, createHash } from "crypto"
import { concatBuffers, toHex, utf8Encode } from "./util"

async function getRandomValues(bytes: Uint8Array) {
if (typeof self !== "undefined" && self.crypto) {
self.crypto.getRandomValues(bytes)
} else {
const { getRandomValues } = await import("crypto")
getRandomValues(bytes)
}
}

async function digest(algorithm: string, data: Uint8Array) {
algorithm = algorithm.toUpperCase()
if (typeof self !== "undefined" && self.crypto) {
return self.crypto.subtle.digest(algorithm, data)
} else {
const { subtle } = await import("crypto")
return subtle.digest(algorithm, data)
}
}

/**
* Generates a random hexadecimal string of a specified size.
Expand All @@ -22,37 +40,32 @@ export function randomHex(size: number) {
}

export async function hash(value: any, options?: HashOptions) {
const { algorithm = "sha1", length, ...rest } = options || {}
const { algorithm = "sha-1", length, ...rest } = options || {}

const h = createHash(algorithm)
const h: Uint8Array[] = []
const append = async (v: any) => {
if (
typeof v == "string" ||
typeof v === "number" ||
typeof v === "boolean"
)
h.update(String(v), "utf8")
h.push(utf8Encode(String(v)))
else if (Array.isArray(v)) for (const c of v) await append(c)
else if (v instanceof Buffer) h.update(v)
else if (v instanceof Buffer) h.push(new Uint8Array(v))
else if (v instanceof Blob)
h.update(new Uint8Array(await v.arrayBuffer()))
h.push(new Uint8Array(await v.arrayBuffer()))
else if (typeof v === "object")
for (const c of Object.keys(v).sort()) {
h.update(c, "utf8")
h.push(utf8Encode(c))
await append(v[c])
}
else h.update(utf8Encode(JSON.stringify(v)))
else h.push(utf8Encode(JSON.stringify(v)))
}
await append(value)
await append(rest)

const buf = await h.digest()
let res: string
try {
res = await buf.toString("hex")
} catch (e) {
res = toHex(new Uint8Array(buf))
}
const buf = await digest(algorithm, concatBuffers(...h))
let res = toHex(new Uint8Array(buf))
if (length) res = res.slice(0, length)
return res
}
2 changes: 1 addition & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ interface Tokenizers {
}

interface HashOptions {
algorithm?: "sha1" | "sha256"
algorithm?: "sha-1" | "sha-256"
length?: number
version?: boolean
}
Expand Down

0 comments on commit 1257c90

Please sign in to comment.