CodeSampleX

示例

@noble/hashes 1.4.0: sha256

已验证示例 — npm @noble/hashes 1.4.0: sha256. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: assert sha256 produces exact 32-byte hash matching NIST…

sha256:9a1431fc71f0e3fce5cae7dafe1a9aaf1300e4114af2b80604a3cb9cb590b57c

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

案例

HOW
目标
verify sha256 in pkg:npm/%40noble/hashes@1.4.0
包
符号
  • sha256
环境
node 22.23.2
创建时间
2026-09-01T19:54:45Z

契约

  1. assert sha256 produces exact 32-byte hash matching NIST standard test vectors for empty, single-block, and multi-block inputs
  2. assert sha256 accepts both UTF-8 string and Uint8Array byte inputs interchangeably
  3. assert sha256 exposes outputLen as 32 bytes and blockLen as 64 bytes
  4. assert sha256.create supports incremental streaming hash updates via update and digest
  5. assert sha256 incremental hash instance supports clone to fork hash state into independent streams
  6. assert sha256 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array
  7. assert sha256 incremental hash instance supports destroy to clean up internal buffer state

文件

  • PROMPT.md
  • csx.json
  • index.js
  • 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 sha256 in pkg:npm/%40noble/hashes@1.4.0
Kind: HOW

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

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:a3197134058ab315b18f1a6326e44b167f02105b00e9c6e931ebd1bcef7d61ce","contract":["assert sha256 produces exact 32-byte hash matching NIST standard test vectors for empty, single-block, and multi-block inputs","assert sha256 accepts both UTF-8 string and Uint8Array byte inputs interchangeably","assert sha256 exposes outputLen as 32 bytes and blockLen as 64 bytes","assert sha256.create supports incremental streaming hash updates via update and digest","assert sha256 incremental hash instance supports clone to fork hash state into independent streams","assert sha256 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array","assert sha256 incremental hash instance supports destroy to clean up internal buffer state"],"goal":"verify sha256 in pkg:npm/%40noble/hashes@1.4.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@1.4.0"],"schemaVersion":1,"symbols":["sha256"]},"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@1.4.0"],"schemaVersion":1,"subject":"pkg:npm/%40noble/hashes@1.4.0","symbols":["sha256"],"verifierAdapter":"node-typescript@1"}
index.js
const { sha256 } = require('@noble/hashes/sha256');
const { bytesToHex, utf8ToBytes, toBytes } = require('@noble/hashes/utils');

/**
 * Computes the SHA-256 hash of a string or Uint8Array and returns the raw Uint8Array digest.
 * @param {string | Uint8Array} input
 * @returns {Uint8Array}
 */
function hashSha256(input) {
  return sha256(input);
}

/**
 * Computes the SHA-256 hash of a string or Uint8Array and returns a lowercase hex string.
 * @param {string | Uint8Array} input
 * @returns {string}
 */
function hashSha256Hex(input) {
  return bytesToHex(sha256(input));
}

/**
 * Creates an incremental SHA-256 hasher.
 * @returns {import('@noble/hashes/utils').Hash<any>}
 */
function createSha256Hasher() {
  return sha256.create();
}

/**
 * Computes SHA-256 over multiple chunks in sequence and returns hex digest.
 * @param {Array<string | Uint8Array>} chunks
 * @returns {string}
 */
function hashChunksHex(chunks) {
  const hasher = sha256.create();
  for (const chunk of chunks) {
    hasher.update(typeof chunk === 'string' ? utf8ToBytes(chunk) : chunk);
  }
  return bytesToHex(hasher.digest());
}

