CodeSampleX

Пример

@noble/hashes 2.2.0

Проверенный пример — npm @noble/hashes 2.2.0. Контракт выполнен на node 22 · linux alpine/x64 · docker и пройден: sha256 computes standard SHA-256…

sha256:91a80f6f5df7e42240a09778e1178abf3dd83f486335926a1ea2246dc3b8c8cf

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
1
Заявленная среда linux 24 · ubuntu · glibc 2.39 x64 npm

Среды запусков проверки

Окружение Контракт Этапы Запуск
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-21

Кейс

HOW
Цель
verify pkg:npm/%40noble/hashes@2.2.0
Пакеты
Создан
2026-08-21T12:18:29Z

Контракт

  1. sha256 computes standard SHA-256 hexadecimal digests for strings and supports incremental streaming updates
  2. sha512, sha384, and sha224 produce expected cryptographic hash digests for byte inputs
  3. sha3_256 and keccak_256 compute distinct digests and shake128/shake256 support variable-length XOF output
  4. blake3 and blake2b compute fast cryptographic digests with optional custom output length
  5. computeHmacSha256 generates valid message authentication codes using SHA-256 and secret keys
  6. deriveHkdfKey, pbkdf2, and scrypt derive deterministic cryptographic keys with configurable iterations and work factors
  7. bytesToHex, hexToBytes, utf8ToBytes, concatBytes, and randomBytes convert and manipulate byte arrays correctly

Файлы

  • 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 pkg:npm/%40noble/hashes@2.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40noble/hashes@2.2.0

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:c68df4082999a4c7606cfca20a1b19e987c4d57f8b25ad377b5450bda1d3c303","contract":["sha256 computes standard SHA-256 hexadecimal digests for strings and supports incremental streaming updates","sha512, sha384, and sha224 produce expected cryptographic hash digests for byte inputs","sha3_256 and keccak_256 compute distinct digests and shake128/shake256 support variable-length XOF output","blake3 and blake2b compute fast cryptographic digests with optional custom output length","computeHmacSha256 generates valid message authentication codes using SHA-256 and secret keys","deriveHkdfKey, pbkdf2, and scrypt derive deterministic cryptographic keys with configurable iterations and work factors","bytesToHex, hexToBytes, utf8ToBytes, concatBytes, and randomBytes convert and manipulate byte arrays correctly"],"goal":"verify pkg:npm/%40noble/hashes@2.2.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.2.0"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40noble/hashes@2.2.0"],"schemaVersion":1,"subject":"pkg:npm/%40noble/hashes@2.2.0","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes",
      "version": "1.0.0",
      "dependencies": {
        "@noble/hashes": "2.2.0"
      }
    },
    "node_modules/@noble/hashes": {
      "version": "2.2.0",
      "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.2.0.tgz",
      "integrity": "sha512-IYqDGiTXab6FniAgnSdZwgWbomxpy9FtYvLKs7wCUs2a8RkITG+DFGO1DM9cr+E3/RgADRpFjrKVaJ1z6sjtEg==",
      "license": "MIT",
      "engines": {
        "node": ">= 20.19.0"
      },
      "funding": {
        "url": "https://paulmillr.com/funding/"
      }
    }
  }
}
package.json
{
  "name": "sample-noble-hashes",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "@noble/hashes": "2.2.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40noble/hashes@2.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.2.0"
  ]
}
src/index.mjs
import { sha256, sha512, sha384, sha224, sha512_256 } from '@noble/hashes/sha2.js';
import { sha3_256, sha3_512, keccak_256, shake128, shake256 } from '@noble/hashes/sha3.js';
import { blake3 } from '@noble/hashes/blake3.js';
import { blake2b, blake2s } from '@noble/hashes/blake2.js';
import { hmac } from '@noble/hashes/hmac.js';
import { hkdf } from '@noble/hashes/hkdf.js';
import { pbkdf2 } from '@noble/hashes/pbkdf2.js';
import { scrypt } from '@noble/hashes/scrypt.js';
import { bytesToHex, hexToBytes, utf8ToBytes, concatBytes, randomBytes } from '@noble/hashes/utils.js';

export {
  sha256,
  sha512,
  sha384,
  sha224,
  sha512_256,
  sha3_256,
  sha3_512,
  keccak_256,
  shake128,
  shake256,
  blake3,
  blake2b,
  blake2s,
  hmac,
  hkdf,
  pbkdf2,
  scrypt,
  bytesToHex,
  hexToBytes,
  utf8ToBytes,
  concatBytes,
  randomBytes
};

/**
 * Computes hexadecimal SHA-256 hash of a string or byte array.
 * @param {string|Uint8Array} input
 * @returns {string} Hex-encoded digest
 */
export function hashSha256(input) {
  const bytes = typeof input === 'string' ? utf8ToBytes(input) : input;
  return bytesToHex(sha256(bytes));
}

/**
 * Computes hexadecimal HMAC-SHA256 of a message with a key.
 * @param {string|Uint8Array} key
 * @param {string|Uint8Array} message
 * @returns {string} Hex-encoded MAC
 */
export function computeHmacSha256(key, message) {
  const keyBytes = typeof key === 'string' ? utf8ToBytes(key) : key;
  const msgBytes = typeof message === 'string' ? utf8ToBytes(message) : message;
  return bytesToHex(hmac(sha256, keyBytes, msgBytes));
}

