Skip to content

Commit

Permalink
impose a min payload limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jul 19, 2023
1 parent a7a7516 commit c7d126d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/multihash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/multihash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down

0 comments on commit c7d126d

Please sign in to comment.