CodeSampleX

示例

crc-32 1.2.2: compute CRC32 checksum for strings and buffers using crc-32

已验证示例 — npm crc-32 1.2.2: compute CRC32 checksum for strings and buffers using crc-32. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.

sha256:87b90caeeaec71c7c6a8217f509f874ef902f909d3cdff3cc76073993d850b54

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-28

案例

HOW
目标
compute CRC32 checksum for strings and buffers using crc-32
包
符号
  • CRC32.bstr
  • CRC32.buf
  • CRC32.str
创建时间
2026-08-28T08:13:17Z

契约

  1. CRC32.str(str) computes 32-bit signed integer CRC-32 checksum for ASCII and UTF-8 strings
  2. CRC32.str('') returns 0 for empty string input
  3. CRC32.buf(buf) computes CRC-32 checksum for Buffer and Uint8Array byte sequences
  4. CRC32.bstr(binaryStr) computes CRC-32 checksum for 8-bit binary strings matching buffer CRC
  5. CRC32.str(part2, seed) and CRC32.buf(part2, seed) support incremental CRC-32 chaining with seed

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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: compute CRC32 checksum for strings and buffers using crc-32
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/crc-32@1.2.2
Demonstrate these symbols/APIs:
  - CRC32.bstr
  - CRC32.buf
  - CRC32.str

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:26527bb7866278813502aecb35dfcb41356fd2401c488e950cb741f18396f86c","contract":["CRC32.str(str) computes 32-bit signed integer CRC-32 checksum for ASCII and UTF-8 strings","CRC32.str('') returns 0 for empty string input","CRC32.buf(buf) computes CRC-32 checksum for Buffer and Uint8Array byte sequences","CRC32.bstr(binaryStr) computes CRC-32 checksum for 8-bit binary strings matching buffer CRC","CRC32.str(part2, seed) and CRC32.buf(part2, seed) support incremental CRC-32 chaining with seed"],"goal":"compute CRC32 checksum for strings and buffers using crc-32","kind":"HOW","packages":["pkg:npm/crc-32@1.2.2"],"schemaVersion":1,"symbols":["CRC32.bstr","CRC32.buf","CRC32.str"]},"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/crc-32@1.2.2"],"schemaVersion":1,"subject":"pkg:npm/crc-32@1.2.2","symbols":["CRC32.bstr","CRC32.buf","CRC32.str"],"verifierAdapter":"node-typescript@1"}
index.mjs
import CRC32 from 'crc-32';

/**
 * Computes CRC-32 checksum of a UTF-8 string.
 * @param {string} str - The string to compute checksum for.
 * @param {number} [seed] - Optional starting CRC value for incremental computation.
 * @returns {number} 32-bit signed integer CRC-32 checksum.
 */
export function checksumString(str, seed) {
  return CRC32.str(str, seed);
}

/**
 * Computes CRC-32 checksum of a binary Buffer or Uint8Array.
 * @param {Uint8Array|Buffer|number[]} buf - The byte buffer.
 * @param {number} [seed] - Optional starting CRC value for incremental computation.
 * @returns {number} 32-bit signed integer CRC-32 checksum.
 */
export function checksumBuffer(buf, seed) {
  return CRC32.buf(buf, seed);
}

/**
 * Computes CRC-32 checksum of an 8-bit binary string.
 * @param {string} bstr - Binary string of 8-bit character codes (0-255).
 * @param {number} [seed] - Optional starting CRC value for incremental computation.
 * @returns {number} 32-bit signed integer CRC-32 checksum.
 */
export function checksumBinaryString(bstr, seed) {
  return CRC32.bstr(bstr, seed);
}

/**
 * Converts signed 32-bit CRC integer to unsigned 32-bit representation.
 * @param {number} crc - 32-bit signed integer CRC.
 * @returns {number} 32-bit unsigned integer CRC.
 */
export function toUnsignedCrc32(crc) {
  return crc >>> 0;
}

