CodeSampleX

示例

@noble/hashes 2.3.0: blake3

已验证示例 — npm @noble/hashes 2.3.0: blake3. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: blake3 exports function with outputLen 32 blockLen 64 canXOF…

sha256:8346a625e237e65c4e0e638eff7285eca78b1df76ec02f5a556d30e0c543132a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

案例

HOW
目标
verify @noble/hashes.blake3 in pkg:npm/%40noble/hashes@2.3.0
包
符号
  • @noble/hashes.blake3
环境
node 22.23.2
创建时间
2026-09-06T12:13:40Z

契约

  1. blake3 exports function with outputLen 32 blockLen 64 canXOF true and create function
  2. blake3 computes standard 32-byte hash from Uint8Array matching official BLAKE3 test vectors
  3. blake3 supports incremental hashing via create update and digest yielding identical output to one-shot
  4. blake3 supports optional 32-byte key for keyed MAC computation
  5. blake3 supports optional context for key derivation function mode
  6. blake3 supports extendable output via dkLen option and xof method
  7. blake3 throws TypeError or Error when input is not Uint8Array or when key length is invalid or when key and context are both provided

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify @noble/hashes.blake3 in pkg:npm/%40noble/hashes@2.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40noble/hashes@2.3.0
Demonstrate these symbols/APIs:
  - @noble/hashes.blake3

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:62642f990d40e22d73d135380dde01b28d3b88c5cf09218355c3a31e352a4f07","contract":["blake3 exports function with outputLen 32 blockLen 64 canXOF true and create function","blake3 computes standard 32-byte hash from Uint8Array matching official BLAKE3 test vectors","blake3 supports incremental hashing via create update and digest yielding identical output to one-shot","blake3 supports optional 32-byte key for keyed MAC computation","blake3 supports optional context for key derivation function mode","blake3 supports extendable output via dkLen option and xof method","blake3 throws TypeError or Error when input is not Uint8Array or when key length is invalid or when key and context are both provided"],"goal":"verify @noble/hashes.blake3 in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.blake3"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"subject":"pkg:npm/%40noble/hashes@2.3.0","symbols":["@noble/hashes.blake3"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { blake3 } from '@noble/hashes/blake3.js';

/**
 * Computes BLAKE3 hash of the given byte array.
 *
 * @param {Uint8Array} data
 * @param {object} [opts]
 * @returns {Uint8Array}
 */
export function hashBlake3(data, opts) {
  return blake3(data, opts);
}

/**
 * Creates an incremental BLAKE3 hasher instance.
 *
 * @param {object} [opts]
 * @returns {object}
 */
export function createBlake3(opts) {
  return blake3.create(opts);
}

