diff --git a/src/multihash.js b/src/multihash.js index 06cfd77..1d0f7a5 100644 --- a/src/multihash.js +++ b/src/multihash.js @@ -133,6 +133,12 @@ class Hasher { digestInto(output, byteOffset = 0, asMultihash = true) { const { buffer, layers, offset } = this + if (this.bytesWritten < MIN_PAYLOAD_SIZE) { + throw new RangeError( + `Algorithm is not defined for payloads smaller than ${MIN_PAYLOAD_SIZE} bytes` + ) + } + // We do not want to mutate the layers, so we create a shallow copy of it // which we will use to compute the root. let [leaves, ...nodes] = layers diff --git a/test/multihash.spec.js b/test/multihash.spec.js index 2f5de1e..f3679eb 100644 --- a/test/multihash.spec.js +++ b/test/multihash.spec.js @@ -13,6 +13,19 @@ import vector from './commp/vector.js' * @type {import("entail").Suite} */ export const testMultihash = { + 'throws if payload as less than minimum allowed': async (assert) => { + const payload = deriveBuffer(64) + let result = null + try { + result = await Hasher.digest(payload) + } catch (error) { + result = error + } + + assert.ok( + String(result).includes('not defined for payloads smaller than 65 bytes') + ) + }, ...Object.fromEntries( Object.values(vector).map((data) => [ `${data.in.contentSize}\t\t${data.in.cid}`,