CodeSampleX

示例

@noble/hashes 2.3.0: shake128

已验证示例 — npm @noble/hashes 2.3.0: shake128. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: shake128 is exported as a callable function from…

sha256:e160f8aea29469510348e807c6711bc50788eb849b0863092d124f0f254e304e

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-10-11

案例

HOW
目标
verify @noble/hashes.shake128 in pkg:npm/%40noble/hashes@2.3.0
包
符号
  • @noble/hashes.shake128
环境
node 22.23.2
创建时间
2026-10-11T20:03:52Z

契约

  1. shake128 is exported as a callable function from @noble/hashes/sha3.js
  2. shake128 strictly requires Uint8Array inputs and throws TypeError on string inputs
  3. shake128 default output produces exact 16-byte digest matching standard test vectors for empty, ASCII, and multi-block byte inputs
  4. shake128 with dkLen option produces exact variable-length output matching NIST test vectors
  5. shake128 exposes outputLen as 16 bytes, blockLen as 168 bytes, and canXOF as true
  6. shake128.create supports incremental streaming hash updates via update and digest with Uint8Array inputs
  7. shake128 incremental hash instance supports xof to squeeze continuous variable-length output
  8. shake128 incremental hash instance supports clone to independently branch hash computation
  9. shake128 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array
  10. shake128 incremental hash instance supports destroy which cleans state and prevents further updates
  11. module wrapper helpers hashShake128, hashShake128Hex, and hashChunksHex accept string and Uint8Array inputs to compute correct digests

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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.shake128 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.shake128

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:ce38e879fa966d6b762bd87598b0e6dd0f1882bbb1e3cfed426c5565e97837fe","contract":["shake128 is exported as a callable function from @noble/hashes/sha3.js","shake128 strictly requires Uint8Array inputs and throws TypeError on string inputs","shake128 default output produces exact 16-byte digest matching standard test vectors for empty, ASCII, and multi-block byte inputs","shake128 with dkLen option produces exact variable-length output matching NIST test vectors","shake128 exposes outputLen as 16 bytes, blockLen as 168 bytes, and canXOF as true","shake128.create supports incremental streaming hash updates via update and digest with Uint8Array inputs","shake128 incremental hash instance supports xof to squeeze continuous variable-length output","shake128 incremental hash instance supports clone to independently branch hash computation","shake128 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array","shake128 incremental hash instance supports destroy which cleans state and prevents further updates","module wrapper helpers hashShake128, hashShake128Hex, and hashChunksHex accept string and Uint8Array inputs to compute correct digests"],"goal":"verify @noble/hashes.shake128 in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.shake128"]},"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.shake128"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes-shake128",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-shake128",
      "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-shake128",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "description": "Clean-room code sample for @noble/hashes shake128",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@noble/hashes": "2.3.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.shake128 in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.shake128"
  ]
}
src/index.js
import { shake128 } from '@noble/hashes/sha3.js';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';

/**
 * Normalizes input to Uint8Array, encoding strings as UTF-8.
 * In @noble/hashes v2, functions strictly require Uint8Array inputs.
 * @param {string | Uint8Array} input
 * @returns {Uint8Array}
 */
export function toBytes(input) {
  if (typeof input === 'string') {
    return utf8ToBytes(input);
  }
  return input;
}

/**
 * Computes the SHAKE128 hash of a string or Uint8Array and returns the raw Uint8Array digest.
 * @param {string | Uint8Array} input
 * @param {{ dkLen?: number }} [opts]
 * @returns {Uint8Array}
 */
export function hashShake128(input, opts) {
  return shake128(toBytes(input), opts);
}

/**
 * Computes the SHAKE128 hash of a string or Uint8Array and returns a lowercase hex string.
 * @param {string | Uint8Array} input
 * @param {{ dkLen?: number }} [opts]
 * @returns {string}
 */
export function hashShake128Hex(input, opts) {
  return bytesToHex(shake128(toBytes(input), opts));
}

/**
 * Creates an incremental SHAKE128 hasher.
 * @param {{ dkLen?: number }} [opts]
 * @returns {ReturnType<typeof shake128.create>}
 */
