@li0ard/gost341194
Version:
GOST R 34.11-94 hash function in pure TypeScript
21 lines (20 loc) • 720 B
JavaScript
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
export const xor = (a, b) => {
let mlen = Math.min(a.length, b.length);
let result = new Uint8Array(mlen);
for (let i = 0; i < mlen; i++)
result[i] = a[i] ^ b[i];
return result;
};
export const hexToNumber = (hex) => {
if (typeof hex !== 'string')
throw new Error('hex string expected, got ' + typeof hex);
return hex === '' ? 0n : BigInt('0x' + hex);
};
export const bytesToNumberBE = (bytes) => hexToNumber(bytesToHex(bytes));
export const numberToBytesBE = (n, len) => {
let num = n.toString(16).padStart(len * 2, '0');
while (num.length % 2 != 0)
num = "0" + num;
return hexToBytes(num);
};