CodeSampleX

サンプル

@noble/hashes 2.3.0: blake2b

検証済みサンプル — npm @noble/hashes 2.3.0: blake2b. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: blake2b computes standard 64-byte BLAKE2b…

sha256:ed9558a09077b96d507cbefc4eff04f9b266a73ed2847e6b6eb3afa8713ae44b

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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.blake2b in pkg:npm/%40noble/hashes@2.3.0
パッケージ
シンボル
  • @noble/hashes.blake2b
環境
node 22.23.2
作成日
2026-09-06T10:05:35Z

コントラクト

  1. blake2b computes standard 64-byte BLAKE2b cryptographic hash digests for byte inputs
  2. blake2b supports configurable output digest length via dkLen option from 1 to 64 bytes
  3. blake2b supports streaming incremental hashing with create, update, and digest
  4. blake2b supports keyed hashing in MAC mode with a secret key
  5. blake2b supports optional salt and personalization byte arrays
  6. blake2b instance clone preserves internal state for branch hashing
  7. blake2b digestInto writes the computed hash directly into a pre-allocated destination buffer

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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.blake2b 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.blake2b
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:e1cd7d2a3dfd8def72667cb2588be07f39bb5c58a196836548ae2bd8f1f3efa9","constraints":{"executionContext":"node"},"contract":["blake2b computes standard 64-byte BLAKE2b cryptographic hash digests for byte inputs","blake2b supports configurable output digest length via dkLen option from 1 to 64 bytes","blake2b supports streaming incremental hashing with create, update, and digest","blake2b supports keyed hashing in MAC mode with a secret key","blake2b supports optional salt and personalization byte arrays","blake2b instance clone preserves internal state for branch hashing","blake2b digestInto writes the computed hash directly into a pre-allocated destination buffer"],"goal":"verify @noble/hashes.blake2b in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.blake2b"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","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.blake2b"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes-blake2b",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-blake2b",
      "version": "1.0.0",
      "license": "MIT-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-blake2b",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "description": "verify @noble/hashes.blake2b in pkg:npm/%40noble/hashes@2.3.0",
  "main": "src/index.mjs",
  "license": "MIT-0",
  "dependencies": {
    "@noble/hashes": "2.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.blake2b in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.blake2b"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.mjs
import { blake2b } from '@noble/hashes/blake2.js';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';

export { blake2b, bytesToHex, utf8ToBytes };

/**
 * Computes BLAKE2b hash of an input (string or Uint8Array) and returns Uint8Array digest.
 * @param {string|Uint8Array} input
 * @param {import('@noble/hashes/blake2.js').Blake2Opts} [options]
 * @returns {Uint8Array}
 */
export function hashBlake2b(input, options) {
  const bytes = typeof input === 'string' ? utf8ToBytes(input) : input;
  return blake2b(bytes, options);
}

/**
 * Computes BLAKE2b hash of an input and returns hexadecimal string.
 * @param {string|Uint8Array} input
 * @param {import('@noble/hashes/blake2.js').Blake2Opts} [options]
 * @returns {string} Hex-encoded digest
 */
export function hashBlake2bHex(input, options) {
  return bytesToHex(hashBlake2b(input, options));
}

/**
 * Computes BLAKE2b MAC with a secret key.
 * @param {Uint8Array} key
 * @param {string|Uint8Array} message
 * @param {import('@noble/hashes/blake2.js').Blake2Opts} [options]
 * @returns {Uint8Array}
 */
export function computeBlake2bMac(key, message, options = {}) {
  const msgBytes = typeof message === 'string' ? utf8ToBytes(message) : message;
  return blake2b(msgBytes, { ...options, key });
}

/**
 * Creates an incremental BLAKE2b instance.
 * @param {import('@noble/hashes/blake2.js').Blake2Opts} [options]
 * @returns {ReturnType<typeof blake2b.create>}
 */
export function createBlake2b(options) {
  return blake2b.create(options);
}
test/contract.mjs
import assert from 'node:assert/strict';
import {
  blake2b,
  bytesToHex,
  utf8ToBytes,
  hashBlake2b,
  hashBlake2bHex,
  computeBlake2bMac,
  createBlake2b
} from '../src/index.mjs';

// 1. Export verification
assert.equal(typeof blake2b, 'function', 'blake2b must be a function');
assert.equal(blake2b.outputLen, 64, 'default outputLen must be 64 bytes');
assert.equal(blake2b.blockLen, 128, 'blockLen must be 128 bytes');

// 2. blake2b computes standard 64-byte BLAKE2b cryptographic hash digests for byte inputs
{
  const msg = utf8ToBytes('The quick brown fox jumps over the lazy dog');
  const digest = blake2b(msg);
  assert.equal(digest.length, 64, 'digest length must be 64 bytes');
  const expectedHex = 'a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918';
  assert.equal(bytesToHex(digest), expectedHex, 'digest must match standard BLAKE2b-512 test vector');
  assert.equal(hashBlake2bHex('The quick brown fox jumps over the lazy dog'), expectedHex);

  // Empty string vector
  const emptyDigest = blake2b(utf8ToBytes(''));
  assert.equal(
    bytesToHex(emptyDigest),
    '786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce',
    'empty input must match standard empty BLAKE2b digest'
  );
}

// 3. blake2b supports configurable output digest length via dkLen option from 1 to 64 bytes
{
  const msg = utf8ToBytes('The quick brown fox jumps over the lazy dog');
  const digest32 = blake2b(msg, { dkLen: 32 });
  assert.equal(digest32.length, 32, 'dkLen: 32 must produce 32-byte digest');
  assert.equal(
    bytesToHex(digest32),
    '01718cec35cd3d796dd00020e0bfecb473ad23457d063b75eff29c0ffa2e58a9',
    '32-byte digest must match expected BLAKE2b-256 test vector'
  );

  const digest16 = blake2b(msg, { dkLen: 16 });
  assert.equal(digest16.length, 16, 'dkLen: 16 must produce 16-byte digest');

  const digest1 = blake2b(msg, { dkLen: 1 });
  assert.equal(digest1.length, 1, 'dkLen: 1 must produce 1-byte digest');

  const digest64 = blake2b(msg, { dkLen: 64 });
  assert.equal(digest64.length, 64, 'dkLen: 64 must produce 64-byte digest');
  assert.deepEqual(digest64, blake2b(msg));
}

// 4. blake2b supports streaming incremental hashing with create, update, and digest
{
  const hasher = createBlake2b({ dkLen: 32 });
  hasher.update(utf8ToBytes('The quick brown '));
  hasher.update(utf8ToBytes('fox jumps over the lazy dog'));
  const incrementalDigest = hasher.digest();
  assert.equal(incrementalDigest.length, 32);
  assert.equal(
    bytesToHex(incrementalDigest),
    '01718cec35cd3d796dd00020e0bfecb473ad23457d063b75eff29c0ffa2e58a9',
    'incremental hashing must match one-shot hash result'
  );
}

// 5. blake2b supports keyed hashing in MAC mode with a secret key
{
  const msg = utf8ToBytes('The quick brown fox jumps over the lazy dog');
  const key = new Uint8Array(32).fill(0x5a);
  const mac1 = computeBlake2bMac(key, msg, { dkLen: 32 });
  const mac2 = blake2b(msg, { key, dkLen: 32 });
  assert.equal(mac1.length, 32);
  assert.deepEqual(mac1, mac2, 'computeBlake2bMac must match blake2b with key option');

  const unkeyed = blake2b(msg, { dkLen: 32 });
  assert.notDeepEqual(mac1, unkeyed, 'keyed MAC must differ from unkeyed digest');

  const diffKey = new Uint8Array(32).fill(0x5b);
  const macDiff = blake2b(msg, { key: diffKey, dkLen: 32 });
  assert.notDeepEqual(mac1, macDiff, 'different keys must produce different MACs');
}

// 6. blake2b supports optional salt and personalization byte arrays
{
  const msg = utf8ToBytes('The quick brown fox jumps over the lazy dog');
  const salt = new Uint8Array(16).fill(0x11);
  const personalization = new Uint8Array(16).fill(0x22);

  const salted = blake2b(msg, { dkLen: 32, salt, personalization });
  assert.equal(salted.length, 32);

  const regular = blake2b(msg, { dkLen: 32 });
  assert.notDeepEqual(salted, regular, 'salted/personalized digest must differ from default');

  const diffSalt = new Uint8Array(16).fill(0x12);
  const salted2 = blake2b(msg, { dkLen: 32, salt: diffSalt, personalization });
  assert.notDeepEqual(salted, salted2, 'different salts must produce different digests');
}

// 7. blake2b instance clone preserves internal state for branch hashing
{
  const prefix = utf8ToBytes('common-prefix-data-');
  const branchA = utf8ToBytes('branch-alpha');
  const branchB = utf8ToBytes('branch-beta');

  const original = blake2b.create({ dkLen: 32 });
  original.update(prefix);

  const cloned = original.clone();

  original.update(branchA);
  cloned.update(branchB);

  const digestA = original.digest();
  const digestB = cloned.digest();

  assert.notDeepEqual(digestA, digestB, 'branched hashes from clone must differ');

  // Verify that cloned prefix matches a fresh instance
  const freshA = blake2b.create({ dkLen: 32 });
  freshA.update(prefix);
  freshA.update(branchA);
  assert.deepEqual(digestA, freshA.digest(), 'cloned state must faithfully reproduce continuous hashing');
}

// 8. blake2b digestInto writes the computed hash directly into a pre-allocated destination buffer
{
  const msg = utf8ToBytes('The quick brown fox jumps over the lazy dog');
  const expectedDigest = blake2b(msg);

  const destBuffer = new Uint8Array(64);
  const hasher = blake2b.create();
  hasher.update(msg);
  hasher.digestInto(destBuffer);

  assert.deepEqual(destBuffer, expectedDigest, 'digestInto must write full digest into buffer');

  // Offset destination buffer with 4-byte aligned byteOffset
  const bigBuffer = new Uint8Array(100).fill(0xff);
  const view = bigBuffer.subarray(12, 76); // 12 is 4-byte aligned
  const hasher2 = blake2b.create();
  hasher2.update(msg);
  hasher2.digestInto(view);
  assert.deepEqual(view, expectedDigest, 'digestInto must write into 4-byte aligned subarray correctly');
  assert.equal(bigBuffer[11], 0xff, 'bytes outside destination slice must remain untouched');
  assert.equal(bigBuffer[76], 0xff, 'bytes outside destination slice must remain untouched');

  // Unaligned byteOffset throws RangeError
  const unalignedView = bigBuffer.subarray(10, 74);
  const hasher3 = blake2b.create();
  assert.throws(
    () => hasher3.digestInto(unalignedView),
    RangeError,
    'digestInto must reject non-4-byte aligned buffers with RangeError'
  );
}

console.log('Contract assertions passed.');

オリジンシーダー

匿名