export function createShake128Hasher(opts) {
  return shake128.create(opts);
}

/**
 * Computes SHAKE128 hash over an array of sequential chunks.
 * @param {Array<string | Uint8Array>} chunks
 * @param {{ dkLen?: number }} [opts]
 * @returns {string}
 */
export function hashChunksHex(chunks, opts) {
  const hasher = shake128.create(opts);
  for (const chunk of chunks) {
    hasher.update(toBytes(chunk));
  }
  return bytesToHex(hasher.digest());
}

export { shake128, bytesToHex, utf8ToBytes };
test/contract.mjs
import assert from 'node:assert/strict';
import { shake128 as directShake128 } from '@noble/hashes/sha3.js';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';
import {
  shake128,
  toBytes,
  hashShake128,
  hashShake128Hex,
  createShake128Hasher,
  hashChunksHex,
} from '../src/index.js';

// 1. shake128 is exported as a callable function from @noble/hashes/sha3.js
assert.equal(typeof directShake128, 'function', 'shake128 must be a function');
assert.equal(typeof shake128, 'function', 're-exported shake128 must be a function');

// 2. shake128 strictly requires Uint8Array inputs and throws TypeError on string inputs
{
  assert.throws(
    () => directShake128('string-input'),
    {
      name: 'TypeError',
      message: /expected Uint8Array/,
    },
    'shake128 must throw TypeError when given a string'
  );
  assert.throws(
    () => {
      const hasher = directShake128.create();
      hasher.update('string-input');
    },
    {
      name: 'TypeError',
      message: /expected Uint8Array/,
    },
    'hasher.update must throw TypeError when given a string'
  );
}

// 3. shake128 default output produces exact 16-byte digest matching standard test vectors for empty, ASCII, and multi-block byte inputs
{
  const emptyBytes = new Uint8Array(0);
  const emptyDigest = directShake128(emptyBytes);
  assert.equal(emptyDigest.length, 16, 'Empty input digest length must be 16 bytes');
  assert.equal(
    bytesToHex(emptyDigest),
    '7f9c2ba4e88f827d616045507605853e',
    'Empty input digest must match SHAKE128 vector'
  );

  const abcBytes = utf8ToBytes('abc');
  const abcDigest = directShake128(abcBytes);
  assert.equal(abcDigest.length, 16, 'abc digest length must be 16 bytes');
  assert.equal(
    bytesToHex(abcDigest),
    '5881092dd818bf5cf8a3ddb793fbcba7',
    'abc digest must match SHAKE128 vector'
  );

  const foxMsg = 'The quick brown fox jumps over the lazy dog';
  const foxDigest = directShake128(utf8ToBytes(foxMsg));
  assert.equal(foxDigest.length, 16, 'fox digest length must be 16 bytes');
  assert.equal(
    bytesToHex(foxDigest),
    'f4202e3c5852f9182a0430fd8144f0a7',
    'fox message digest must match SHAKE128 vector'
  );
}

// 4. shake128 with dkLen option produces exact variable-length output matching NIST test vectors
{
  const empty32 = directShake128(new Uint8Array(0), { dkLen: 32 });
  assert.equal(empty32.length, 32, 'Empty input 32-byte digest length must be 32');
  assert.equal(
    bytesToHex(empty32),
    '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26',
    'Empty input 32-byte digest must match SHAKE128 vector'
  );

  const abc32 = directShake128(utf8ToBytes('abc'), { dkLen: 32 });
  assert.equal(abc32.length, 32, 'abc 32-byte digest length must be 32');
  assert.equal(
    bytesToHex(abc32),
    '5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8',
    'abc 32-byte digest must match SHAKE128 vector'
  );

  const fox32 = directShake128(utf8ToBytes('The quick brown fox jumps over the lazy dog'), { dkLen: 32 });
  assert.equal(fox32.length, 32, 'fox 32-byte digest length must be 32');
  assert.equal(
    bytesToHex(fox32),
    'f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e',
    'fox 32-byte digest must match SHAKE128 vector'
  );

  assert.deepEqual(abc32.subarray(0, 16), directShake128(utf8ToBytes('abc')));
}

