diff options
author | polwex <polwex@sortug.com> | 2025-07-16 11:59:08 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-07-16 11:59:08 +0700 |
commit | 9c2fba56e0f68f976c1abe486f9fd3c6e93b437e (patch) | |
tree | 22470efb92a9f1b673136d7c0bdadc06abb3ba35 /lib/utils/bit.ts | |
parent | 8ae0d2779c26f74e64a1db2b028bd2ac2f599cb4 (diff) |
'm'
Diffstat (limited to 'lib/utils/bit.ts')
-rw-r--r-- | lib/utils/bit.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/utils/bit.ts b/lib/utils/bit.ts new file mode 100644 index 0000000..bc1ed1a --- /dev/null +++ b/lib/utils/bit.ts @@ -0,0 +1,35 @@ +import { sha256 } from "urbit-key-generation/src/utils"; + +export function shas(buf: Buffer, salt: Buffer) { + return sha256(xor(salt, sha256(buf))); +} + +export function xor(a: Buffer, b: Buffer) { + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + console.log("a", a); + console.log("b", b); + throw new Error("only xor buffers!"); + } + const length = Math.max(a.byteLength, b.byteLength); + const result = new Uint8Array(length); + for (let i = 0; i < length; i++) { + result[i] = a[i] ^ b[i]; + } + return result; +} + +export function shaf(buf: Buffer, salt: Buffer) { + const result = shas(buf, salt); + const halfway = result.length / 2; + const front = result.slice(0, halfway); + const back = result.slice(halfway, result.length); + return xor(front, back); +} + +export function hex2buf(hex: string) { + return Buffer.from(hex, "hex").reverse(); +} + +export function buf2hex(buf: Uint8Array) { + return Buffer.from(buf).reverse().toString("hex"); +} |