Port of TweetNaCl / NaCl to JavaScript for modern browsers and Node.js. Public domain.
The primary goal of this project is to produce a translation of TweetNaCl to JavaScript which is as close as possible to the original C implementation, plus a thin layer of idiomatic high-level API on top of it.
There are two versions, you can use either of them:
-
nacl.js
is the port of TweetNaCl with minimum differences from the original + high-level API. -
nacl-fast.js
is likenacl.js
, but with some functions replaced with faster versions.
You can install TweetNaCl.is via a package manager:
$ bower install tweetnacl
NPM:
$ npm install tweetnacl
All API functions accept and return bytes as Uint8Array
s. If you need to
encode or decode strings, use functions from nacl.util
namespace.
Implements curve25519-xsalsa20-poly1305.
Generates a new random key pair for box and returns it as an object with
publicKey
and secretKey
members:
{
publicKey: ..., // Uint8Array with 32-byte public key
secretKey: ... // Uint8Array with 32-byte secret key
}
Returns a key pair for box with public key corresponding to the given secret key.
Encrypt and authenticates message using peer's public key, our secret key, and the given nonce, which must be unique for each distinct message for a key pair.
Returns an encrypted and authenticated message, which is
nacl.box.overheadLength
longer than the original message.
Authenticates and decrypts the given box with peer's public key, our secret key, and the given nonce.
Returns the original message, or false
if authentication fails.
Returns a precomputed shared key which can be used in nacl.box.after
and
nacl.box.open.after
.
Same as nacl.box
, but uses a shared key precomputed with nacl.box.before
.
Same as nacl.box.open
, but uses a shared key precomputed with nacl.box.before
.
Length of public key in bytes.
Length of secret key in bytes.
Length of precomputed shared key in bytes.
Length of nonce in bytes.
Length of overhead added to box compared to original message.
Implements xsalsa20-poly1305.
Encrypt and authenticates message using the key and the nonce. The nonce must be unique for each distinct message for this key.
Returns an encrypted and authenticated message, which is
nacl.secretbox.overheadLength
longer than the original message.
Authenticates and decrypts the given secret box using the key and the nonce.
Returns the original message, or false
if authentication fails.
Length of key in bytes.
Length of nonce in bytes.
Length of overhead added to secret box compared to original message.
Implements curve25519.
Multiplies an integer n
by a group element p
and returns the resulting
group element.
Multiplies an integer n
by a standard group element and returns the resulting
group element.
Length of scalar in bytes.
Length of group element in bytes.
Implements ed25519.
Generates new random key pair for signing and returns it as an object with
publicKey
and secretKey
members:
{
publicKey: ..., // Uint8Array with 32-byte public key
secretKey: ... // Uint8Array with 64-byte secret key
}
Returns a signing key pair with public key corresponding to the given
64-byte secret key. The secret key must have been generated by
nacl.sign.keyPair
or nacl.sign.keyPair.fromSeed
.
Returns a new signing key pair generated deterministically from a 32-byte seed.
The seed must contain enough entropy to be secure. This method is not
recommended for general use: instead, use nacl.sign.keyPair
to generate a new
key pair from a random seed.
Signs the message using the secret key and returns a signed message.
Verifies the signed message and returns the message without signature.
Returns null
if verification failed.
Signs the message using the secret key and returns a signature.
Verifies the signature for the message and returns true
if verification
succeeded or false
if it failed.
Length of signing public key in bytes.
Length of signing secret key in bytes.
Length of signature in bytes.
Implements SHA-512.
Returns SHA-512 hash of the message.
Length of hash in bytes.
Returns a Uint8Array
of the given length containing random bytes of
cryptographic quality.
Implementation note
TweetNaCl.js uses the following methods to generate random bytes, depending on the platform it runs on:
window.crypto.getRandomValues
(WebCrypto standard)window.msCrypto.getRandomValues
(Internet Explorer 11)crypto.randomBytes
(Node.js)
Note that browsers are required to throw QuotaExceededError
exception if
requested length
is more than 65536, so do not ask for more than 65536 bytes
in one call (multiple calls to get as many bytes as you like are okay:
browsers can generate infinite amount of random bytes without any bad
consequences).
If the platform doesn't provide a suitable PRNG, the following functions, which require random numbers, will throw exception:
nacl.randomBytes
nacl.box.keyPair
nacl.sign.keyPair
Other functions are deterministic and will continue working.
If a platform you are targeting doesn't implement secure random number
generator, but you somehow have a cryptographically-strong source of entropy
(not Math.random
!), and you know what you are doing, you can plug it into
TweetNaCl.js like this:
nacl.setPRNG(function(x, n) {
// ... copy n random bytes into x ...
});
Note that nacl.setPRNG
completely replaces internal random byte generator
with the one provided.
Compares x
and y
in constant time and returns true
if their lengths are
non-zero and equal, and their contents are equal.
Returns false
if either of the arguments has zero length, or arguments have
different lengths, or their contents differ.
Encoding/decoding functions are provided for convenience. They are correct, however their performance and wide compatibility with uncommon runtimes is not something that is considered important compared to the simplicity and size of implementation. You can use third-party libraries if you need to.
Decodes string and returns Uint8Array
of bytes.
Encodes Uint8Array
or Array
of bytes into string.
Decodes Base-64 encoded string and returns Uint8Array
of bytes.
Encodes Uint8Array
or Array
of bytes into string using Base-64 encoding.
TweetNaCl.js supports modern browsers that have a cryptographically secure pseudorandom number generator and typed arrays, including the latest versions of:
- Chrome
- Firefox
- Safari (Mac, iOS)
- Internet Explorer 11
Other systems:
- Node.js (we test on 0.10 and later)
Install NPM modules needed for development:
$ npm install
To build minified versions:
$ npm run build
Tests use minified version, so make sure to rebuild it every time you change
nacl.js
or nacl-fast.js
.
To run tests in Node.js:
$ npm test
By default all tests described here work on nacl.min.js
. To test other
versions, set environment variable NACL_SRC
to the file name you want to test.
For example, the following command will test fast minified version:
$ NACL_SRC=nacl-fast.min.js npm test
To run full suite of tests in Node.hs, including comparing outputs of JavaScript port to outputs of the original C version:
$ npm run testall
To prepare tests for browsers:
$ npm run browser
and then open tests/browser/test.html
(or tests/browser/test-fast.html
) to
run them.
To run headless browser tests with testling
:
$ npm run testling
(If you get Error: spawn ENOENT
, install xvfb: sudo apt-get install xvfb
.)
To run benchmarks in Node.js:
$ npm run bench
$ NACL_SRC=nacl-fast.min.js npm run bench
To run benchmarks in a browser, open test/benchmark/bench.html
(or
test/benchmark/bench-fast.html
).
JavaScript port:
- Dmitry Chestnykh (ported xsalsa20, poly1305, curve25519)
- Devi Mandiri (ported curve25519, ed25519, sha512)
Original authors of NaCl, TweetNaCl and Poly1305-donna (who are not responsible for any errors in this implementation):
- Daniel J. Bernstein
- Wesley Janssen
- Tanja Lange
- Peter Schwabe
- Matthew Dempsky
- Andrew Moon
Contributors have dedicated their work to the public domain.
This software is distributed without any warranty.