Skip to content

Instantly share code, notes, and snippets.

@taurenshaman
Created March 5, 2021 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taurenshaman/86f96ae81a3f26188bffa735218a70c0 to your computer and use it in GitHub Desktop.
Save taurenshaman/86f96ae81a3f26188bffa735218a70c0 to your computer and use it in GitHub Desktop.
pw-core/src/signers/blake2b-signer.ts
import { Hasher } from '.';
import { Reader } from 'ckb-js-toolkit';
import blake2b from 'blake2b';
export class Blake2bHasher extends Hasher {
constructor(key: string = null) {
const keyData = !key || key.length === 0 ? null : Buffer.from(key);
const h = blake2b(
32,
keyData,
null,
new Uint8Array(Reader.fromRawString('ckb-default-hash').toArrayBuffer())
);
super(h);
}
update(data: string | ArrayBuffer): Hasher {
let array: Buffer;
if (data instanceof Reader) {
/** Reader type params not enter this branch, it's weired */
array = Buffer.from(data.serializeJson().replace('0x', ''));
} else if (data instanceof ArrayBuffer) {
array = Buffer.from(new Uint8Array(data));
} else if (typeof data === 'string') {
array = Buffer.from(data);
} else {
array = Buffer.from(new Uint8Array(new Reader(data).toArrayBuffer()));
}
this.h.update(array);
return this;
}
digest(): Reader {
const out = new Uint8Array(32);
this.h.digest(out);
return new Reader(out.buffer);
}
reset(): void {
this.h = blake2b(
32,
null,
null,
new Uint8Array(Reader.fromRawString('ckb-default-hash').toArrayBuffer())
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment