CodeSampleX

示例

asn1js 3.0.10: Integer

已验证示例 — npm asn1js 3.0.10: Integer. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: asn1js.Integer constructs integer ASN.1 elements and provides…

sha256:2335ee62fb18c5b61662984ebc676c044c2ca4fd33baacdf4191609b82bb1a3a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-04

案例

HOW
目标
verify asn1js.Integer in pkg:npm/asn1js@3.0.10
包
符号
  • asn1js.Integer
环境
node 22.23.2
创建时间
2026-09-04T08:48:37Z

契约

  1. asn1js.Integer constructs integer ASN.1 elements and provides valueBlock.valueDec and toBigInt accessors
  2. asn1js.Integer toBER encodes integer values into BER/DER format with two's complement sign extension and zero padding
  3. asn1js.fromBER decodes BER integer buffers into asn1js.Integer instances extracting numeric and BigInt values
  4. asn1js.verifySchema validates BER integer elements against asn1js.Integer schemas and rejects mismatched types

文件

  • .npmrc
  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

下载源代码构件 (tar.gz)

源代码

.npmrc
update-notifier=false
audit=false
fund=false
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 asn1js.Integer in pkg:npm/asn1js@3.0.10
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/asn1js@3.0.10
Demonstrate these symbols/APIs:
  - asn1js.Integer
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:6590af4711daf51c21b6beaf9826b8be4f577466c6b7c3b43ffdaa55dcc0f1bb","constraints":{"executionContext":"node"},"contract":["asn1js.Integer constructs integer ASN.1 elements and provides valueBlock.valueDec and toBigInt accessors","asn1js.Integer toBER encodes integer values into BER/DER format with two's complement sign extension and zero padding","asn1js.fromBER decodes BER integer buffers into asn1js.Integer instances extracting numeric and BigInt values","asn1js.verifySchema validates BER integer elements against asn1js.Integer schemas and rejects mismatched types"],"goal":"verify asn1js.Integer in pkg:npm/asn1js@3.0.10","kind":"HOW","packages":["pkg:npm/asn1js@3.0.10"],"schemaVersion":1,"symbols":["asn1js.Integer"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/asn1js@3.0.10"],"schemaVersion":1,"subject":"pkg:npm/asn1js@3.0.10","symbols":["asn1js.Integer"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-asn1js-integer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-asn1js-integer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "asn1js": "3.0.10"
      }
    },
    "node_modules/asn1js": {
      "version": "3.0.10",
      "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.10.tgz",
      "integrity": "sha512-S2s3aOytiKdFRdulw2qPE51MzjzVOisppcVv7jVFR+Kw0kxwvFrDcYA0h7Ndqbmj0HkMIXYWaoj7fli8kgx1eg==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "pvtsutils": "^1.3.6",
        "pvutils": "^1.1.5",
        "tslib": "^2.8.1"
      },
      "engines": {
        "node": ">=12.0.0"
      }
    },
    "node_modules/pvtsutils": {
      "version": "1.3.6",
      "resolved": "https://registry.npmjs.org/pvtsutils/-/pvtsutils-1.3.6.tgz",
      "integrity": "sha512-PLgQXQ6H2FWCaeRak8vvk1GW462lMxB5s3Jm673N82zI4vqtVUPuZdffdZbPDFRoU8kAhItWFtPCWiPpp4/EDg==",
      "license": "MIT",
      "dependencies": {
        "tslib": "^2.8.1"
      }
    },
    "node_modules/pvutils": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/pvutils/-/pvutils-1.2.0.tgz",
      "integrity": "sha512-BbubeCEyTuQjVMakvJQ/Sxbc93F2pwmbsxONT/ZRrwU7Ua38d8unYTwXpTVLAKJ4BDuH9IGztCjQcd/N/39Dvg==",
      "license": "MIT",
      "engines": {
        "node": ">=16.0.0"
      }
    },
    "node_modules/tslib": {
      "version": "2.8.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
      "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
      "license": "0BSD"
    }
  }
}
package.json
{
  "name": "sample-asn1js-integer",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for asn1js.Integer",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "asn1js": "3.0.10"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify asn1js.Integer in pkg:npm/asn1js@3.0.10",
  "kind": "HOW",
  "packages": [
    "pkg:npm/asn1js@3.0.10"
  ],
  "symbols": [
    "asn1js.Integer"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const asn1js = require('asn1js');

/**
 * Creates an ASN.1 Integer instance.
 * @param {number} value
 * @returns {asn1js.Integer}
 */
function createInteger(value) {
  return new asn1js.Integer({ value });
}

/**
 * Encodes an integer value into a BER ArrayBuffer.
 * @param {number} value
 * @returns {ArrayBuffer}
 */
function encodeInteger(value) {
  const intBlock = new asn1js.Integer({ value });
  return intBlock.toBER();
}

/**
 * Decodes a BER buffer into an asn1js.Integer instance and returns its BigInt value.
 * @param {ArrayBuffer|Uint8Array} inputBuffer
 * @returns {bigint}
 */
function decodeInteger(inputBuffer) {
  const decoded = asn1js.fromBER(inputBuffer);
  if (decoded.offset === -1) {
    throw new Error('Failed to decode BER buffer');
  }
  if (!(decoded.result instanceof asn1js.Integer)) {
    throw new TypeError('Decoded block is not an ASN.1 Integer');
  }
  return decoded.result.toBigInt();
}

/**
 * Creates an Integer schema element for structure validation.
 * @param {string} name
 * @returns {asn1js.Integer}
 */
function createIntegerSchema(name) {
  return new asn1js.Integer({ name });
}

/**
 * Verifies a BER buffer against a schema.
 * @param {ArrayBuffer|Uint8Array} inputBuffer
 * @param {asn1js.BaseBlock} schema
 * @returns {{ verified: boolean, result: any }}
 */
function verifyIntegerSchema(inputBuffer, schema) {
  return asn1js.verifySchema(inputBuffer, schema);
}

module.exports = {
  createInteger,
  encodeInteger,
  decodeInteger,
  createIntegerSchema,
  verifyIntegerSchema,
  asn1js
};
test/contract.js
'use strict';

const assert = require('node:assert');
const asn1js = require('asn1js');
const {
  createInteger,
  encodeInteger,
  decodeInteger,
  createIntegerSchema,
  verifyIntegerSchema
} = require('../src/index.js');

// 1. Integer element construction and accessors (valueDec, toBigInt)
const intZero = new asn1js.Integer({ value: 0 });
assert.strictEqual(intZero.valueBlock.valueDec, 0, 'intZero valueDec must be 0');
assert.strictEqual(intZero.toBigInt(), 0n, 'intZero toBigInt() must be 0n');

const intPositive = new asn1js.Integer({ value: 42 });
assert.strictEqual(intPositive.valueBlock.valueDec, 42, 'intPositive valueDec must be 42');
assert.strictEqual(intPositive.toBigInt(), 42n, 'intPositive toBigInt() must be 42n');

const intNegative = new asn1js.Integer({ value: -1 });
assert.strictEqual(intNegative.valueBlock.valueDec, -1, 'intNegative valueDec must be -1');
assert.strictEqual(intNegative.toBigInt(), -1n, 'intNegative toBigInt() must be -1n');

// 2. toBER encoding into standard BER/DER bytes
const berZero = intZero.toBER();
assert(berZero instanceof ArrayBuffer, 'toBER must return ArrayBuffer');
assert.deepStrictEqual(
  Array.from(new Uint8Array(berZero)),
  [0x02, 0x01, 0x00],
  'BER encoding of 0 must be [0x02, 0x01, 0x00]'
);

const ber42 = intPositive.toBER();
assert.deepStrictEqual(
  Array.from(new Uint8Array(ber42)),
  [0x02, 0x01, 0x2a],
  'BER encoding of 42 must be [0x02, 0x01, 0x2a]'
);

const berNeg1 = intNegative.toBER();
assert.deepStrictEqual(
  Array.from(new Uint8Array(berNeg1)),
  [0x02, 0x01, 0xff],
  'BER encoding of -1 must be [0x02, 0x01, 0xff]'
);

// 128 (0x80) requires 0x00 prefix to maintain positive sign in two's complement
const int128 = new asn1js.Integer({ value: 128 });
const ber128 = int128.toBER();
assert.deepStrictEqual(
  Array.from(new Uint8Array(ber128)),
  [0x02, 0x02, 0x00, 0x80],
  'BER encoding of 128 must be [0x02, 0x02, 0x00, 0x80]'
);

// 3. fromBER decoding into asn1js.Integer instances
const dec42 = asn1js.fromBER(ber42);
assert.strictEqual(dec42.offset, 3, 'fromBER must consume 3 bytes');
assert(dec42.result instanceof asn1js.Integer, 'decoded result must be asn1js.Integer');
assert.strictEqual(dec42.result.valueBlock.valueDec, 42, 'decoded valueDec must be 42');
assert.strictEqual(dec42.result.toBigInt(), 42n, 'decoded toBigInt() must be 42n');

const decNeg1 = asn1js.fromBER(berNeg1);
assert.strictEqual(decNeg1.offset, 3, 'fromBER must consume 3 bytes for -1');
assert(decNeg1.result instanceof asn1js.Integer, 'decoded result must be asn1js.Integer');
assert.strictEqual(decNeg1.result.valueBlock.valueDec, -1, 'decoded valueDec must be -1');
assert.strictEqual(decNeg1.result.toBigInt(), -1n, 'decoded toBigInt() must be -1n');

const dec128 = asn1js.fromBER(ber128);
assert.strictEqual(dec128.offset, 4, 'fromBER must consume 4 bytes for 128');
assert(dec128.result instanceof asn1js.Integer, 'decoded result must be asn1js.Integer');
assert.strictEqual(dec128.result.valueBlock.valueDec, 128, 'decoded valueDec must be 128');
assert.strictEqual(dec128.result.toBigInt(), 128n, 'decoded toBigInt() must be 128n');

// Invalid/truncated buffer handling
const truncated = new Uint8Array([0x02, 0x02, 0x00]).buffer;
const decTrunc = asn1js.fromBER(truncated);
assert.strictEqual(decTrunc.offset, -1, 'truncated buffer must return offset -1');

// 4. Schema verification with asn1js.verifySchema
const schema = new asn1js.Integer({ name: 'counter' });
const resValid = asn1js.verifySchema(ber42, schema);
assert.strictEqual(resValid.verified, true, 'schema verification must pass for integer buffer');
assert.strictEqual(resValid.result.counter.toBigInt(), 42n, 'schema result must contain counter with value 42n');

const wrongSchema = new asn1js.Boolean({ name: 'counter' });
const resInvalid = asn1js.verifySchema(ber42, wrongSchema);
assert.strictEqual(resInvalid.verified, false, 'schema verification must fail for mismatched type');

// 5. Helper functions from src/index.js
const created = createInteger(100);
assert(created instanceof asn1js.Integer, 'createInteger must return asn1js.Integer');
assert.strictEqual(created.toBigInt(), 100n);

const encoded = encodeInteger(100);
assert(encoded instanceof ArrayBuffer);
assert.strictEqual(decodeInteger(encoded), 100n);

const schemaHelper = createIntegerSchema('counter');
const validated = verifyIntegerSchema(encoded, schemaHelper);
assert.strictEqual(validated.verified, true);
assert.strictEqual(validated.result.counter.toBigInt(), 100n);

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

原始种子者

匿名