/**
 * Computes hexadecimal BLAKE3 hash of a string or byte array.
 * @param {string|Uint8Array} input
 * @returns {string} Hex-encoded digest
 */
export function hashBlake3(input) {
  const bytes = typeof input === 'string' ? utf8ToBytes(input) : input;
  return bytesToHex(blake3(bytes));
}

/**
 * Derives a key using HKDF-SHA256.
 * @param {Uint8Array} ikm
 * @param {Uint8Array} salt
 * @param {Uint8Array} info
 * @param {number} length
 * @returns {Uint8Array}
 */
export function deriveHkdfKey(ikm, salt, info, length = 32) {
  return hkdf(sha256, ikm, salt, info, length);
}
test/contract.mjs
import assert from 'node:assert';
import {
  sha256,
  sha512,
  sha384,
  sha224,
  sha3_256,
  sha3_512,
  keccak_256,
  shake128,
  shake256,
  blake3,
  blake2b,
  blake2s,
  hmac,
  hkdf,
  pbkdf2,
  scrypt,
  bytesToHex,
  hexToBytes,
  utf8ToBytes,
  concatBytes,
  randomBytes,
  hashSha256,
  computeHmacSha256,
  hashBlake3,
  deriveHkdfKey
} from '../src/index.mjs';

// 1. SHA-256 computation and incremental hashing
const emptySha256 = sha256(utf8ToBytes(''));
assert.strictEqual(bytesToHex(emptySha256), 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
assert.strictEqual(hashSha256('abc'), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');

const incremental = sha256.create();
incremental.update(utf8ToBytes('a'));
incremental.update(utf8ToBytes('bc'));
assert.strictEqual(bytesToHex(incremental.digest()), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');

// 2. SHA-512, SHA-384, and SHA-224 digests
const sha512Hex = bytesToHex(sha512(utf8ToBytes('abc')));
assert.strictEqual(
  sha512Hex,
  'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f'
);

const sha384Hex = bytesToHex(sha384(utf8ToBytes('abc')));
assert.strictEqual(
  sha384Hex,
  'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7'
);

const sha224Hex = bytesToHex(sha224(utf8ToBytes('abc')));
assert.strictEqual(sha224Hex, '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7');

// 3. SHA3-256, Keccak-256, and SHAKE XOF
const sha3Hex = bytesToHex(sha3_256(utf8ToBytes('abc')));
assert.strictEqual(sha3Hex, '3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532');

const keccakHex = bytesToHex(keccak_256(utf8ToBytes('abc')));
assert.strictEqual(keccakHex, '4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45');
assert.notStrictEqual(sha3Hex, keccakHex);

const s128 = shake128(utf8ToBytes('abc'), { dkLen: 16 });
assert.strictEqual(s128.length, 16);
assert.strictEqual(bytesToHex(s128), '5881092dd818bf5cf8a3ddb793fbcba7');

const s256 = shake256(utf8ToBytes('abc'), { dkLen: 32 });
assert.strictEqual(s256.length, 32);
assert.strictEqual(bytesToHex(s256), '483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739');

// 4. BLAKE3 and BLAKE2b
assert.strictEqual(hashBlake3('abc'), '6437b3ac38465133ffb63b75273a8db548c558465d79db03fd359c6cd5bd9d85');

const b2b = blake2b(utf8ToBytes('abc'), { dkLen: 32 });
assert.strictEqual(b2b.length, 32);
assert.strictEqual(bytesToHex(b2b), 'bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319');

// 5. HMAC with SHA-256
const mac = computeHmacSha256('key', 'The quick brown fox jumps over the lazy dog');
assert.strictEqual(mac, 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8');

// 6. HKDF, PBKDF2, and Scrypt key derivations
const derivedHkdf = deriveHkdfKey(
  utf8ToBytes('secret-ikm'),
  utf8ToBytes('salt-123'),
  utf8ToBytes('info-label'),
  32
);
assert.strictEqual(derivedHkdf.length, 32);

const derivedPbkdf2 = pbkdf2(sha256, 'password', 'salt', { c: 1000, dkLen: 32 });
assert.strictEqual(derivedPbkdf2.length, 32);
assert.strictEqual(bytesToHex(derivedPbkdf2), '632c2812e46d4604102ba7618e9d6d7d2f8128f6266b4a03264d2a0460b7dcb3');

const derivedScrypt = scrypt(utf8ToBytes('password'), utf8ToBytes('salt'), { N: 1024, r: 8, p: 1, dkLen: 32 });
assert.strictEqual(derivedScrypt.length, 32);
assert.strictEqual(bytesToHex(derivedScrypt), '16dbc8906763c7f048977a68f9d305f7710e068ca2cd95dab372125bb3f19608');

// 7. Byte utilities
const decoded = hexToBytes('deadbeef');
assert.deepStrictEqual(decoded, new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
assert.strictEqual(bytesToHex(decoded), 'deadbeef');

const joined = concatBytes(new Uint8Array([1, 2]), new Uint8Array([3, 4]));
assert.deepStrictEqual(joined, new Uint8Array([1, 2, 3, 4]));

const random = randomBytes(16);
assert.strictEqual(random.length, 16);
assert.ok(random instanceof Uint8Array);

console.log('Contract tests passed successfully.');

Исходный сидер

аноним