CodeSampleX

샘플

@noble/hashes 2.3.0: scryptAsync

검증된 샘플 — npm @noble/hashes 2.3.0: scryptAsync. node 22 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: scryptAsync derives a deterministic key from…

sha256:6257bf1db885e9114f90bd512063571c82aa2b4bfe59394f6ddd29f27027f5f9

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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.scryptAsync in pkg:npm/%40noble/hashes@2.3.0
패키지
심벌
  • @noble/hashes.scryptAsync
환경
node 22.23.2
생성일
2026-09-07T03:38:35Z

컨트랙트

  1. scryptAsync derives a deterministic key from string or byte password and salt matching RFC 7914 test vectors
  2. scryptAsync matches synchronous scrypt output for identical cost parameters and custom dkLen
  3. scryptAsync yields execution and invokes onProgress callback with fractional completion from 0 to 1
  4. scryptAsync respects asyncTick parameter for scheduler yielding without altering the output
  5. scryptAsync rejects when maxmem budget is insufficient for temporary memory allocations
  6. scryptAsync validates work factors and rejects invalid N, r, p, or dkLen parameters

파일

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

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:5260a137de1c96ba9180e76c1aaf095f0ad305ba5ba7e199001f07aba23a6c2a","contract":["scryptAsync derives a deterministic key from string or byte password and salt matching RFC 7914 test vectors","scryptAsync matches synchronous scrypt output for identical cost parameters and custom dkLen","scryptAsync yields execution and invokes onProgress callback with fractional completion from 0 to 1","scryptAsync respects asyncTick parameter for scheduler yielding without altering the output","scryptAsync rejects when maxmem budget is insufficient for temporary memory allocations","scryptAsync validates work factors and rejects invalid N, r, p, or dkLen parameters"],"goal":"verify @noble/hashes.scryptAsync in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.scryptAsync"]},"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.scryptAsync"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes-scrypt-async",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-scrypt-async",
      "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-scrypt-async",
  "version": "1.0.0",
  "type": "module",
  "description": "verify @noble/hashes.scryptAsync in pkg:npm/%40noble/hashes@2.3.0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@noble/hashes": "2.3.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.scryptAsync in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.scryptAsync"
  ]
}
src/index.mjs
import { scrypt, scryptAsync } from '@noble/hashes/scrypt.js';
import { bytesToHex, hexToBytes, utf8ToBytes } from '@noble/hashes/utils.js';

export { scrypt, scryptAsync, bytesToHex, hexToBytes, utf8ToBytes };

/**
 * Derives a key using Scrypt asynchronously.
 * @param {string|Uint8Array} password
 * @param {string|Uint8Array} salt
 * @param {import('@noble/hashes/scrypt.js').ScryptOpts} opts
 * @returns {Promise<Uint8Array>}
 */
export async function deriveScryptAsync(password, salt, opts) {
  return await scryptAsync(password, salt, opts);
}

/**
 * Derives a key with progress tracking using Scrypt asynchronously.
 * @param {string|Uint8Array} password
 * @param {string|Uint8Array} salt
 * @param {import('@noble/hashes/scrypt.js').ScryptOpts} opts
 * @param {(progress: number) => void} onProgress
 * @returns {Promise<Uint8Array>}
 */
export async function deriveScryptWithProgress(password, salt, opts, onProgress) {
  return await scryptAsync(password, salt, { ...opts, onProgress });
}
test/contract.mjs
import assert from 'node:assert';
import {
  scrypt,
  scryptAsync,
  bytesToHex,
  utf8ToBytes,
  deriveScryptAsync,
  deriveScryptWithProgress
} from '../src/index.mjs';

