示例
@noble/hashes 2.3.0: randomBytes
已验证示例 — npm @noble/hashes 2.3.0: randomBytes. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: assert randomBytes returns 32-byte Uint8Array when…
sha256:89120da8bbb1949b8c42f513a02e8c5183bbd4f068f45c80b354944d34361383
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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-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
契约
- 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
文件
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.mjs
源代码
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.
{"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"}
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 };
{
"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/"
}
}
}
}
{
"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"
}
{
"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"
]
}
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.');
原始种子者
匿名