Socket
Book a DemoInstallSign in
Socket

@vf.js/pki

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vf.js/pki

这是一个构建与pki.js之上的,用于处理公钥,私钥,证书,签名等基础设施的简单工具包。

latest
npmnpm
Version
1.1.1
Version published
Maintainers
2
Created
Source

@vf.js/pki

说明

这是一个构建与pki.js之上的,用于处理公钥,私钥,证书,签名等基础设施的简单工具包。

  • 提供密钥创建服务
  • 提供证书创建服务 CSR 请求

安装

npm install @vf.js/pki

示例


import {
    generateKeypairPem,
    pemToPrivateKey,
    pemToPublicKey,
    certificationRequest,
    utf8tob64
} from "@vf.js/pki"

// 创建Pem格式的密钥对
const keypairPem = generateKeypairPem({ "alg": 'EC', "curve": "secp256r1" })

console.log(keypairPem.privateKeyPem)
console.log(keypairPem.publicKeyPem)

// 获取原生的密钥信息
const privateKey = await pemToPrivateKey(keypairPem.privateKeyPem);
const publicKey = await pemToPublicKey(keypairPem.publicKeyPem);

// 创建CSR证书请求
const csr = await certificationRequest(privateKey,publicKey,[
        {
            name: 'commonName',
            value: 'www.example.com'
        }, 
        {
            name: 'countryName',
            value: 'China'
        },
        {
            name: 'stateOrProvinceName',
            value: 'BeiJing'
        },
        {
            name: 'localityName',
            value: 'BeiJing'
        },
        {
            name: 'organizationName',
            value: 'Huawei'
        }, 
        {
            name: 'organizationalUnitName',
            value: 'client'
        },
])

console.log(csr.csrPem)

API

import { xxxxx } from "@vf.js/pki"

创建密钥对

const keypairPem = await generateKeypairPem({ "alg": 'EC', "curve": "secp256r1" })

console.log(keypairPem.privateKeyPem)
console.log(keypairPem.publicKeyPem)

默认加密类型

DefaultAlgorithm = 
{
    name: "ECDSA", // ✅ 正确的算法名称
    hash: "SHA-256", // ECDSA 必须指定哈希算法,比如 SHA-256
    namedCurve: "P-256", // 指定椭圆曲线,比如 P-256, P-384, P-521
};

从PEM证书获取公钥PEM

import { certToPublicPem } from "@vf.js/pki"

const pubPem = await certToPublicPem(certStrPem)

pem转换ArrayBuffer


await pemToArrayBuffer(pemStr);

ArrayBuffer转换pem

const pem = await arrayBufferToPem(signature,'CERTIFICATE REQUEST');

pem转换为ASN1格式

await pemToASN1(pemStr)

pem转换为私钥的原生模式

await pemToPrivateKey(pem: string, keyUsages?: KeyUsage[], keyFormat?: "pkcs8" | "raw")

pem转换为公钥的原生模式

await pemToPublicKey(publicKeyPem)

创建证书请求

查看示例

certificationRequest

获取原生Crypto

getCrypto

base64转utf8

b64toutf8

utf8转base64

utf8tob64

FAQs

Package last updated on 02 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts