mirror of
https://github.com/selfxyz/self.git
synced 2026-02-09 05:46:01 -05:00
22 lines
657 B
TypeScript
22 lines
657 B
TypeScript
import {sha256} from 'js-sha256';
|
|
|
|
// hash logic here because the one in utils.ts only works with node
|
|
export function hash(bytesArray: number[]) {
|
|
let unsignedBytesArray = bytesArray.map(toUnsignedByte);
|
|
let hash = sha256(unsignedBytesArray);
|
|
return hexToSignedBytes(hash);
|
|
}
|
|
|
|
export function hexToSignedBytes(hexString: string): number[] {
|
|
let bytes = [];
|
|
for (let i = 0; i < hexString.length - 1; i += 2) {
|
|
let byte = parseInt(hexString.substr(i, 2), 16);
|
|
bytes.push(byte >= 128 ? byte - 256 : byte);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
export function toUnsignedByte(signedByte: number) {
|
|
return signedByte < 0 ? signedByte + 256 : signedByte;
|
|
}
|