Port of TweetNaCl.js to TypeScript with several API changes for compatibility with Tree-Shaking to help modern JavaScript bundlers like Rollup and Webpack >2.x attain much optimization.
Also includes:
NOTE: May be you need crypto.getRandomValues()
polyfill for browsers which doesn't supported it.
Originally this project had been a port of nacl-fast.js
to TypeScript.
Now it also includes support of SealedBox and HMAC-Auth.
All API functions accept and return bytes as ByteArray
s (natively as Uint8Array
s).
If you need to encode or decode strings, use functions encodeUTF8/decodeUTF8
.
Implements x25519-xsalsa20-poly1305.
Generates a new random key pair for box and returns it as an object with
publicKey
and secretKey
members:
interface BoxKeyPair {
publicKey: ByteArray; // Array with 32-byte public key
secretKey: ByteArray; // Array with 32-byte secret key
}
Returns a key pair for box with public key corresponding to the given secret key.
Encrypts 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
BoxLength.Overhead
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 undefined
if authentication fails.
Returns a precomputed shared key which can be used in box_after
and
box_open_after
.
Same as box
, but uses a shared key precomputed with box_before
.
Same as box_open
, but uses a shared key precomputed with 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.
Encrypts 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
SecretBox.Overhead
longer than the original message.
Authenticates and decrypts the given secret box using the key and the nonce.
Returns the original message, or undefined
if authentication fails.
Length of key in bytes.
Length of nonce in bytes.
Length of overhead added to secret box compared to original message.
Sealed boxes are designed to anonymously send messages to a recipient given its public key.
Encrypts message using the recipient's public key.
Returns an encrypted message, which is SealedBox.Overhead
longer than the original message.
Decrypts the given sealed box using the recipient's key pair.
Returns the original message, or undefined
if decryption fails.
Length of public key of recipient in bytes.
Length of secret key of recipient in bytes.
Length of nonce in bytes.
Length of overhead added to box compared to original message.
Implements x25519.
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:
interface SignKeyPair {
publicKey: ByteArray; // Array with 32-byte public key
secretKey: ByteArray; // Array 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
sign_keyPair
or 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 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 undefined
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 seed for sign_keyPair_fromSeed
in bytes.
Length of signature in bytes.
Implements SHA-512.
Returns SHA-512 hash of the message.
Length of hash in bytes.
Implements HMAC-SHA-512-256
Authenticates the given message with the secret key. (In other words, returns HMAC-SHA-512-256 of the message under the key.)
Returns HMAC-SHA-512 (without truncation) of the message under the key
Length of authenticator returned by auth
.
Length of authenticator returned by auth_full
.
Length of key for auth
and auth_full
(key length is currently not
enforced).
Returns a ByteArray
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)If the platform doesn't provide a suitable PRNG, the following functions, which require random numbers, will throw exception:
randomBytes
box_keyPair
sign_keyPair
Other functions are deterministic and will continue working.
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.
TweetNaCl.js supports modern browsers that have a cryptographically secure pseudorandom number generator and typed arrays, including the latest versions of:
Other systems:
Install NPM modules needed for development:
$ npm install
To build js run compilation:
$ npm run compile
To run tests in Node:
$ npm run test-node
To run tests in browsers and Node (CI-mode):
$ npm run test
To run tests in browsers and Node (Dev-mode):
$ npm run test-dev