CodeSampleX

サンプル

@noble/hashes 2.3.0: randomBytes

検証済みサンプル — npm @noble/hashes 2.3.0: randomBytes. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: assert randomBytes returns 32-byte Uint8Array…

sha256:89120da8bbb1949b8c42f513a02e8c5183bbd4f068f45c80b354944d34361383

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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-07

ケース

HOW
ゴール
verify @noble/hashes.randomBytes in pkg:npm/%40noble/hashes@2.3.0
パッケージ
シンボル
  • @noble/hashes.randomBytes
環境
node 22.23.2
作成日
2026-09-07T01:05:32Z

コントラクト

  1. assert randomBytes returns 32-byte Uint8Array when called without arguments
  2. assert randomBytes generates Uint8Array of requested byte length
  3. assert randomBytes generates empty 0-length Uint8Array when length is 0
  4. assert randomBytes supports maximum allowed buffer length of 65536 bytes
  5. assert consecutive randomBytes invocations produce distinct pseudorandom byte sequences
  6. assert randomBytes throws TypeError when bytesLength is not a number
  7. assert randomBytes throws RangeError when bytesLength is negative, non-integer, or NaN
  8. assert randomBytes throws RangeError when bytesLength exceeds 65536 quota limit
  9. assert helper wrapper functions generate bytes and hex strings accurately

ファイル

  • 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 @noble/hashes.randomBytes 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.randomBytes

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:9cb10a083ff5fe09f95fb688dbf791d5c1ab658f367397a8bbb518acc8b3873e","contract":["assert randomBytes returns 32-byte Uint8Array when called without arguments","assert randomBytes generates Uint8Array of requested byte length","assert randomBytes generates empty 0-length Uint8Array when length is 0","assert randomBytes supports maximum allowed buffer length of 65536 bytes","assert consecutive randomBytes invocations produce distinct pseudorandom byte sequences","assert randomBytes throws TypeError when bytesLength is not a number","assert randomBytes throws RangeError when bytesLength is negative, non-integer, or NaN","assert randomBytes throws RangeError when bytesLength exceeds 65536 quota limit","assert helper wrapper functions generate bytes and hex strings accurately"],"goal":"verify @noble/hashes.randomBytes in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.randomBytes"]},"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.randomBytes"],"verifierAdapter":"node-typescript@1"}
index.js
import { randomBytes } from '@noble/hashes/utils.js';

/**
 * Generates a cryptographically secure random byte buffer of specified length.
 * @param {number} [bytesLength=32] - Number of random bytes to generate (0 <= bytesLength <= 65536)
 * @returns {Uint8Array}
 */
export function generateRandomBytes(bytesLength = 32) {
  return randomBytes(bytesLength);
}

/**
 * Generates a random byte buffer and returns it as a hexadecimal string.
 * @param {number} [bytesLength=32]
 * @returns {string}
 */
export function generateRandomHex(bytesLength = 32) {
  const bytes = randomBytes(bytesLength);
  return Array.from(bytes)
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('');
}

/**
 * Validates whether the given length is valid for randomBytes.
 * @param {unknown} bytesLength
 * @returns {boolean}
 */
export function isValidRandomBytesLength(bytesLength) {
  if (typeof bytesLength !== 'number' || !Number.isSafeInteger(bytesLength)) {
    return false;
  }
  return bytesLength >= 0 && bytesLength <= 65536;
}

