CodeSampleX

サンプル

@noble/hashes 1.4.0: sha256

検証済みサンプル — npm @noble/hashes 1.4.0: sha256. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: sha256 is exported as a callable function from…

sha256:3a93765423b2dc25e24bdd1b0e0995db452040e4501ab03dcd9021b9ff129fbb

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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.sha256 in pkg:npm/%40noble/hashes@1.4.0
パッケージ
シンボル
  • @noble/hashes.sha256
環境
node 22.23.2
作成日
2026-09-06T13:20:31Z

コントラクト

  1. sha256 is exported as a callable function from @noble/hashes/sha256
  2. sha256 produces exact 32-byte digest matching standard NIST test vectors for empty, ASCII, and multi-block string inputs
  3. sha256 accepts both string and Uint8Array byte inputs yielding identical hash outputs
  4. sha256 exposes outputLen as 32 bytes and blockLen as 64 bytes
  5. sha256.create supports incremental streaming hash updates via update and digest
  6. sha256 incremental hash instance supports clone to independently branch hash computation
  7. sha256 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array
  8. sha256 incremental hash instance supports destroy to clean up internal buffer state
  9. module wrapper helpers hashSha256, hashSha256Hex, and hashChunksHex correctly compute 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.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:
  - @noble/hashes.sha256
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:05e782ea081dbafb68ffb8b587b7b8d6b5b428b80962a9cdcb038257fe11bcd9","constraints":{"executionContext":"node"},"contract":["sha256 is exported as a callable function from @noble/hashes/sha256","sha256 produces exact 32-byte digest matching standard NIST test vectors for empty, ASCII, and multi-block string inputs","sha256 accepts both string and Uint8Array byte inputs yielding identical hash outputs","sha256 exposes outputLen as 32 bytes and blockLen as 64 bytes","sha256.create supports incremental streaming hash updates via update and digest","sha256 incremental hash instance supports clone to independently branch hash computation","sha256 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array","sha256 incremental hash instance supports destroy to clean up internal buffer state","module wrapper helpers hashSha256, hashSha256Hex, and hashChunksHex correctly compute digests"],"goal":"verify @noble/hashes.sha256 in pkg:npm/%40noble/hashes@1.4.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@1.4.0"],"schemaVersion":1,"symbols":["@noble/hashes.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":["@noble/hashes.sha256"],"verifierAdapter":"node-typescript@1"}
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": "Clean-room code sample for @noble/hashes sha256",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@noble/hashes": "1.4.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.sha256 in pkg:npm/%40noble/hashes@1.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@1.4.0"
  ],
  "symbols": [
    "@noble/hashes.sha256"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const { sha256 } = require('@noble/hashes/sha256');
const { bytesToHex, utf8ToBytes } = require('@noble/hashes/utils');

/**
 * Computes the SHA-256 hash of a string or Uint8Array and returns the raw 32-byte 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 hash over an array of sequential chunks.
 * @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,
};
test/contract.mjs
import assert from 'node:assert/strict';
import { sha256 as directSha256 } from '@noble/hashes/sha256';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
import {
  sha256,
  hashSha256,
  hashSha256Hex,
  createSha256Hasher,
  hashChunksHex,
} from '../src/index.js';

// 1. sha256 is exported as a callable function from @noble/hashes/sha256
assert.equal(typeof directSha256, 'function', 'sha256 must be a function');
assert.equal(typeof sha256, 'function', 'module export sha256 must be a function');

// 2. sha256 produces exact 32-byte digest matching standard NIST test vectors for empty, ASCII, and multi-block string inputs
{
  const emptyHash = bytesToHex(sha256(''));
  assert.equal(
    emptyHash,
    'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
    'Empty input sha256 digest must match NIST vector'
  );
  assert.equal(sha256('').length, 32, 'Empty input digest byte length must be 32');

  const abcHash = bytesToHex(sha256('abc'));
  assert.equal(
    abcHash,
    'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
    'sha256("abc") digest must match NIST vector'
  );

  const multiBlockMsg = 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq';
  const multiBlockHash = bytesToHex(sha256(multiBlockMsg));
  assert.equal(
    multiBlockHash,
    '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
    'Multi-block message sha256 must match NIST vector'
  );
}

// 3. sha256 accepts both string and Uint8Array byte inputs yielding identical hash outputs
{
  const text = 'café 🚀 한글';
  const fromString = bytesToHex(sha256(text));
  const fromBytes = bytesToHex(sha256(utf8ToBytes(text)));
  assert.equal(fromString, fromBytes, 'String and Uint8Array utf8 inputs must produce identical digests');
}

// 4. sha256 exposes outputLen as 32 bytes and blockLen as 64 bytes
assert.equal(sha256.outputLen, 32, 'sha256.outputLen must be 32 bytes');
assert.equal(sha256.blockLen, 64, 'sha256.blockLen must be 64 bytes');

// 5. sha256.create supports incremental streaming hash updates via update and digest
{
  const hasher = createSha256Hasher();
  hasher.update(utf8ToBytes('abc'));
  hasher.update('dbcd');
  hasher.update(utf8ToBytes('ecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'));
  const digestHex = bytesToHex(hasher.digest());
  assert.equal(
    digestHex,
    '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
    'Incremental sha256 stream must match expected multi-block digest'
  );
}

// 6. sha256 incremental hash instance supports clone to independently branch hash computation
{
  const base = createSha256Hasher();
  base.update('common prefix: ');
  const branchA = base.clone();
  const branchB = base.clone();
  branchA.update('action_alpha');
  branchB.update('action_beta');

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

// 7. sha256 incremental hash instance supports digestInto to write digest into pre-allocated Uint8Array
{
  const outBuf = new Uint8Array(32);
  const hasher = createSha256Hasher();
  hasher.update('abc');
  hasher.digestInto(outBuf);
  assert.equal(
    bytesToHex(outBuf),
    'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
    'digestInto must write digest into target Uint8Array buffer'
  );
}

// 8. sha256 incremental hash instance supports destroy to clean up internal buffer state
{
  const hasher = createSha256Hasher();
  hasher.update('transient secret payload');
  hasher.destroy();
}

// 9. Module wrapper helpers hashSha256, hashSha256Hex, and hashChunksHex correctly compute digests
{
  const rawBytes = hashSha256('abc');
  assert(rawBytes instanceof Uint8Array, 'hashSha256 must return Uint8Array');
  assert.equal(rawBytes.length, 32, 'hashSha256 length must be 32');

  assert.equal(
    hashSha256Hex('abc'),
    'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
    'hashSha256Hex must return correct lowercase hex string'
  );

  const chunksHex = hashChunksHex(['abc', 'dbcd', 'ecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq']);
  assert.equal(
    chunksHex,
    '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
    'hashChunksHex must match expected multi-block digest'
  );
}

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

オリジンシーダー

匿名