async function testRfcVectorAndInputTypes() {
  // RFC 7914 test vector: password='password', salt='salt', N=1024, r=8, p=1, dkLen=32
  const expectedHex = '16dbc8906763c7f048977a68f9d305f7710e068ca2cd95dab372125bb3f19608';

  const keyFromString = await scryptAsync('password', 'salt', { N: 1024, r: 8, p: 1, dkLen: 32 });
  assert.strictEqual(keyFromString.length, 32);
  assert.strictEqual(bytesToHex(keyFromString), expectedHex);

  const keyFromBytes = await scryptAsync(utf8ToBytes('password'), utf8ToBytes('salt'), {
    N: 1024,
    r: 8,
    p: 1,
    dkLen: 32
  });
  assert.strictEqual(bytesToHex(keyFromBytes), expectedHex);

  const keyFromHelper = await deriveScryptAsync('password', 'salt', {
    N: 1024,
    r: 8,
    p: 1,
    dkLen: 32
  });
  assert.strictEqual(bytesToHex(keyFromHelper), expectedHex);
}

async function testSyncEquivalenceAndDkLen() {
  const opts = { N: 16, r: 8, p: 1, dkLen: 64 };
  const asyncResult = await scryptAsync('passphrase', 'unique-salt', opts);
  const syncResult = scrypt('passphrase', 'unique-salt', opts);

  assert.strictEqual(asyncResult.length, 64);
  assert.strictEqual(syncResult.length, 64);
  assert.strictEqual(bytesToHex(asyncResult), bytesToHex(syncResult));
}

async function testProgressCallback() {
  const progressUpdates = [];
  const onProgress = (p) => {
    progressUpdates.push(p);
  };

  const key = await deriveScryptWithProgress('progress-pass', 'salt', { N: 32, r: 8, p: 1, dkLen: 32 }, onProgress);
  assert.strictEqual(key.length, 32);
  assert.ok(progressUpdates.length > 0, 'onProgress should be called at least once');

  for (let i = 0; i < progressUpdates.length; i++) {
    const val = progressUpdates[i];
    assert.ok(val >= 0 && val <= 1, `progress value ${val} must be within [0, 1]`);
    if (i > 0) {
      assert.ok(val >= progressUpdates[i - 1], 'progress values should be monotonically non-decreasing');
    }
  }
  assert.strictEqual(progressUpdates[progressUpdates.length - 1], 1, 'final progress must be 1');
}

async function testAsyncTickYielding() {
  const keyDefault = await scryptAsync('tick-test', 'salt', { N: 32, r: 8, p: 1, dkLen: 32 });
  const keyCustomTick = await scryptAsync('tick-test', 'salt', { N: 32, r: 8, p: 1, dkLen: 32, asyncTick: 1 });

  assert.strictEqual(bytesToHex(keyDefault), bytesToHex(keyCustomTick));
}

async function testMaxmemConstraint() {
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 1024, r: 8, p: 1, maxmem: 100 });
    },
    (err) => {
      assert.match(err.message, /"maxmem" limit was hit/);
      return true;
    }
  );
}

async function testInvalidParameters() {
  // N must be a power of 2
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 3, r: 8, p: 1 });
    },
    (err) => {
      assert.match(err.message, /"N" expected a power of 2/);
      return true;
    }
  );

  // r must be >= 1
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 16, r: 0, p: 1 });
    },
    (err) => {
      assert.match(err.message, /"r" expected integer >= 1/);
      return true;
    }
  );

  // p must be >= 1
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 16, r: 8, p: 0 });
    },
    (err) => {
      assert.match(err.message, /"p" expected integer/);
      return true;
    }
  );

  // dkLen must be >= 1
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 16, r: 8, p: 1, dkLen: 0 });
    },
    (err) => {
      assert.match(err.message, /"dkLen" expected integer/);
      return true;
    }
  );

  // onProgress must be a function if defined
  await assert.rejects(
    async () => {
      await scryptAsync('password', 'salt', { N: 16, r: 8, p: 1, onProgress: 'not-a-function' });
    },
    (err) => {
      assert.match(err.message, /"onProgress" must be a function/);
      return true;
    }
  );
}

async function runAll() {
  await testRfcVectorAndInputTypes();
  await testSyncEquivalenceAndDkLen();
  await testProgressCallback();
  await testAsyncTickYielding();
  await testMaxmemConstraint();
  await testInvalidParameters();

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

await runAll();

오리진 시더

익명