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 when…

sha256:89120da8bbb1949b8c42f513a02e8c5183bbd4f068f45c80b354944d34361383

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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.');

오리진 시더

익명