export { randomBytes };
package-lock.json
{
  "name": "sample-noble-hashes-randombytes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-randombytes",
      "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-randombytes",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "description": "verify @noble/hashes.randomBytes in pkg:npm/%40noble/hashes@2.3.0",
  "main": "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.randomBytes in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.randomBytes"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { randomBytes } from '@noble/hashes/utils.js';
import {
  generateRandomBytes,
  generateRandomHex,
  isValidRandomBytesLength,
} from '../index.js';

// 1. Returns 32-byte Uint8Array when called without arguments (default length 32)
const defaultBytes = randomBytes();
assert(defaultBytes instanceof Uint8Array, 'randomBytes must return Uint8Array');
assert.strictEqual(defaultBytes.length, 32, 'Default randomBytes length must be 32');

// 2. Generates Uint8Array of requested byte length
const customBytes = randomBytes(16);
assert(customBytes instanceof Uint8Array, 'randomBytes(16) must return Uint8Array');
assert.strictEqual(customBytes.length, 16, 'randomBytes(16) length must be 16');

const singleByte = randomBytes(1);
assert.strictEqual(singleByte.length, 1, 'randomBytes(1) length must be 1');

// 3. Generates empty 0-length Uint8Array when length is 0
const emptyBytes = randomBytes(0);
assert(emptyBytes instanceof Uint8Array, 'randomBytes(0) must return Uint8Array');
assert.strictEqual(emptyBytes.length, 0, 'randomBytes(0) length must be 0');

// 4. Supports maximum allowed buffer length of 65536 bytes
const maxBytes = randomBytes(65536);
assert(maxBytes instanceof Uint8Array, 'randomBytes(65536) must return Uint8Array');
assert.strictEqual(maxBytes.length, 65536, 'randomBytes(65536) length must be 65536');

// 5. Consecutive randomBytes invocations produce distinct pseudorandom byte sequences
const sampleA = randomBytes(32);
const sampleB = randomBytes(32);
assert.notDeepStrictEqual(
  Array.from(sampleA),
  Array.from(sampleB),
  'Consecutive randomBytes(32) calls must not produce identical outputs'
);

// 6. Throws TypeError when bytesLength is not a number
assert.throws(
  () => randomBytes('16'),
  {
    name: 'TypeError',
    message: '"bytesLength" expected number, got string',
  }
);
assert.throws(
  () => randomBytes(null),
  {
    name: 'TypeError',
    message: '"bytesLength" expected number, got object',
  }
);
assert.throws(
  () => randomBytes(true),
  {
    name: 'TypeError',
    message: '"bytesLength" expected number, got boolean',
  }
);

// 7. Throws RangeError when bytesLength is negative, non-integer, or NaN
assert.throws(
  () => randomBytes(-1),
  {
    name: 'RangeError',
    message: '"bytesLength" expected integer >= 0, got -1',
  }
);
assert.throws(
  () => randomBytes(1.5),
  {
    name: 'RangeError',
    message: '"bytesLength" expected integer >= 0, got 1.5',
  }
);
assert.throws(
  () => randomBytes(NaN),
  {
    name: 'RangeError',
    message: '"bytesLength" expected integer >= 0, got NaN',
  }
);

// 8. Throws RangeError when bytesLength exceeds 65536 quota limit
assert.throws(
  () => randomBytes(65537),
  {
    name: 'RangeError',
    message: '"bytesLength" expected <= 65536, got 65537',
  }
);
assert.throws(
  () => randomBytes(100000),
  {
    name: 'RangeError',
    message: '"bytesLength" expected <= 65536, got 100000',
  }
);

// 9. Helper wrapper functions generate bytes and hex strings accurately
const helperBytes = generateRandomBytes(24);
assert(helperBytes instanceof Uint8Array, 'generateRandomBytes must return Uint8Array');
assert.strictEqual(helperBytes.length, 24, 'generateRandomBytes(24) length must be 24');

const hexString = generateRandomHex(16);
assert.strictEqual(typeof hexString, 'string', 'generateRandomHex must return a string');
assert.strictEqual(hexString.length, 32, '16 random bytes in hex representation must have 32 characters');
assert(/^[0-9a-f]{32}$/.test(hexString), 'generateRandomHex must return valid lowercase hex characters');

assert.strictEqual(isValidRandomBytesLength(32), true, 'isValidRandomBytesLength(32) must be true');
assert.strictEqual(isValidRandomBytesLength(0), true, 'isValidRandomBytesLength(0) must be true');
assert.strictEqual(isValidRandomBytesLength(65536), true, 'isValidRandomBytesLength(65536) must be true');
assert.strictEqual(isValidRandomBytesLength(-1), false, 'isValidRandomBytesLength(-1) must be false');
assert.strictEqual(isValidRandomBytesLength(65537), false, 'isValidRandomBytesLength(65537) must be false');
assert.strictEqual(isValidRandomBytesLength('32'), false, 'isValidRandomBytesLength("32") must be false');
assert.strictEqual(isValidRandomBytesLength(1.5), false, 'isValidRandomBytesLength(1.5) must be false');

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

オリジンシーダー

匿名