npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-crypto-utils

v1.0.1

Published

一个安全、轻量级、零依赖的加密工具包,提供密钥对生成、数字签名和验证功能。基于 Web Crypto API 构建,适用于浏览器和 Node.js 环境。

Readme

Crypto Utils

一个安全、轻量级、零依赖的加密工具包,提供密钥对生成、数字签名和验证功能。基于 Web Crypto API 构建,适用于浏览器和 Node.js 环境。

编程语言

  • TypeScript:该项目使用 TypeScript 编写,提供完整的类型支持和现代 JavaScript 特性。

特性

  • 🔐 安全可靠:使用 Web Crypto API,符合密码学最佳实践
  • 📦 零依赖:完全自包含,无外部依赖
  • 🌐 跨平台:支持浏览器和 Node.js 环境
  • 🔧 灵活配置:支持多种算法和配置选项
  • 🛡️ 生产就绪:包含完整的错误处理和类型定义
  • 📚 TypeScript:完整的 TypeScript 类型支持

安装

npm install ts-crypto-utils

使用方法

生成密钥对

import { KeyGenerator } from 'ts-crypto-utils';

async function generateKeys() {
  try {
    const keyPair = await KeyGenerator.generateKeyPair();
    
    console.log('公钥:', keyPair.publicKey);
    console.log('私钥:', keyPair.privateKey);
    
    return keyPair;
  } catch (error) {
    console.error('密钥生成失败:', error);
  }
}

// 使用示例
generateKeys();

数字签名

import { KeyGenerator } from 'ts-crypto-utils';

async function signData() {
  // 生成密钥对
  const keyPair = await KeyGenerator.generateKeyPair();
  
  // 要签名的数据
  const data = "Hello, World!";
  
  // 使用私钥签名
  const signature = await keyPair.privateKey.sign(data);
  console.log('签名:', signature);
  
  return { keyPair, data, signature };
}

验证签名

import { KeyGenerator } from 'ts-crypto-utils';

async function verifySignature() {
  const { keyPair, data, signature } = await signData();
  
  // 使用公钥验证签名
  const isValid = await keyPair.publicKey.verify(signature, data);
  console.log('签名验证结果:', isValid); // 应该输出 true
  
  // 验证被篡改的数据
  const isInvalid = await keyPair.publicKey.verify(signature, "tampered data");
  console.log('被篡改数据的验证结果:', isInvalid); // 应该输出 false
}

导入/导出密钥

import { PrivateKey, PublicKey } from 'ts-crypto-utils';

async function keyImportExport() {
  // 生成密钥对
  const keyPair = await KeyGenerator.generateKeyPair();
  
  // 导出私钥为 JWK 格式
  const privateKeyJWK = await keyPair.privateKey.exportKeyAsJWKString();
  console.log('导出的私钥 (JWK):', privateKeyJWK);
  
  // 从 JWK 字符串导入私钥
  const importedPrivateKey = await PrivateKey.fromJWKString(privateKeyJWK);
  
  // 导出公钥为十六进制格式
  const publicKeyHex = await keyPair.publicKey.exportKeyAsHexString();
  console.log('导出的公钥 (十六进制):', publicKeyHex);
  
  // 从十六进制字符串导入公钥
  const importedPublicKey = await PublicKey.fromHexString(publicKeyHex);
}

API 参考

KeyGenerator

  • generateKeyPair(): 生成 ECDSA P-521 密钥对

PrivateKey

  • sign(data: string): 使用私钥对数据进行签名
  • exportKeyAsJWKString(): 导出私钥为 JWK 格式
  • fromJWKString(jwkString: string): 从 JWK 字符串创建私钥实例

PublicKey

  • verify(signature: string, data: string): 使用公钥验证签名
  • exportKeyAsHexString(): 导出公钥为十六进制格式
  • fromHexString(hexString: string): 从十六进制字符串创建公钥实例

算法详情

该库使用以下加密算法配置:

  • 算法: ECDSA
  • 曲线: P-521
  • 哈希: SHA-512

注意事项

  • 该库依赖于 Web Crypto API,在较老的浏览器或环境中可能不可用
  • 请妥善保管私钥,私钥泄露将导致安全风险
  • 生成的密钥对仅在当前会话中有效,如需持久化请使用导出/导入功能