Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
refactor(cryptography): normalise value into Uint8Array before hash…
Browse files Browse the repository at this point in the history
…ing (#683)
  • Loading branch information
faustbrian authored Jan 12, 2022
1 parent 2f176e1 commit e759845
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/cryptography/source/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ export class Hash {
return Buffer.from(sha256(Hash.#bufferize(buffer)));
}

static #bufferize(buffer: Buffer | string) {
return buffer instanceof Buffer ? buffer : Buffer.from(buffer);
static #bufferize(value: Buffer | string): Uint8Array {
if (value instanceof Buffer) {
const buffer = new ArrayBuffer(value.length);
const result = new Uint8Array(buffer);

for (let i = 0; i < value.length; ++i) {
result[i] = value[i];
}

return result;
}

return new TextEncoder().encode(value);
}
}

0 comments on commit e759845

Please sign in to comment.