// 5. shake128 exposes outputLen as 16 bytes, blockLen as 168 bytes, and canXOF as true
assert.equal(directShake128.outputLen, 16, 'shake128.outputLen must be 16 bytes');
assert.equal(directShake128.blockLen, 168, 'shake128.blockLen must be 168 bytes');
assert.equal(directShake128.canXOF, true, 'shake128.canXOF must be true');

// 6. shake128.create supports incremental streaming hash updates via update and digest with Uint8Array inputs
{
  const hasher = directShake128.create({ dkLen: 32 });
  hasher.update(utf8ToBytes('The quick '));
  hasher.update(utf8ToBytes('brown fox '));
  hasher.update(utf8ToBytes('jumps over the lazy dog'));
  const digestHex = bytesToHex(hasher.digest());
  assert.equal(
    digestHex,
    'f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e',
    'Incremental shake128 stream must match expected multi-part digest'
  );
  assert.throws(
    () => hasher.digest(),
    /already called/,
    'Calling digest after completion must throw'
  );
}

// 7. shake128 incremental hash instance supports xof to squeeze continuous variable-length output
{
  const hasher = directShake128.create();
  hasher.update(utf8ToBytes('abc'));
  const chunk1 = hasher.xof(16);
  const chunk2 = hasher.xof(16);
  assert.equal(chunk1.length, 16);
  assert.equal(chunk2.length, 16);
  assert.equal(
    bytesToHex(chunk1) + bytesToHex(chunk2),
    '5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8',
    'Sequential xof calls must stream continuous XOF output'
  );
}

// 8. shake128 incremental hash instance supports clone to independently branch hash computation
{
  const base = directShake128.create({ dkLen: 32 });
  base.update(utf8ToBytes('common prefix: '));
  const branchA = base.clone();
  const branchB = base.clone();
  branchA.update(utf8ToBytes('action_alpha'));
  branchB.update(utf8ToBytes('action_beta'));

  assert.equal(
    bytesToHex(branchA.digest()),
    bytesToHex(directShake128(utf8ToBytes('common prefix: action_alpha'), { dkLen: 32 })),
    'Cloned branch A must compute independent digest'
  );
  assert.equal(
    bytesToHex(branchB.digest()),
    bytesToHex(directShake128(utf8ToBytes('common prefix: action_beta'), { dkLen: 32 })),
    'Cloned branch B must compute independent digest'
  );
}

// 9. shake128 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array
{
  const outBuf = new Uint8Array(16);
  const hasher = directShake128.create();
  hasher.update(utf8ToBytes('abc'));
  hasher.digestInto(outBuf);
  assert.equal(
    bytesToHex(outBuf),
    '5881092dd818bf5cf8a3ddb793fbcba7',
    'digestInto must write digest into target Uint8Array buffer'
  );
}

// 10. shake128 incremental hash instance supports destroy which cleans state and prevents further updates
{
  const hasher = directShake128.create();
  hasher.update(utf8ToBytes('sensitive data'));
  hasher.destroy();
  assert.throws(
    () => hasher.update(utf8ToBytes('more data')),
    /destroyed/,
    'Calling update after destroy must throw'
  );
}

// 11. module wrapper helpers hashShake128, hashShake128Hex, and hashChunksHex accept string and Uint8Array inputs to compute correct digests
{
  const rawFromStr = hashShake128('abc');
  const rawFromBytes = hashShake128(utf8ToBytes('abc'));
  assert(rawFromStr instanceof Uint8Array, 'hashShake128 must return Uint8Array');
  assert.equal(rawFromStr.length, 16, 'hashShake128 length must be 16');
  assert.equal(bytesToHex(rawFromStr), bytesToHex(rawFromBytes), 'String and byte input must produce identical hash');

  assert.equal(
    hashShake128Hex('abc', { dkLen: 32 }),
    '5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8',
    'hashShake128Hex must return correct lowercase hex string'
  );

  const chunksHex = hashChunksHex(['The quick ', 'brown fox ', utf8ToBytes('jumps over the lazy dog')], { dkLen: 32 });
  assert.equal(
    chunksHex,
    'f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e',
    'hashChunksHex must match expected multi-part digest'
  );
}

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

原始种子者

匿名