/**
 * Formats CRC integer as 8-character zero-padded lowercase hexadecimal string.
 * @param {number} crc - 32-bit signed or unsigned integer CRC.
 * @returns {string} 8-character hex string.
 */
export function toHexCrc32(crc) {
  return (crc >>> 0).toString(16).padStart(8, '0');
}
package-lock.json
{
  "name": "sample-crc-32",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-crc-32",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "crc-32": "1.2.2"
      }
    },
    "node_modules/crc-32": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/crc-32/-/crc-32-1.2.2.tgz",
      "integrity": "sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==",
      "license": "Apache-2.0",
      "bin": {
        "crc32": "bin/crc32.njs"
      },
      "engines": {
        "node": ">=0.8"
      }
    }
  }
}
package.json
{
  "name": "sample-crc-32",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "crc-32": "1.2.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "compute CRC32 checksum for strings and buffers using crc-32",
  "kind": "HOW",
  "packages": [
    "pkg:npm/crc-32@1.2.2"
  ],
  "symbols": [
    "CRC32.bstr",
    "CRC32.buf",
    "CRC32.str"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import CRC32 from 'crc-32';
import {
  checksumString,
  checksumBuffer,
  checksumBinaryString,
  toUnsignedCrc32,
  toHexCrc32,
} from '../index.mjs';

// 1. Verify CRC32.str computation on standard test vectors
const foxText = 'The quick brown fox jumps over the lazy dog';
const foxCrc = CRC32.str(foxText);
assert.equal(typeof foxCrc, 'number');
assert.equal(toUnsignedCrc32(foxCrc), 0x414fa339);
assert.equal(toHexCrc32(foxCrc), '414fa339');
assert.equal(checksumString(foxText), foxCrc);

// Empty string yields 0
assert.equal(CRC32.str(''), 0);
assert.equal(checksumString(''), 0);

// 2. Verify UTF-8 multi-byte string handling with CRC32.str
const unicodeText = 'Hello, 世界! 🌍';
const unicodeCrc = CRC32.str(unicodeText);
assert.equal(typeof unicodeCrc, 'number');
assert.equal(checksumString(unicodeText), unicodeCrc);

// 3. Verify CRC32.buf on Uint8Array and Buffer matches UTF-8 string encoding
const utf8Bytes = Buffer.from(unicodeText, 'utf8');
const bufCrc = CRC32.buf(utf8Bytes);
assert.equal(bufCrc, unicodeCrc);
assert.equal(checksumBuffer(utf8Bytes), unicodeCrc);

const uint8Array = new Uint8Array(utf8Bytes);
assert.equal(CRC32.buf(uint8Array), unicodeCrc);
assert.equal(checksumBuffer(uint8Array), unicodeCrc);

// 4. Verify CRC32.bstr on 8-bit binary string
const binaryData = [0x00, 0x01, 0x7f, 0x80, 0xff];
const binaryStr = String.fromCharCode(...binaryData);
const bstrCrc = CRC32.bstr(binaryStr);
const bbufCrc = CRC32.buf(new Uint8Array(binaryData));
assert.equal(bstrCrc, bbufCrc);
assert.equal(checksumBinaryString(binaryStr), bstrCrc);

// 5. Verify incremental / chained CRC32 calculation with seed
const part1 = 'The quick brown fox ';
const part2 = 'jumps over the lazy dog';
const seed1 = CRC32.str(part1);
const combinedCrc = CRC32.str(part2, seed1);
assert.equal(combinedCrc, foxCrc);

// Incremental computation across buffers
const part1Buf = Buffer.from(part1, 'utf8');
const part2Buf = Buffer.from(part2, 'utf8');
const seedBuf = CRC32.buf(part1Buf);
const combinedBufCrc = CRC32.buf(part2Buf, seedBuf);
assert.equal(combinedBufCrc, foxCrc);

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

原始种子者

匿名