Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

forge

> DevOps
Open source

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps

5.3K stars0 likes0 views
WebsiteGitHub

About

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps

Forge

A native implementation of [TLS][] (and various other cryptographic tools) in [JavaScript][].

Introduction

The Forge software is a fully native implementation of the [TLS][] protocol in JavaScript, a set of cryptography utilities, and a set of tools for developing Web Apps that utilize many network resources.

Performance

Forge is fast. Benchmarks against other popular JavaScript cryptography libraries can be found here:

  • http://dominictarr.github.io/crypto-bench/
  • http://cryptojs.altervista.org/test/simulate-threading-speed_test.html

Documentation

  • Introduction
  • Performance
  • Installation
  • Testing
  • Contributing

API

  • Options

Transports

  • TLS
  • HTTP
  • SSH
  • XHR
  • Sockets

Ciphers

  • CIPHER
  • AES
  • DES
  • RC2

PKI

  • ED25519
  • RSA
  • RSA-KEM
  • X.509
  • PKCS#5
  • PKCS#7
  • PKCS#8
  • PKCS#10
  • PKCS#12
  • ASN.1

Message Digests

  • SHA1
  • SHA256
  • SHA384
  • SHA512
  • MD5
  • HMAC

Utilities

  • Prime
  • PRNG
  • Tasks
  • Utilities
  • Logging
  • Flash Networking Support

Other

  • Security Considerations
  • Library Background
  • Contact
  • Donations

Installation

Note: Please see the Security Considerations section before using packaging systems and pre-built files.

Forge uses a [CommonJS][] module structure with a build process for browser bundles. The older [0.6.x][] branch with standalone files is available but will not be regularly updated.

Node.js

If you want to use forge with [Node.js][], it is available through npm:

https://www.npmjs.com/package/node-forge

Installation:

bash
npm install node-forge

You can then use forge as a regular module:

javascript
var forge = require('node-forge');

The npm package includes pre-built forge.min.js, forge.all.min.js, and prime.worker.min.js using the [UMD][] format.

jsDelivr CDN

To use it via jsDelivr include this in your html:

xml
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/forge.min.js"></script>

unpkg CDN

To use it via unpkg include this in your html:

xml
<script src="https://unpkg.com/[email protected]/dist/forge.min.js"></script>

Development Requirements

The core JavaScript has the following requirements to build and test:

  • Building a browser bundle:
    • Node.js
    • npm
  • Testing
    • Node.js
    • npm
    • Chrome, Firefox, Safari (optional)

Some special networking features can optionally use a Flash component. See the Flash README for details.

Building for a web browser

To create single file bundles for use with browsers run the following:

bash
npm install
npm run build

This will create single non-minimized and minimized files that can be included in the browser:

dist/forge.js
dist/forge.min.js

A bundle that adds some utilities and networking support is also available:

dist/forge.all.js
dist/forge.all.min.js

Include the file via:

xml
<script src="YOUR_SCRIPT_PATH/forge.js"></script>

or

xml
<script src="YOUR_SCRIPT_PATH/forge.min.js"></script>

The above bundles will synchronously create a global 'forge' object.

Note: These bundles will not include any WebWorker scripts (eg: dist/prime.worker.js), so these will need to be accessible from the browser if any WebWorkers are used.

Building a custom browser bundle

The build process uses [webpack][] and the config file can be modified to generate a file or files that only contain the parts of forge you need.

[Browserify][] override support is also present in package.json.

Testing

Prepare to run tests

bash
npm install

Running automated tests with Node.js

Forge natively runs in a [Node.js][] environment:

bash
npm test

Running automated tests with Headless Chrome

Automated testing is done via [Karma][]. By default it will run the tests with Headless Chrome.

bash
npm run test-karma

Is 'mocha' reporter output too verbose? Other reporters are available. Try 'dots', 'progress', or 'tap'.

bash
npm run test-karma -- --reporters progress

By default [webpack][] is used. [Browserify][] can also be used.

BUNDLER=browserify npm run test-karma

Running automated tests with one or more browsers

You can also specify one or more browsers to use.

bash
npm run test-karma -- --browsers Chrome,Firefox,Safari,ChromeHeadless

The reporter option and BUNDLER environment variable can also be used.

Running manual tests in a browser

Testing in a browser uses [webpack][] to combine forge and all tests and then loading the result in a browser. A simple web server is provided that will output the HTTP or HTTPS URLs to load. It also will start a simple Flash Policy Server. Unit tests and older legacy tests are provided. Custom ports can be used by running node tests/server.js manually.

To run the unit tests in a browser a special forge build is required:

bash
npm run test-build

To run legacy browser based tests the main forge build is required:

bash
npm run build

The tests are run with a custom server that prints out the URLs to use:

bash
npm run test-server

Running other tests

There are some other random tests and benchmarks available in the tests directory.

Coverage testing

To perform coverage testing of the unit tests, run the following. The results will be put in the coverage/ directory. Note that coverage testing can slow down some tests considerably.

bash
npm install
npm run coverage

Contributing