export { blake3 };
export default blake3;
package-lock.json
{
  "name": "sample-noble-hashes-blake3",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-blake3",
      "version": "1.0.0",
      "dependencies": {
        "@noble/hashes": "2.3.0"
      }
    },
    "node_modules/@noble/hashes": {
      "version": "2.3.0",
      "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.3.0.tgz",
      "integrity": "sha512-oN+QwyX7VSHotibwubG3kpzbwKrfnyR6OOO+3Nk/53ADL7FmgHHz4TgrbaYKvvOw09u6QTx0oiH1cNCIOuN0CQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 20.19.0"
      },
      "funding": {
        "url": "https://paulmillr.com/funding/"
      }
    }
  }
}
package.json
{
  "name": "sample-noble-hashes-blake3",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "dependencies": {
    "@noble/hashes": "2.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.blake3 in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.blake3"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { blake3, hashBlake3, createBlake3 } from '../index.mjs';

function toHex(bytes) {
  return Buffer.from(bytes).toString('hex');
}

// 1. blake3 exports function with outputLen 32 blockLen 64 canXOF true and create function
{
  assert.strictEqual(typeof blake3, 'function', 'blake3 should be a function');
  assert.strictEqual(blake3.outputLen, 32, 'outputLen should be 32');
  assert.strictEqual(blake3.blockLen, 64, 'blockLen should be 64');
  assert.strictEqual(blake3.canXOF, true, 'canXOF should be true');
  assert.strictEqual(typeof blake3.create, 'function', 'blake3.create should be a function');
  assert.strictEqual(typeof hashBlake3, 'function', 'hashBlake3 helper should be a function');
  assert.strictEqual(typeof createBlake3, 'function', 'createBlake3 helper should be a function');
}

// 2. blake3 computes standard 32-byte hash from Uint8Array matching official BLAKE3 test vectors
{
  const emptyHash = blake3(new Uint8Array(0));
  assert.strictEqual(emptyHash instanceof Uint8Array, true);
  assert.strictEqual(emptyHash.length, 32);
  assert.strictEqual(
    toHex(emptyHash),
    'af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262',
    'empty input hash must match official BLAKE3 vector'
  );

  const abc = new Uint8Array([0x61, 0x62, 0x63]);
  const abcHash = hashBlake3(abc);
  assert.strictEqual(abcHash.length, 32);
  assert.strictEqual(
    toHex(abcHash),
    '6437b3ac38465133ffb63b75273a8db548c558465d79db03fd359c6cd5bd9d85',
    'abc hash must match official BLAKE3 vector'
  );
}

// 3. blake3 supports incremental hashing via create update and digest yielding identical output to one-shot
{
  const part1 = new Uint8Array([1, 2, 3, 4, 5]);
  const part2 = new Uint8Array([6, 7, 8, 9, 10]);
  const full = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

  const hasher = createBlake3();
  hasher.update(part1);
  hasher.update(part2);
  const digest = hasher.digest();

  const oneShot = blake3(full);
  assert.deepStrictEqual(digest, oneShot, 'incremental hash must equal one-shot hash');
}

// 4. blake3 supports optional 32-byte key for keyed MAC computation
{
  const msg = new Uint8Array([0x61, 0x62, 0x63]);
  const key = new Uint8Array(32).fill(0x42);

  const keyed = blake3(msg, { key });
  const unkeyed = blake3(msg);

  assert.strictEqual(keyed.length, 32);
  assert.notDeepStrictEqual(keyed, unkeyed, 'keyed hash must differ from unkeyed hash');

  const keyedHasher = blake3.create({ key });
  keyedHasher.update(msg);
  assert.deepStrictEqual(keyedHasher.digest(), keyed, 'incremental keyed hash must match one-shot keyed hash');
}

// 5. blake3 supports optional context for key derivation function mode
{
  const msg = new Uint8Array([0x61, 0x62, 0x63]);
  const context = new Uint8Array([0x61, 0x70, 0x70]);

  const kdf = blake3(msg, { context });
  const unkeyed = blake3(msg);

  assert.strictEqual(kdf.length, 32);
  assert.notDeepStrictEqual(kdf, unkeyed, 'derived key must differ from default hash');
}

// 6. blake3 supports extendable output via dkLen option and xof method
{
  const msg = new Uint8Array([0x61, 0x62, 0x63]);
  const abcHash = blake3(msg);

  for (const dkLen of [16, 32, 64, 128]) {
    const out = blake3(msg, { dkLen });
    assert.strictEqual(out.length, dkLen, `digest length must match dkLen ${dkLen}`);
  }

  const xof64 = blake3(msg, { dkLen: 64 });
  assert.strictEqual(xof64.length, 64);
  assert.deepStrictEqual(xof64.subarray(0, 32), abcHash, 'first 32 bytes must match default digest');

  const hasher = blake3.create();
  hasher.update(msg);
  const xofOut = hasher.xof(64);
  assert.deepStrictEqual(xofOut, xof64, 'hasher xof must match one-shot xof output');
}

// 7. blake3 throws TypeError or Error when input is not Uint8Array or when key length is invalid or when key and context are both provided
{
  const msg = new Uint8Array([0x61, 0x62, 0x63]);
  const key = new Uint8Array(32).fill(0x42);
  const context = new Uint8Array([0x61, 0x70, 0x70]);

  assert.throws(
    () => blake3('not bytes'),
    /Uint8Array/i,
    'should throw when input is not Uint8Array'
  );

  assert.throws(
    () => blake3([1, 2, 3]),
    /Uint8Array/i,
    'should throw when input is an array'
  );

  assert.throws(
    () => blake3(msg, { key: new Uint8Array(16) }),
    /key/i,
    'should throw when key length is not 32 bytes'
  );

  assert.throws(
    () => blake3(msg, { key, context }),
    /cannot use both "key" and "context"/i,
    'should throw when both key and context are provided'
  );
}

console.log('All @noble/hashes.blake3 contract assertions passed.');

原始种子者

匿名