Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ hmac
true
```

Strings are UTF-8. Already have bytes? Pass a `Uint8Array`. `create()` takes the exact registry name, while `resolveAlgorithm()` accepts labels, case and punctuation. Unsupported encodings throw a `HashError`. A hex string is still text here. Decode it first.
Strings are UTF-8. Already have bytes? Pass a `Uint8Array`. `create()` takes the exact registry name, while `resolveAlgorithm()` accepts labels, case and punctuation. Unsupported encodings throw a `HashError`. A hex string is still text here. Decode it first. Bare functions like `sha256` don't take kindly to text. Give them `new TextEncoder().encode(text)` or let `create(name).hash(text)` handle it.

[Hashing and byte imports](https://hashes.agntn.dev/guide/hashing) · [Verify](https://hashes.agntn.dev/guide/verify) · [KDFs and salts](https://hashes.agntn.dev/guide/kdf)

Expand Down
11 changes: 6 additions & 5 deletions src/core/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ export function viewOf(data: Uint8Array): DataView {
}

/**
* Throws unless the value is bytes. A string or a plain array would otherwise hash to a digest of
* something else without a word.
* Throws unless the value is bytes, and tells hashed text how to become them. A string or a plain
* array would otherwise hash to a digest of something else without a word.
*
* @param value - What the caller passed.
* @param name - The argument's name, for the message.
*/
export function assertBytes(value: unknown, name: string): asserts value is Uint8Array {
if (!(value instanceof Uint8Array)) {
throw new HashError(
`${name} must be a Uint8Array, not ${value === null ? "null" : typeof value}`,
);
const kind = value === null ? "null" : typeof value;
const text = kind === "string" && (name === "data" || name === "message");
const hint = text ? ". Use TextEncoder, or create(name).hash(text)" : "";
throw new HashError(`${name} must be a Uint8Array, not ${kind}${hint}`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("one byte function bundled with esbuild", () => {
// Every one of these came out near 20 kB with all 18 constant tables before the fix. The count
// catches one table coming back, which is too small to show in the size.
it.each([
["md5", 3, 4_000],
["md5", 3, 4_100],
["sha1", 1, 3_500],
["sha256", 2, 4_000],
/* SHA-256's two tables plus its own initial value, and still no registry. */
Expand All @@ -94,9 +94,9 @@ describe("one byte function bundled with esbuild", () => {
["ripemd160", 5, 5_000],
["blake2b", 3, 8_000],
/* CRC-32 carries every variant and the error a wrong one throws, whatever the value. */
["crc32", 0, 2_600],
["crc32", 0, 2_700],
["scrypt", 2, 8_500],
["argon2id", 3, 12_000],
["argon2id", 3, 12_100],
["bcrypt", 0, 4_000],
])("%s leaves the other algorithms out", async (name, tables, limit) => {
const code = await bundle(name);
Expand Down
12 changes: 9 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ const OPENSSL_NAMES = {

const SAMPLES = ["", "abc", "hello world", "zażółć gęślą jaźń 🦊"];

/** What a byte function adds when it gets text instead of bytes. */
const TEXT_HINT = ". Use TextEncoder, or create(name).hash(text)";

describe("@agntn/hashes", () => {
it("exports the package version", () => {
expect(version).toBe(pkg.version);
Expand Down Expand Up @@ -1602,7 +1605,7 @@ describe("byte functions", () => {
it("refuse anything but bytes instead of hashing it as something else", () => {
const loose = (value: unknown) => value as Uint8Array;
expect(() => sha256(loose("abc"))).toThrow(
new HashError("data must be a Uint8Array, not string"),
new HashError("data must be a Uint8Array, not string" + TEXT_HINT),
);
expect(() => keccak256(loose([1, 2, 3]))).toThrow(
new HashError("data must be a Uint8Array, not object"),
Expand All @@ -1611,11 +1614,14 @@ describe("byte functions", () => {
new HashError("state must be a Uint8Array, not object"),
);
expect(() => shake256(loose("abc"), 32)).toThrow(
new HashError("data must be a Uint8Array, not string"),
new HashError("data must be a Uint8Array, not string" + TEXT_HINT),
);
expect(() => blake256(loose(null))).toThrow(
new HashError("message must be a Uint8Array, not null"),
);
expect(() => blake256(loose("abc"))).toThrow(
new HashError("message must be a Uint8Array, not string" + TEXT_HINT),
);
expect(() => crc32(loose(new ArrayBuffer(4)))).toThrow(HashError);
expect(() => crc32IsoHdlc(loose(new ArrayBuffer(4)))).toThrow(HashError);
expect(() => crc32Bzip2(loose(new ArrayBuffer(4)))).toThrow(HashError);
Expand All @@ -1631,7 +1637,7 @@ describe("byte functions", () => {
new Sha3_256Hasher(),
]) {
expect(() => hasher.update(loose("abc"))).toThrow(
new HashError("data must be a Uint8Array, not string"),
new HashError("data must be a Uint8Array, not string" + TEXT_HINT),
);
}
expect(() => hmac(() => new Sha256Hasher(), loose("key"), new Uint8Array(0))).toThrow(
Expand Down
Loading