Any contributions (eg: PRs) that are accepted will be brought under the same license used by the rest of the Forge project. This license allows Forge to be used under the terms of either the BSD License or the GNU General Public License (GPL) Version 2.

See: LICENSE

If a contribution contains 3rd party source code with its own license, it may retain it, so long as that license is compatible with the Forge license.

API

Options

If at any time you wish to disable the use of native code, where available, for particular forge features like its secure random number generator, you may set the forge.options.usePureJavaScript flag to true. It is not recommended that you set this flag as native code is typically more performant and may have stronger security properties. It may be useful to set this flag to test certain features that you plan to run in environments that are different from your testing environment.

To disable native code when including forge in the browser:

javascript
// run this *after* including the forge script
forge.options.usePureJavaScript = true;

To disable native code when using Node.js:

javascript
var forge = require('node-forge');
forge.options.usePureJavaScript = true;

Transports

TLS

Provides a native javascript client and server-side [TLS][] implementation.

Examples

…

Connect to a TLS server using node's net.Socket:

…

HTTP

Provides a native [JavaScript][] mini-implementation of an http client that uses pooled sockets.

Examples

…

SSH

Provides some SSH utility functions.

Examples

javascript
// encodes (and optionally encrypts) a private RSA key as a Putty PPK file
forge.ssh.privateKeyToPutty(privateKey, passphrase, comment);

// encodes a public RSA key as an OpenSSH file
forge.ssh.publicKeyToOpenSSH(key, comment);

// encodes a private RSA key as an OpenSSH file
forge.ssh.privateKeyToOpenSSH(privateKey, passphrase);

// gets the SSH public key fingerprint in a byte buffer
forge.ssh.getPublicKeyFingerprint(key);

// gets a hex-encoded, colon-delimited SSH public key fingerprint
forge.ssh.getPublicKeyFingerprint(key, {encoding: 'hex', delimiter: ':'});

XHR

Provides an XmlHttpRequest implementation using forge.http as a backend.

Examples

javascript
// TODO

Sockets

Provides an interface to create and use raw sockets provided via Flash.

Examples

javascript
// TODO

Ciphers

CIPHER

Provides a basic API for block encryption and decryption. There is built-in support for the ciphers: [AES][], [3DES][], and [DES][], and for the modes of operation: [ECB][], [CBC][], [CFB][], [OFB][], [CTR][], and [GCM][].

These algorithms are currently supported:

  • AES-ECB
  • AES-CBC
  • AES-CFB
  • AES-OFB
  • AES-CTR
  • AES-GCM
  • 3DES-ECB
  • 3DES-CBC
  • DES-ECB
  • DES-CBC

When using an [AES][] algorithm, the key size will determine whether AES-128, AES-192, or AES-256 is used (all are supported). When a [DES][] algorithm is used, the key size will determine whether [3DES][] or regular [DES][] is used. Use a [3DES][] algorithm to enforce Triple-DES.

Examples

…

Using forge in Node.js to match openssl's "enc" command line tool (Note: OpenSSL "enc" uses a non-standard file format with a custom key derivation function and a fixed iteration count of 1, which some consider less secure than alternatives such as OpenPGP/GnuPG):

…

AES

Provides [AES][] encryption and decryption in [CBC][], [CFB][], [OFB][], [CTR][], and [GCM][] modes. See CIPHER for examples.

DES

Provides [3DES][] and [DES][] encryption and decryption in [ECB][] and [CBC][] modes. See CIPHER for examples.

RC2

Examples

javascript
// generate a random key and IV
var key = forge.random.getBytesSync(16);
var iv = forge.random.getBytesSync(8);

// encrypt some bytes
var cipher = forge.rc2.createEncryptionCipher(key);
cipher.start(iv);
cipher.update(forge.util.createBuffer(someBytes));
cipher.finish();
var encrypted = cipher.output;
// outputs encrypted hex
console.log(encrypted.toHex());

// decrypt some bytes
var cipher = forge.rc2.createDecryptionCipher(key);
cipher.start(iv);
cipher.update(encrypted);
cipher.finish();
// outputs decrypted hex
console.log(cipher.output.toHex());

PKI

Provides [X.509][] certificate support, ED25519 key generation and signing/verifying, and RSA public and private key encoding, decoding, encryption/decryption, and signing/verifying.

ED25519

Special thanks to [TweetNaCl.js][] for providing the bulk of the implementation.

Examples

…

RSA

Examples

…

RSA-KEM

Examples

…

X.509

Examples

…

PKCS#5

Provides the password-based key-derivation function from [PKCS#5][].

Examples

javascript
// generate a password-based 16-byte key
// note an optional message digest can be passed as the final parameter
var salt = forge.random.getBytesSync(128);
var derivedKey = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);

// generate key asynchronously
// note an optional message digest can be passed before the callback
forge.pkcs5.pbkdf2('password', salt, numIterations, 16, function(err, derivedKey) {
  // do something w/derivedKey
});

PKCS#7

Provides cryptographically protected messages from [PKCS#7][].

Examples

…

PKCS#8

__Exampl

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptaesasn1certificatecipher

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理