module.exports = {
  sha256,
  hashSha256,
  hashSha256Hex,
  createSha256Hasher,
  hashChunksHex,
  bytesToHex,
  utf8ToBytes,
  toBytes,
};
package-lock.json
{
  "name": "sample-noble-hashes-sha256",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-sha256",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@noble/hashes": "1.4.0"
      }
    },
    "node_modules/@noble/hashes": {
      "version": "1.4.0",
      "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz",
      "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==",
      "license": "MIT",
      "engines": {
        "node": ">= 16"
      },
      "funding": {
        "url": "https://paulmillr.com/funding/"
      }
    }
  }
}
package.json
{
  "name": "sample-noble-hashes-sha256",
  "version": "1.0.0",
  "private": true,
  "description": "verify sha256 in pkg:npm/%40noble/hashes@1.4.0",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@noble/hashes": "1.4.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify sha256 in pkg:npm/%40noble/hashes@1.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@1.4.0"
  ],
  "symbols": [
    "sha256"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
import {
  hashSha256,
  hashSha256Hex,
  createSha256Hasher,
  hashChunksHex,
} from '../index.js';

// 1. One-shot hashing of empty input matches standard NIST vector
const emptyHashBytes = sha256('');
const emptyHashHex = bytesToHex(emptyHashBytes);
assert.strictEqual(
  emptyHashHex,
  'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
  'Empty string sha256 must match standard NIST digest'
);
assert.strictEqual(emptyHashBytes.length, 32, 'Digest byte length must be 32');

// 2. One-shot hashing with known test vectors (string and Uint8Array)
const abcHashHex = bytesToHex(sha256('abc'));
const abcHashFromBytes = bytesToHex(sha256(utf8ToBytes('abc')));
assert.strictEqual(
  abcHashHex,
  'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  'sha256("abc") must match standard test vector'
);
assert.strictEqual(
  abcHashHex,
  abcHashFromBytes,
  'String and Uint8Array utf8 byte inputs must produce identical digests'
);

// 3. Multi-block message hash verification
const multiBlockMsg = 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq';
const multiBlockHash = bytesToHex(sha256(multiBlockMsg));
assert.strictEqual(
  multiBlockHash,
  '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  'Multi-block message sha256 must match standard NIST digest'
);

// 4. Algorithm properties: outputLen and blockLen
assert.strictEqual(sha256.outputLen, 32, 'sha256.outputLen must be 32 bytes');
assert.strictEqual(sha256.blockLen, 64, 'sha256.blockLen must be 64 bytes');

// 5. Incremental streaming updates with sha256.create()
const streamHasher = sha256.create();
streamHasher.update(utf8ToBytes('abc'));
streamHasher.update('dbcd');
streamHasher.update(utf8ToBytes('ecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'));
const streamDigest = bytesToHex(streamHasher.digest());
assert.strictEqual(
  streamDigest,
  '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  'Incremental sha256.create() stream must match one-shot hash'
);

// 6. Cloning hash state mid-stream to branch computations
const baseHasher = sha256.create();
baseHasher.update('common prefix: ');
const branchA = baseHasher.clone();
const branchB = baseHasher.clone();
branchA.update('action_alpha');
branchB.update('action_beta');

assert.strictEqual(
  bytesToHex(branchA.digest()),
  bytesToHex(sha256('common prefix: action_alpha')),
  'Cloned branch A must compute independent digest'
);
assert.strictEqual(
  bytesToHex(branchB.digest()),
  bytesToHex(sha256('common prefix: action_beta')),
  'Cloned branch B must compute independent digest'
);

// 7. digestInto pre-allocated Uint8Array buffer
const outputBuffer = new Uint8Array(32);
const directHasher = sha256.create();
directHasher.update('abc');
directHasher.digestInto(outputBuffer);
assert.strictEqual(
  bytesToHex(outputBuffer),
  'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  'digestInto must write digest into target buffer'
);

// 8. State cleanup with destroy()
const destroyHasher = sha256.create();
destroyHasher.update('transient data');
destroyHasher.destroy();

// 9. Module wrapper helpers
assert.strictEqual(
  hashSha256Hex('abc'),
  'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  'hashSha256Hex helper must match expected hex digest'
);
assert.strictEqual(
  hashChunksHex(['abc', 'dbcd', 'ecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq']),
  '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  'hashChunksHex helper must correctly stream array of chunks'
);

console.log('All sha256 contract assertions passed.');

原始种子者

匿名