@freedomofpress/crypto-browser

中文文档
0.3.0发布于 2026/10/31Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 11.6万

Browser-compatible cryptography utilities.

原始使用文档
文档目录16 节
  1. Features
  2. ASN.1 Parsing & Encoding
  3. Encoding Utilities
  4. Cryptographic Operations
  5. Additional Utilities
  6. Installation
  7. Usage
  8. Import a public key and verify a signature
  9. Parse ASN.1 structures
  10. Encode/decode utilities
  11. Dependencies
  12. Ed25519 fallback and digest verification
  13. Structure
  14. Development
  15. License
  16. Acknowledgments

crypto-browser

CI

Browser-compatible cryptography utilities for Sigstore and TUF verification.

Used by tuf-browser and sigstore-browser.

[!CAUTION] This library has not received an independent security audit. Maintenance is performed by volunteers, and the project is not officially supported or endorsed by the Freedom of the Press Foundation.

Features

ASN.1 Parsing & Encoding

  • ASN1Obj: Parse and encode DER-encoded ASN.1 structures
  • ASN1Tag: Handle ASN.1 tags and type checking
  • Length encoding/decoding: DER length format support
  • Type parsers: Extract integers, OIDs, dates, bit strings, booleans from ASN.1

Encoding Utilities

  • Base64: Encode/decode with standard and URL-safe variants
  • Hex: Encode/decode hexadecimal strings
  • PEM: Convert between PEM and DER formats
  • UTF-8: String to Uint8Array conversion
  • Bytes: concatBytes, and constant-time comparison with uint8ArrayEqual
  • Digests: sha256(bytes) and digest(algorithm, bytes) returning bytes
  • Merkle trees (RFC 6962): hashLeaf, hashChildren, rootHash, verifyInclusionProof, verifyConsistencyProof, tested against the transparency-dev vectors

Cryptographic Operations

  • Key import: Support for ECDSA (P-256/P-384/P-521), Ed25519, and RSA keys
  • Signature verification: Verify signatures using Web Crypto API
    • ECDSA with SHA-256/384/512
    • Ed25519
    • RSA-PSS and RSASSA-PKCS1-v1_5
  • Digest verification: ECDSA signature verification over pre-computed digests, in the opt-in noble entry
  • PKCS#1 to SPKI conversion: Convert RSA keys for Web Crypto compatibility

Additional Utilities

  • ByteStream: Efficient byte buffer with auto-growing allocation
  • Canonical JSON: OLPC canonical JSON (TUF) and RFC 8785 JCS (Rekor, WEBCAT), both tested against official vectors

Installation

npm install @freedomofpress/crypto-browser

Usage

Import a public key and verify a signature

import { importKey, verifySignature } from '@freedomofpress/crypto-browser';

const publicKey = await importKey('ECDSA', 'P-256', pemKey);
const isValid = await verifySignature(publicKey, message, signature, 'sha256');

Parse ASN.1 structures

import { ASN1Obj } from '@freedomofpress/crypto-browser';

const asn1 = ASN1Obj.parseBuffer(derBytes);
const oid = asn1.subs[0].toOID();
const integer = asn1.subs[1].toInteger();

Encode/decode utilities

import {
  base64ToUint8Array,
  uint8ArrayToHex,
  toDER,
  fromDER,
} from '@freedomofpress/crypto-browser';

const bytes = base64ToUint8Array('SGVsbG8=');
const hex = uint8ArrayToHex(bytes);
const der = toDER(pemString);
const pem = fromDER(derBytes, 'PUBLIC KEY');

Dependencies

  • @noble/curves: only reached through @freedomofpress/crypto-browser/noble, so bundles that never import that entry do not carry it

Ed25519 fallback and digest verification

getSubtle() returns the platform's native SubtleCrypto. Platforms without Ed25519 in WebCrypto can route every helper through @noble/curves for Ed25519 keys only:

import { installEd25519Fallback } from "@freedomofpress/crypto-browser/noble";

await installEd25519Fallback();

The same entry exports verifySignatureOverDigest(key, digest, signature) for ECDSA signatures when only the digest of the signed data is available.

Structure

src/
  asn1/           # ASN.1 parsing and encoding
  encoding.ts     # Base64, hex, UTF-8 encoding
  pem.ts          # PEM format conversion
  stream.ts       # ByteStream utility
  crypto.ts       # Cryptographic operations
  canonicalize.ts # Canonical JSON

Development

# Install dependencies
npm install

# Build the project
npm run build

License

Apache-2.0 - see LICENSE file for details.

Acknowledgments

This package provides browser-compatible versions of cryptographic utilities from the Sigstore and TUF ecosystems. All code is licensed under Apache-2.0.

Primary Sources:

All files maintain their original copyright headers.

版本与发布记录

@freedomofpress/crypto-browser 共发布 13 个版本,首次发布于 2025/11/27;过去 12 个月发布了 13 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/10/3115 天前
0.2.22026/10/3116 天前
0.2.12026/10/3016 天前
0.2.02026/10/3016 天前
0.1.82026/09/201 个月前
0.1.72026/02/149 个月前

关于 @freedomofpress/crypto-browser 的常见问题

@freedomofpress/crypto-browser 是什么?

Browser-compatible cryptography utilities.

如何安装 @freedomofpress/crypto-browser?

在项目目录运行 npm install @freedomofpress/crypto-browser,或使用 pnpm add / yarn add / bun add。

@freedomofpress/crypto-browser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freedomofpress/crypto-browser 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@freedomofpress/crypto-browser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@freedomofpress/crypto-browser 可以商用吗?

@freedomofpress/crypto-browser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@freedomofpress/crypto-browser 的下载量有多少?

根据 npm 官方下载统计,@freedomofpress/crypto-browser 在 2026/10/16 – 2026/11/14 这 30 天内被下载 115,686 次,日均约 3,856 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freedomofpress/crypto-browser 还在维护吗?

最新版本 0.3.0 发布于 2026/10/31(15 天前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2025/11/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。