A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
A native implementation of [TLS][] (and various other cryptographic tools) in [JavaScript][].
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.
Forge is fast. Benchmarks against other popular JavaScript cryptography libraries can be found here:
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.
If you want to use forge with [Node.js][], it is available through npm:
https://www.npmjs.com/package/node-forge
Installation:
npm install node-forgeYou can then use forge as a regular module:
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.
To use it via jsDelivr include this in your html:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/forge.min.js"></script>To use it via unpkg include this in your html:
<script src="https://unpkg.com/[email protected]/dist/forge.min.js"></script>The core JavaScript has the following requirements to build and test:
Some special networking features can optionally use a Flash component. See the Flash README for details.
To create single file bundles for use with browsers run the following:
npm install
npm run buildThis will create single non-minimized and minimized files that can be included in the browser:
dist/forge.js
dist/forge.min.jsA bundle that adds some utilities and networking support is also available:
dist/forge.all.js
dist/forge.all.min.jsInclude the file via:
<script src="YOUR_SCRIPT_PATH/forge.js"></script>or
<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.
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.
npm installForge natively runs in a [Node.js][] environment:
npm testAutomated testing is done via [Karma][]. By default it will run the tests with Headless Chrome.
npm run test-karmaIs 'mocha' reporter output too verbose? Other reporters are available. Try 'dots', 'progress', or 'tap'.
npm run test-karma -- --reporters progressBy default [webpack][] is used. [Browserify][] can also be used.
BUNDLER=browserify npm run test-karmaYou can also specify one or more browsers to use.
npm run test-karma -- --browsers Chrome,Firefox,Safari,ChromeHeadlessThe reporter option and BUNDLER environment variable can also be used.
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:
npm run test-buildTo run legacy browser based tests the main forge build is required:
npm run buildThe tests are run with a custom server that prints out the URLs to use:
npm run test-serverThere are some other random tests and benchmarks available in the tests directory.
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.
npm install
npm run coverageAny 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.
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:
// run this *after* including the forge script
forge.options.usePureJavaScript = true;To disable native code when using Node.js:
var forge = require('node-forge');
forge.options.usePureJavaScript = true;Provides a native javascript client and server-side [TLS][] implementation.
Examples
…Connect to a TLS server using node's net.Socket:
…Provides a native [JavaScript][] mini-implementation of an http client that uses pooled sockets.
Examples
…Provides some SSH utility functions.
Examples
// 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: ':'});Provides an XmlHttpRequest implementation using forge.http as a backend.
Examples
// TODOProvides an interface to create and use raw sockets provided via Flash.
Examples
// TODOProvides 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:
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):
…Provides [AES][] encryption and decryption in [CBC][], [CFB][], [OFB][], [CTR][], and [GCM][] modes. See CIPHER for examples.
Provides [3DES][] and [DES][] encryption and decryption in [ECB][] and [CBC][] modes. See CIPHER for examples.
Examples
// 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());Provides [X.509][] certificate support, ED25519 key generation and signing/verifying, and RSA public and private key encoding, decoding, encryption/decryption, and signing/verifying.
Special thanks to [TweetNaCl.js][] for providing the bulk of the implementation.
Examples
…Examples
…Examples
…Examples
…Provides the password-based key-derivation function from [PKCS#5][].
Examples
// 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
});Provides cryptographically protected messages from [PKCS#7][].
Examples
…__Exampl
No open issues yet, or sync has not completed.