pem vs crypto vs node-forge vs pem-jwk
暗号化ライブラリ
pemcryptonode-forgepem-jwk類似パッケージ:

暗号化ライブラリ

暗号化ライブラリは、データのセキュリティを確保するために使用されるツールです。これらのライブラリは、データの暗号化、復号化、署名、検証などの機能を提供し、アプリケーションのセキュリティを強化します。特に、ウェブアプリケーションやAPIでのデータ保護に不可欠です。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
pem590,906574338 kB213年前MIT
crypto032-139年前ISC
node-forge05,3031.65 MB4562ヶ月前(BSD-3-Clause OR GPL-2.0)
pem-jwk073-97年前MPL-2.0

機能比較: pem vs crypto vs node-forge vs pem-jwk

暗号化機能

  • pem:

    PEM形式のデータを簡単に扱える機能を提供し、証明書や鍵の生成・変換が容易です。

  • crypto:

    基本的なハッシュ関数(SHA-256など)や対称・非対称暗号化アルゴリズム(AES、RSAなど)を提供し、セキュアなデータ処理が可能です。

  • node-forge:

    多様な暗号化アルゴリズムをサポートし、特にPKI(公開鍵基盤)に関連する機能が豊富です。

  • pem-jwk:

    PEM形式の鍵をJWK形式に変換するためのシンプルなAPIを提供し、Webアプリケーションとの互換性を高めます。

使用シナリオ

  • pem:

    サーバーサイドでの証明書や鍵の管理に特化しており、特にSSL/TLS設定に役立ちます。

  • crypto:

    Node.jsアプリケーションでの基本的な暗号化処理やハッシュ生成に最適です。

  • node-forge:

    ブラウザやNode.js環境での高度な暗号化処理や証明書管理に適しています。

  • pem-jwk:

    APIでの鍵管理やJWT(JSON Web Token)での使用に最適です。

設計原則

  • pem:

    特定のフォーマット(PEM)に特化した設計で、使いやすさを重視しています。

  • crypto:

    シンプルで直感的なAPIを提供し、Node.jsの他のモジュールと統合しやすい設計です。

  • node-forge:

    モジュール化された設計で、必要な機能だけをインポートして使用できる柔軟性があります。

  • pem-jwk:

    シンプルな変換機能に特化しており、他のライブラリとの組み合わせが容易です。

メンテナンス

  • pem:

    シンプルな機能セットにより、メンテナンスが容易で、特定のニーズに応じた更新が行われています。

  • crypto:

    Node.jsの標準ライブラリであるため、常に最新のセキュリティ基準に従って更新されています。

  • node-forge:

    オープンソースで活発にメンテナンスされており、新しい機能やバグ修正が定期的に行われています。

  • pem-jwk:

    軽量でシンプルなライブラリであり、メンテナンスが容易です。

学習曲線

  • pem:

    シンプルなAPI設計により、すぐに使い始めることができ、学習が容易です。

  • crypto:

    Node.jsに組み込まれているため、他のNode.jsの機能に慣れている開発者にとっては学習が容易です。

  • node-forge:

    多機能であるため、学習曲線はやや急ですが、豊富なドキュメントがサポートしています。

  • pem-jwk:

    特化した機能のため、すぐに理解できるシンプルさがあります。

選び方: pem vs crypto vs node-forge vs pem-jwk

  • pem:

    PEM形式の鍵や証明書の操作が必要な場合に最適です。特に、証明書の生成や変換を簡単に行いたい場合に選択してください。

  • crypto:

    Node.jsの標準ライブラリであり、パフォーマンスが重要なアプリケーションに適しています。基本的な暗号化機能が必要な場合に選択してください。

  • node-forge:

    より高度な暗号化機能や、X.509証明書の生成、PKCS#12ファイルの操作が必要な場合に選択します。

  • pem-jwk:

    PEM形式の鍵をJWK(JSON Web Key)形式に変換する必要がある場合に選択します。特に、Webアプリケーションでの鍵の管理が重要な場合に役立ちます。

pem のREADME

pem

Create private keys and certificates with node.js

Build Status npm version npm downloads pem documentation

JavaScript Style Guide

Installation

Install with npm

npm install pem

or use yarn

yarn add pem

:warning: Please make sure you have openssl or libressl already installed on your system/container, without them pem will not work.

Examples

Here are some examples for creating an SSL key/cert on the fly, and running an HTTPS server on port 443. 443 is the standard HTTPS port, but requires root permissions on most systems. To get around this, you could use a higher port number, like 4300, and use https://localhost:4300 to access your server.

Basic https

var https = require('https')
var pem = require('pem')

pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
  if (err) {
    throw err
  }
  https.createServer({ key: keys.clientKey, cert: keys.certificate }, function (req, res) {
    res.end('o hai!')
  }).listen(443)
})

Express

var https = require('https')
var pem = require('pem')
var express = require('express')

pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
  if (err) {
    throw err
  }
  var app = express()

  app.get('/', function (req, res) {
    res.send('o hai!')
  })

  https.createServer({ key: keys.clientKey, cert: keys.certificate }, app).listen(443)
})

API

Please have a look into the API documentation.

we had to clean up a bit

Custom extensions config file

You can specify custom OpenSSL extensions using the config or extFile options for createCertificate (or using csrConfigFile with createCSR).

extFile and csrConfigFile should be paths to the extension files. While config will generate a temporary file from the supplied file contents.

If you specify config then the v3_req section of your config file will be used.

The following would be an example of a Certificate Authority extensions file:

[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
commonName = Common Name
commonName_max = 64

[v3_req]
basicConstraints = critical,CA:TRUE

While the following would specify subjectAltNames in the resulting certificate:

[req]
req_extensions = v3_req

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = host1.example.com
DNS.2 = host2.example.com
DNS.3 = host3.example.com

Note that createCertificate and createCSR supports the altNames option which would be easier to use in most cases.

:warning: Warning: If you specify altNames the custom extensions file will not be passed to OpenSSL.

Setting openssl location

In some systems the openssl executable might not be available by the default name or it is not included in $PATH. In this case you can define the location of the executable yourself as a one time action after you have loaded the pem module:

var pem = require('pem')
pem.config({
  pathOpenSSL: '/usr/local/bin/openssl'
})
// do something with the pem module

:warning: CSR/Certificates with special chars

For more details, search in test/pem.spec.js: Create CSR with specialchars config file

If you use special chars like:

-!$%^&*()_+|~=`{}[]:/;<>?,.@#

You should know that the result mey have escaped characters when you read it in your application. Will try to fix this in the future, but not sure.

Special thanks to

  • Andris Reinman (@andris9) - Initiator of pem

License

MIT