Socket
Socket
Sign inDemoInstall

sodium-native

Package Overview
Dependencies
Maintainers
2
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sodium-native - npm Package Compare versions

Comparing version 2.4.9 to 3.0.0

binding.c

11

CHANGELOG.md

@@ -5,2 +5,13 @@ # CHANGELOG

## v3.0.0
* Updated to use n-api (@chm-diederichs).
* Removed object instance apis and replaced them with init, update, final methods.
* Bumped dev dependencies.
* Bumped libsodium to 1.0.18.
## v2.4.10
* Prebuilds for Electron 8
## v2.4.9

@@ -7,0 +18,0 @@

10

libsodium/packaging/dotnet-core/README.md

@@ -34,4 +34,4 @@ This directory contains scripts and files to package libsodium for .NET Core.

binary. In this case, a package revision number is added as a fourth part to
the libsodium version, starting at `1`. For example, `1.0.17` is the initial
release of the package for libsodium 1.0.17 and `1.0.17.5` is the fifth
the libsodium version, starting at `1`. For example, `1.0.18` is the initial
release of the package for libsodium 1.0.18 and `1.0.18.5` is the fifth
revision (sixth release) of that package.

@@ -41,5 +41,5 @@ * *pre-release label*

`-preview-##` format where `##` is the number of the pre-release, starting at
`01`. For example, `1.0.17-preview-01` is the first pre-release of the package
for libsodium 1.0.17 and `1.0.17.5-preview-02` the second pre-release of the
fifth revision of the package for libsodium 1.0.17.
`01`. For example, `1.0.18-preview-01` is the first pre-release of the package
for libsodium 1.0.18 and `1.0.18.5-preview-02` the second pre-release of the
fifth revision of the package for libsodium 1.0.18.

@@ -46,0 +46,0 @@

{
"name": "sodium-native",
"version": "2.4.9",
"version": "3.0.0",
"description": "Low level bindings for libsodium",

@@ -8,11 +8,10 @@ "main": "index.js",

"ini": "^1.3.5",
"nan": "^2.14.0",
"node-gyp-build": "^4.1.0"
"node-gyp-build": "^4.2.0"
},
"devDependencies": {
"prebuildify": "^3.0.2",
"sodium-test": "^0.7.0",
"prebuildify": "^3.0.4",
"sodium-test": "^0.8.0",
"sodium-vectors": "^1.0.0",
"standard": "^12.0.1",
"tape": "^4.6.3"
"standard": "^14.3.1",
"tape": "^4.13.0"
},

@@ -24,4 +23,4 @@ "scripts": {

"install": "node-gyp-build \"node preinstall.js\" \"node postinstall.js\"",
"prebuild": "prebuildify -a --strip --preinstall \"node preinstall.js\" --postinstall \"node postinstall.js\"",
"prebuild-ia32": "prebuildify -a --strip --preinstall \"node preinstall.js\" --postinstall \"node postinstall.js\" --arch=ia32"
"prebuild": "prebuildify --napi --strip --preinstall \"node preinstall.js\" --postinstall \"node postinstall.js\"",
"prebuild-ia32": "prebuildify --arch=ia32 --napi --strip --preinstall \"node preinstall.js\" --postinstall \"node postinstall.js\""
},

@@ -34,3 +33,4 @@ "repository": {

"Emil Bay <github@tixz.dk> (http://bayes.dk)",
"Mathias Buus (@mafintosh)"
"Mathias Buus <mathiasbuus@gmail.com> (https://mafinto.sh)",
"Christophe Diederichs <chm-diederichs@hyperdivision.dk>"
],

@@ -37,0 +37,0 @@ "license": "MIT",

@@ -74,3 +74,3 @@ #!/usr/bin/env node

var possiblePaths = possiblePathSuffixes.map(p => process.env['PROGRAMFILES(X86)'] + p)
.concat(possiblePathSuffixes.map(p => process.env['PROGRAMFILES'] + p))
.concat(possiblePathSuffixes.map(p => process.env.PROGRAMFILES + p))

@@ -77,0 +77,0 @@ possiblePaths.push(process.env.WINDIR + '/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe')

@@ -46,1190 +46,8 @@ # sodium-native

## API
## Documentation
[**Go to docs for the latest release**](https://github.com/sodium-friends/sodium-native/tree/v2.4.8) (The following docs may be for a unreleased version)
Complete documentation may be found on the [sodium-friends website](https://sodium-friends.github.io/docs/docs/getstarted)
#### `var sodium = require('sodium-native')`
Loads the bindings. If you get an module version error you probably need to reinstall the module because you switched node versions.
### Memory Protection
Bindings to the secure memory API.
[See the libsodium "Securing memory allocations" docs for more information](https://download.libsodium.org/doc/memory_management).
#### `sodium.sodium_memzero(buffer)`
Zero out the data in `buffer`.
#### `sodium.sodium_mlock(buffer)`
Lock the memory contained in `buffer`
#### `sodium.sodium_munlock(buffer)`
Unlock previously `sodium_mlock`ed memory contained in `buffer`. This will also `sodium_memzero` `buffer`
#### `var buffer = sodium.sodium_malloc(size)`
Allocate a buffer of `size` which is memory protected. See [libsodium docs](https://download.libsodium.org/doc/memory_management#guarded-heap-allocations) for details. Be aware that many Buffer methods may break the security guarantees of `sodium.sodium_malloc`'ed memory. To check if a `Buffer` is a "secure" buffer,
you can call access the getter `buffer.secure` which will be `true`.
#### `sodium.sodium_mprotect_noaccess(buffer)`
Make `buffer` allocated using `sodium.sodium_malloc` inaccessible, crashing the process if any access is attempted.
Note that this will have no effect for normal `Buffer`s.
#### `sodium.sodium_mprotect_readonly(buffer)`
Make `buffer` allocated using `sodium.sodium_malloc` read-only, crashing the process if any writing is attempted.
Note that this will have no effect for normal `Buffer`s.
#### `sodium.sodium_mprotect_readwrite(buffer)`
Make `buffer` allocated using `sodium.sodium_malloc` read-write, undoing `sodium_mprotect_noaccess` or `sodium_mprotect_readonly`.
Note that this will have no effect for normal `Buffer`s.
### Generating random data
Bindings to the random data generation API.
[See the libsodium randombytes docs for more information](https://download.libsodium.org/doc/generating_random_data/).
#### `var uint32 = sodium.randombytes_random()`
Generate a random 32-bit unsigned integer `[0, 0xffffffff]` (both inclusive)
#### `var uint = sodium.randombytes_uniform(upper_bound)`
Generate a random 32-bit unsigned integer `[0, upper_bound)` (last exclusive).
`upper_bound` must be `0xffffffff` at most.
#### `sodium.randombytes_buf(buffer)`
Fill `buffer` with random data.
#### `sodium.randombytes_buf_deterministic(buffer, seed)`
Fill `buffer` with random data, generated from `seed`. `seed` must be a Buffer
of at least `sodium.randombytes_SEEDBYTES` length
### Helpers
Bindings to various helper functions.
[See the libsodium helpers docs for more information](https://download.libsodium.org/doc/helpers/).
### `var bool = sodium.sodium_memcmp(b1, b2)`
Compare `b1` with `b2`, in **constant-time** for `b1.length`.
* `b1` must be `Buffer`
* `b2` must be `Buffer` and must be `b1.length` bytes
Returns `true` when equal, otherwise `false`.
### `var direction = sodium.sodium_compare(b1, b2)`
Compare `b1` with `b2`, regarding either as little-endian encoded number.
* `b1` must be `Buffer`
* `b2` must be `Buffer` and must be `b1.length` bytes
Returns `1`, `0` or `-1` on whether `b1` is greater, equal or less than `b2`.
This is the same scheme as `Array.prototype.sort` expect.
### `sodium.sodium_add(a, b)`
Adds `b` to `a` (wrapping), regarding either as little-endian encoded number,
and writing the result into `a`.
* `a` must be `Buffer`
* `b` must be `Buffer` and must be `a.length` bytes
### `sodium.sodium_sub(a, b)`
Subtractss `b` from `a` (wrapping), regarding either as little-endian encoded
number, and writing the result into `a`.
* `a` must be `Buffer`
* `b` must be `Buffer` and must be `a.length` bytes
### `sodium.sodium_increment(buf)`
Increment `buf` as a little-endian number. This operation is **constant-time**
for the length of `buf`.
* `buf` must be `Buffer`
### `var bool = sodium.sodium_is_zero(buf, len)`
Test whether `buf` is all zero for `len` bytes. This operation is
**constant-time** for `len`.
* `len` must be integer at most the length of `buf`
Returns `true` if all `len` bytes are zero, otherwise `false`.
### Padding
Bindings to the padding API.
[See the libsodium padding docs for more information](https://download.libsodium.org/doc/padding).
#### `var paddedLength = sodium.sodium_pad(buf, unpaddedLength, blocksize)`
Pad `buf` with random data from index `unpaddedLength` up to closest multiple of
`blocksize`.
* `buf` must be `Buffer`
* `unpadded_buflen` must be integer at most `buf.length`
* `blocksize` must be integer greater than 1 but at most `buf.length`
Returns the length of the padded data (so you may `.slice` the buffer to here).
#### `var unpaddedLength = sodium.sodium_unpad(buf, paddedLength, blocksize)`
Calculate `unpaddedLength` from a padded `buf` with `blocksize`
* `buf` must be `Buffer`
* `padded_buflen` must be integer at most `buf.length`
* `blocksize` must be integer greater than 1 but at most `buf.length`
Returns the length of the unpadded data (so you may `.slice` the buffer to here).
### Signing
Bindings for the crypto_sign API.
[See the libsodium crypto_sign docs for more information](https://download.libsodium.org/doc/public-key_cryptography/public-key_signatures).
#### `crypto_sign_seed_keypair(publicKey, secretKey, seed)`
Create a new keypair based on a seed.
* `publicKey` should be a buffer with length `crypto_sign_PUBLICKEYBYTES`.
* `secretKey` should be a buffer with length `crypto_sign_SECRETKEYBYTES`.
* `seed` should be a buffer with length `crypto_sign_SEEDBYTES`.
The generated public and secret key will be stored in passed in buffers.
#### `crypto_sign_keypair(publicKey, secretKey)`
Create a new keypair.
* `publicKey` should be a buffer with length `crypto_sign_PUBLICKEYBYTES`.
* `secretKey` should be a buffer with length `crypto_sign_SECRETKEYBYTES`.
The generated public and secret key will be stored in passed in buffers.
#### `crypto_sign(signedMessage, message, secretKey)`
Sign a message.
* `signedMessage` should be a buffer with length `crypto_sign_BYTES + message.length`.
* `message` should be a buffer of any length.
* `secretKey` should be a secret key.
The generated signed message will be stored in `signedMessage`.
#### `var bool = crypto_sign_open(message, signedMessage, publicKey)`
Verify and open a message.
* `message` should be a buffer with length `signedMessage.length - crypto_sign_BYTES`.
* `signedMessage` at least `crypto_sign_BYTES` length.
* `publicKey` should be a public key.
Will return `true` if the message could be verified. Otherwise `false`.
If verified the originally signed message is stored in the `message` buffer.
#### `crypto_sign_detached(signature, message, secretKey)`
Same as `crypto_sign` except it only stores the signature.
* `signature` should be a buffer with length `crypto_sign_BYTES`.
* `message` should be a buffer of any length.
* `secretKey` should be a secret key.
The generated signature is stored in `signature`.
#### `var bool = crypto_sign_verify_detached(signature, message, publicKey)`
Verify a signature.
* `signature` should be a buffer with length `crypto_sign_BYTES`.
* `message` should be a buffer of any length.
* `publicKey` should be a public key.
Will return `true` if the message could be verified. Otherwise `false`.
#### `crypto_sign_ed25519_pk_to_curve25519(curve_pk, ed_pk)`
Convert an ed25519 public key to curve25519 (which can be used with `box` and `scalarmult`)
* `curve_pk` should be a buffer with length `crypto_box_PUBLICKEYBYTES`
* `ed_pk` should be a buffer with length `crypto_sign_PUBLICKEYBYTES`
#### `crypto_sign_ed25519_sk_to_curve25519(curve_sk, ed_sk)`
Convert an ed25519 secret key to curve25519 (which can be used with `box` and `scalarmult`)
* `curve_sk` should be a buffer with length `crypto_box_SECRETKEYBYTES`
* `ed_sk` should be a buffer with length `crypto_sign_SECRETKEYBYTES`
#### `crypto_sign_ed25519_sk_to_pk(pk, sk)`
Extract an ed25519 public key from an ed25519 secret key
* `pk` must be `Buffer` of at least `crypto_sign_PUBLICKEYBYTES` bytes
* `sk` must be `Buffer` of at least `crypto_sign_SECRETKEYBYTES` bytes
### Generic hashing
Bindings for the crypto_generichash API.
[See the libsodium crypto_generichash docs for more information](https://download.libsodium.org/doc/hashing/generic_hashing).
#### `crypto_generichash(output, input, [key])`
Hash a value with an optional key using the generichash method.
* `output` should be a buffer with length within `crypto_generichash_BYTES_MIN` - `crypto_generichash_BYTES_MAX`.
* `input` should be a buffer of any length.
* `key` is an optional buffer of length within `crypto_generichash_KEYBYTES_MIN` - `crypto_generichash_KEYBYTES_MAX`.
The generated hash is stored in `output`.
Also exposes `crypto_generichash_BYTES` and `crypto_generichash_KEYBYTES` that can be used as "default" buffer sizes.
#### `crypto_generichash_batch(output, inputArray, [key])`
Same as `crypto_generichash` except this hashes an array of buffers instead of a single one.
#### `var instance = crypto_generichash_instance([key], [outputLength])`
Create a generichash instance that can hash a stream of input buffers.
* `key` is an optional buffer as above.
* `outputLength` the buffer size of your output.
#### `instance.update(input)`
Update the instance with a new piece of data.
* `input` should be a buffer of any size.
#### `instance.final(output)`
Finalize the instance.
* `output` should be a buffer as above with the same length you gave when creating the instance.
The generated hash is stored in `output`.
### Public / secret key box encryption
Bindings for the crypto_box API.
[See the libsodium crypto_box docs for more information](https://download.libsodium.org/doc/public-key_cryptography/authenticated_encryption).
#### `crypto_box_seed_keypair(publicKey, secretKey, seed)`
Create a new keypair based on a seed.
* `publicKey` should be a buffer with length `crypto_box_PUBLICKEYBYTES`.
* `secretKey` should be a buffer with length `crypto_box_SECRETKEYBYTES`.
* `seed` should be a buffer with length `crypto_box_SEEDBYTES`.
The generated public and secret key will be stored in passed in buffers.
#### `crypto_box_keypair(publicKey, secretKey)`
Create a new keypair.
* `publicKey` should be a buffer with length `crypto_box_PUBLICKEYBYTES`.
* `secretKey` should be a buffer with length `crypto_box_SECRETKEYBYTES`.
The generated public and secret key will be stored in passed in buffers.
#### `crypto_box_detached(ciphertext, mac, message, nonce, publicKey, secretKey)`
Encrypt a message.
* `ciphertext` should be a buffer with length `message.length`.
* `mac` should be a buffer with length `crypto_box_MACBYTES`.
* `message` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_box_NONCEBYTES`.
* `publicKey` should be a public key.
* `secretKey` should be a secret key.
The encrypted message will be stored in `ciphertext` and the authentification code will be stored in `mac`.
#### `crypto_box_easy(ciphertext, message, nonce, publicKey, secretKey)`
Same as `crypto_box_detached` except it encodes the mac in the message.
* `ciphertext` should be a buffer with length `message.length + crypto_box_MACBYTES`.
* `message` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_box_NONCEBYTES`.
* `publicKey` should be a public key.
* `secretKey` should be a secret key.
The encrypted message and authentification code will be stored in `ciphertext`.
#### `var bool = crypto_box_open_detached(message, ciphertext, mac, nonce, publicKey, secretKey)`
Decrypt a message.
* `message` should be a buffer with length `ciphertext.length`.
* `mac` should be a buffer with length `crypto_box_MACBYTES`.
* `ciphertext` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_box_NONCEBYTES`.
* `publicKey` should be a public key.
* `secretKey` should be a secret key.
Returns `true` if the message could be decrypted. Otherwise `false`.
The decrypted message will be stored in `message`.
#### `var bool = crypto_box_open_easy(message, ciphertext, nonce, publicKey, secretKey)`
Decrypt a message encoded with the easy method.
* `message` should be a buffer with length `ciphertext.length - crypto_box_MACBYTES`.
* `ciphertext` should be a buffer with length at least `crypto_box_MACBYTES`.
* `nonce` should be a buffer with length `crypto_box_NONCEBYTES`.
* `publicKey` should be a public key.
* `secretKey` should be a secret key.
Returns `true` if the message could be decrypted. Otherwise `false`.
The decrypted message will be stored in `message`.
### Sealed box encryption
Bindings for the crypto_box_seal API.
[See the libsodium crypto_box_seal docs for more information](https://download.libsodium.org/doc/public-key_cryptography/sealed_boxes).
Keypairs can be generated with `crypto_box_keypair()` or `crypto_box_seed_keypair()`.
#### `crypto_box_seal(ciphertext, message, publicKey)`
Encrypt a message in a sealed box using a throwaway keypair.
The ciphertext cannot be associated with the sender due to the sender's key
being a single use keypair that is overwritten during encryption.
* `ciphertext` should be a buffer with length at least `message.length + crypto_box_SEALBYTES`.
* `message` should be a buffer with any length.
* `publicKey` should be the receipent's public key.
#### `var bool = crypto_box_seal_open(message, ciphertext, publicKey, secretKey)`
Decrypt a message encoded with the sealed box method.
* `message` should be a buffer with length at least `ciphertext.length - crypto_box_SEALBYTES`.
* `ciphertext` should be a buffer with length at least `crypto_box_SEALBYTES`.
* `publicKey` should be the receipient's public key.
* `secretKey` should be the receipient's secret key.
Note: the keypair of the recipient is required here, both public and secret key.
This is because during encryption the recipient's public key is used to generate
the nonce. The throwaway public key generated by the sender is stored in the first
`crypto_box_PUBLICKEYBYTE`'s of the ciphertext.
### Secret key box encryption
Bindings for the crypto_secretbox API.
[See the libsodium crypto_secretbox docs for more information](https://download.libsodium.org/doc/secret-key_cryptography/secretbox).
#### `crypto_secretbox_detached(ciphertext, mac, message, nonce, secretKey)`
Encrypt a message.
* `ciphertext` should be a buffer with length `message.length`.
* `mac` should be a buffer with length `crypto_secretbox_MACBYTES`.
* `message` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_secretbox_NONCEBYTES`.
* `secretKey` should be a secret key with length `crypto_secretbox_KEYBYTES`.
The encrypted message will be stored in `ciphertext` and the authentification code will be stored in `mac`.
#### `crypto_secretbox_easy(ciphertext, message, nonce, secretKey)`
Same as `crypto_secretbox_detached` except it encodes the mac in the message.
* `ciphertext` should be a buffer with length `message.length + crypto_secretbox_MACBYTES`.
* `message` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_secretbox_NONCEBYTES`.
* `secretKey` should be a secret key with length `crypto_secretbox_KEYBYTES`.
#### `var bool = crypto_secretbox_open_detached(message, ciphertext, mac, nonce, secretKey)`
Decrypt a message.
* `message` should be a buffer with length `ciphertext.length`.
* `mac` should be a buffer with length `crypto_secretbox_MACBYTES`.
* `ciphertext` should be a buffer of any length.
* `nonce` should be a buffer with length `crypto_secretbox_NONCEBYTES`.
* `secretKey` should be a secret key.
Returns `true` if the message could be decrypted. Otherwise `false`.
The decrypted message will be stored in `message`.
#### `var bool = crypto_secretbox_open_easy(message, ciphertext, nonce, secretKey)`
Decrypt a message encoded with the easy method.
* `message` should be a buffer with length `ciphertext.length - crypto_secretbox_MACBYTES`.
* `ciphertext` should be a buffer with length at least `crypto_secretbox_MACBYTES`.
* `nonce` should be a buffer with length `crypto_secretbox_NONCEBYTES`.
* `secretKey` should be a secret key.
Returns `true` if the message could be decrypted. Otherwise `false`.
The decrypted message will be stored in `message`.
### AEAD (Authenticated Encryption with Additional Data)
Bindings for the crypto_aead_* APIs.
[See the libsodium AEAD docs for more information](https://download.libsodium.org/doc/secret-key_cryptography/aead).
Currently only `crypto_aead_xchacha20poly1305_ietf` is exposed.
### Constants
#### Buffer lengths (Integer)
- `crypto_aead_xchacha20poly1305_ietf_ABYTES`
- `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`
- `crypto_aead_xchacha20poly1305_ietf_NPUBBYTES`
- `crypto_aead_xchacha20poly1305_ietf_NSECBYTES`
- `crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX` - Note this is `Number.MAX_SAFE_INTEGER` for now
#### `crypto_aead_xchacha20poly1305_ietf_keygen(key)`
Generate a new encryption key.
* `key` should be a buffer of length `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`.
The generated key is stored in `key`.
### `var clen = crypto_aead_xchacha20poly1305_ietf_encrypt(ciphertext, message, [ad], null, npub, key)`
Encrypt a message with (`npub`, `key`) and optional additional data `ad`.
* `ciphertext` should be a `Buffer` of size
`message.length + crypto_aead_xchacha20poly1305_ietf_ABYTES`.
* `message` should be a `Buffer`.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of authentication tag appended to the message.
* `null` is in the position of the unused `nsec` argument. This should always be
`null`.
* `npub` should be `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_NPUBBYTES`.
* `key` should be a `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`.
Returns how many bytes were written to `ciphertext`. Note that in-place
encryption is possible.
### `var mlen = crypto_aead_xchacha20poly1305_ietf_decrypt(message, null, ciphertext, [ad], npub, key)`
Decrypt a message with (`npub`, `key`) and optional additional data `ad`.
* `message` should be a `Buffer` of size
`ciphertext.length - crypto_aead_xchacha20poly1305_ietf_ABYTES`.
* `null` is in the position of the unused `nsec` argument. This should always be
`null`.
* `ciphertext` should be a `Buffer`.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of authentication tag appended to the message.
* `npub` should be `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_NPUBBYTES`.
* `key` should be a `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`.
Returns how many bytes were written to `message`. Note that in-place
encryption is possible.
### `var maclen = crypto_aead_xchacha20poly1305_ietf_encrypt_detached(ciphertext, mac, message, [ad], null, npub, key)`
Encrypt a message with (`npub`, `key`) and optional additional data `ad`.
* `ciphertext` should be a `Buffer` of size `message.length`.
* `mac` should be `Buffer` of size `crypto_aead_xchacha20poly1305_ietf_ABYTES`.
* `message` should be a `Buffer`.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of authentication tag appended to the message.
* `null` is in the position of the unused `nsec` argument. This should always be
`null`.
* `npub` should be `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_NPUBBYTES`.
* `key` should be a `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`.
Returns how many bytes were written to `mac`. Note that in-place
encryption is possible.
### `crypto_aead_xchacha20poly1305_ietf_decrypt_detached(message, null, ciphertext, mac, [ad], npub, key)`
Decrypt a message with (`npub`, `key`) and optional additional data `ad`.
* `message` should be a `Buffer` of size `ciphertext.length`.
* `null` is in the position of the unused `nsec` argument. This should always be
`null`.
* `ciphertext` should be a `Buffer`.
* `mac` should be `Buffer` of size `crypto_aead_xchacha20poly1305_ietf_ABYTES`.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of authentication tag appended to the message.
* `npub` should be `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_NPUBBYTES`.
* `key` should be a `Buffer` of length `crypto_aead_xchacha20poly1305_ietf_KEYBYTES`.
Returns nothing, but will throw on in case the MAC cannot be authenticated. Note
that in-place encryption is possible.
### Non-authenticated streaming encryption
Bindings for the crypto_stream API.
[See the libsodium crypto_stream docs for more information](https://download.libsodium.org/doc/advanced/stream_ciphers/xsalsa20).
#### `crypto_stream(ciphertext, nonce, key)`
Generate random data based on a nonce and key into the ciphertext.
* `ciphertext` should be a buffer of any size.
* `nonce` should be a buffer with length `crypto_stream_NONCEBYTES`.
* `key` should be a secret key with length `crypto_stream_KEYBYTES`.
The generated data is stored in `ciphertext`.
#### `crypto_stream_xor(ciphertext, message, nonce, key)` or
#### `crypto_stream_chacha20_xor(ciphertext, message, nonce, key)`
Encrypt, but *not* authenticate, a message based on a nonce and key
* `ciphertext` should be a buffer with length `message.length`.
* `message` should be a buffer of any size.
* `nonce` should be a buffer with length `crypto_stream_NONCEBYTES`.
* `key` should be a secret key with length `crypto_stream_KEYBYTES`.
The encrypted data is stored in `ciphertext`. To decrypt, swap `ciphertext` and `message`.
Also supports in-place encryption where you use the same buffer as `ciphertext` and `message`.
Encryption defaults to XSalsa20, use `crypto_stream_chacha20_xor` if you want
to encrypt/decrypt with ChaCha20 instead.
#### `var instance = crypto_stream_xor_instance(nonce, key)` or
#### `var instance = crypto_stream_chacha20_xor_instance(nonce, key)`
A streaming instance to the `crypto_stream_xor` api. Pass a nonce and key in the constructor.
Encryption defaults to XSalsa20, use `crypto_stream_chacha20_xor_instance` if
you want to encrypt/decrypt with ChaCha20 instead.
#### `instance.update(ciphertext, message)`
Encrypt the next message
#### `instance.final()`
Finalize the stream. Zeros out internal state.
### Authentication
Bindings for the crypto_auth API.
[See the libsodium crypto_auth docs for more information](https://download.libsodium.org/doc/secret-key_cryptography/secret-key_authentication).
#### `crypto_auth(output, input, key)`
Create an authentication token.
* `output` should be a buffer of length `crypto_auth_BYTES`.
* `input` should be a buffer of any size.
* `key` should be a buffer of length `crypto_auth_KEYBYTES`.
The generated token is stored in `output`.
#### `var bool = crypto_auth_verify(output, input, key)`
Verify a token.
* `output` should be a buffer of length `crypto_auth_BYTES`.
* `input` should be a buffer of any size.
* `key` should be a buffer of length `crypto_auth_KEYBYTES`.
Returns `true` if the token could be verified. Otherwise `false`.
### Stream encryption
Bindings for the crypto_secretstream API.
[See the libsodium crypto_secretstream docs for more information](https://download.libsodium.org/doc/secret-key_cryptography/secretstream).
### Constants
#### Buffer lengths (Integer)
- `crypto_secretstream_xchacha20poly1305_ABYTES`
- `crypto_secretstream_xchacha20poly1305_HEADERBYTES`
- `crypto_secretstream_xchacha20poly1305_KEYBYTES`
- `crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX`
- `crypto_secretstream_xchacha20poly1305_TAGBYTES` - NOTE: Unofficial constant
#### Message tags (`Buffer`)
- `crypto_secretstream_xchacha20poly1305_TAG_MESSAGE`
- `crypto_secretstream_xchacha20poly1305_TAG_PUSH`
- `crypto_secretstream_xchacha20poly1305_TAG_REKEY`
- `crypto_secretstream_xchacha20poly1305_TAG_FINAL`
### `crypto_secretstream_xchacha20poly1305_keygen(key)`
Generate a new encryption key.
* `key` should be a buffer of length `crypto_secretstream_xchacha20poly1305_KEYBYTES`.
The generated key is stored in `key`.
### `var state = crypto_secretstream_xchacha20poly1305_state_new()`
Create a new stream state. Returns an opaque object used in the next methods.
### `crypto_secretstream_xchacha20poly1305_init_push(state, header, key)`
Initialise `state` from the writer side with message `header` and
encryption key `key`. The header must be sent or stored with the stream.
The key must be exchanged securely with the receiving / reading side.
* `state` should be an opaque state object.
* `header` should be a buffer of size `crypto_secretstream_xchacha20poly1305_HEADERBYTES`.
* `key` should be a buffer of length `crypto_secretstream_xchacha20poly1305_KEYBYTES`.
### `var mlen = crypto_secretstream_xchacha20poly1305_push(state, ciphertext, message, [ad], tag)`
Encrypt a message with a certain tag and optional additional data `ad`.
* `state` should be an opaque state object.
* `ciphertext` should be a buffer of size `message.length + crypto_secretstream_xchacha20poly1305_ABYTES`.
* `message` should be a buffer.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of authentication tag appended to the message.
* `tag` should be `Buffer` of length `crypto_secretstream_xchacha20poly1305_TAGBYTES`
Note that `tag` should be one of the `crypto_secretstream_xchacha20poly1305_TAG_*` constants.
Returns number of encrypted bytes written to `ciphertext`.
### `crypto_secretstream_xchacha20poly1305_init_pull(state, header, key)`
Initialise `state` from the reader side with message `header` and
encryption key `key`. The header must be retrieved from somewhere.
The key must be exchanged securely with the sending / writing side.
* `state` should be an opaque state object.
* `header` should be a buffer of size `crypto_secretstream_xchacha20poly1305_HEADERBYTES`.
* `key` should be a buffer of length `crypto_secretstream_xchacha20poly1305_KEYBYTES`.
### `var clen = crypto_secretstream_xchacha20poly1305_pull(state, message, tag, ciphertext, [ad])`
Decrypt a message with optional additional data `ad`, and write message tag to
`tag`. Make sure to check this!
* `state` should be an opaque state object.
* `message` should be a buffer of size `ciphertext.length - crypto_secretstream_xchacha20poly1305_ABYTES`.
* `tag` should be a buffer of `crypto_secretstream_xchacha20poly1305_TAGBYTES`.
* `ad` is optional and should be `null` or `Buffer`. Included in the computation
of the authentication tag appended to the message.
Note that `tag` should be one of the `crypto_secretstream_xchacha20poly1305_TAG_*` constants.
Returns number of decrypted bytes written to `message`.
### `crypto_secretstream_xchacha20poly1305_rekey(state)`
Rekey the opaque `state` object.
### One-time Authentication
Bindings for the crypto_onetimeauth API.
[See the libsodium crypto_onetimeauth docs for more information](https://download.libsodium.org/doc/advanced/poly1305).
#### `crypto_onetimeauth(output, input, key)`
Create a authentication token based on a onetime key.
* `output` should be a buffer of length `crypto_onetimauth_BYTES`.
* `input` should be a buffer of any size.
* `key` should be a buffer of length `crypto_onetimeauth_KEYBYTES`.
The generated token is stored in `output`.
#### `var bool = crypto_onetimeauth_verify(output, input, key)`
Verify a token.
* `output` should be a buffer of length `crypto_onetimeauth_BYTES`.
* `input` should be a buffer of any size.
* `key` should be a buffer of length `crypto_onetimeauth_KEYBYTES`.
Returns `true` if the token could be verified. Otherwise `false`.
#### `var instance = crypto_onetimeauth_instance(key)`
Create an instance that create a token from a onetime key and a stream of input data.
* `key` should be a buffer of length `crypto_onetimeauth_KEYBYTES`.
#### `instance.update(input)`
Update the instance with a new piece of data.
* `input` should be a buffer of any size.
#### `instance.final(output)`
Finalize the instance.
* `output` should be a buffer of length `crypto_onetimeauth_BYTES`.
The generated hash is stored in `output`.
### Password Hashing
Bindings for the crypto_pwhash API.
[See the libsodium crypto_pwhash docs for more information](https://download.libsodium.org/doc/password_hashing/).
#### `crypto_pwhash(output, password, salt, opslimit, memlimit, algorithm)`
Create a password hash.
* `output` should be a buffer with length within `crypto_pwhash_BYTES_MIN` - `crypto_pwhash_BYTES_MAX`.
* `password` should be a buffer of any size.
* `salt` should be a buffer with length `crypto_pwhash_SALTBYTES`.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_OPSLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_MEMLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
* `algorithm` should be a number specifying the algorithm you want to use.
Available default ops and mem limits are
* `crypto_pwhash_OPSLIMIT_INTERACTIVE`
* `crypto_pwhash_OPSLIMIT_MODERATE`
* `crypto_pwhash_OPSLIMIT_SENSITIVE`
* `crypto_pwhash_MEMLIMIT_INTERACTIVE`
* `crypto_pwhash_MEMLIMIT_MODERATE`
* `crypto_pwhash_MEMLIMIT_SENSITIVE`
The available algorithms are
* `crypto_pwhash_ALG_DEFAULT`
* `crypto_pwhash_ALG_ARGON2ID13`
* `crypto_pwhash_ALG_ARGON2I13`
The generated hash will be stored in `output` and the entire `output` buffer will be used.
#### `crypto_pwhash_str(output, password, opslimit, memlimit)`
Create a password hash with a random salt.
* `output` should be a buffer with length `crypto_pwhash_STRBYTES`.
* `password` should be a buffer of any size.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_OPSLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_MEMLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
The generated hash, settings, salt, version and algorithm will be stored in `output` and the entire `output` buffer will be used.
#### `var bool = crypto_pwhash_str_verify(str, password)`
Verify a password hash generated with the above method.
* `str` should be a buffer with length `crypto_pwhash_STRBYTES`.
* `password` should be a buffer of any size.
Returns `true` if the hash could be verified with the settings contained in `str`. Otherwise `false`.
#### `var bool = crypto_pwhash_str_needs_rehash(hash, opslimit, memlimit)`
Check if a password hash needs rehash, either because the default algorithm
changed, opslimit or memlimit increased or because the hash is malformed.
* `hash` should be a buffer with length `crypto_pwhash_STRBYTES`.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_OPSLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_MEMLIMIT_MIN` - `crypto_pwhash_OPSLIMIT_MAX`.
Returns `true` if the hash should be rehashed the settings contained in `str`.
Otherwise `false` if it is still good.
#### `crypto_pwhash_async(output, password, salt, opslimit, memlimit, algorithm, callback)`
Just like `crypto_pwhash` but will run password hashing on a separate worker so it will not block the event loop. `callback(err)` will receive any errors from the hashing but all argument errors will `throw`. The resulting hash is written to `output`. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_async`
#### `crypto_pwhash_str_async(output, password, opslimit, memlimit, callback)`
Just like `crypto_pwhash_str` but will run password hashing on a separate worker so it will not block the event loop. `callback(err)` will receive any errors from the hashing but all argument errors will `throw`. The resulting hash with parameters is written to `output`. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_str_async`
#### `crypto_pwhash_str_verify_async(str, password, callback)`
Just like `crypto_pwhash_str_verify` but will run password hashing on a separate worker so it will not block the event loop. `callback(err, bool)` will receive any errors from the hashing but all argument errors will `throw`. If the verification succeeds `bool` is `true`, otherwise `false`. Due to an issue with libsodium `err` is currently never set. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_str_verify_async`
### Password Hashing (Scrypt)
Bindings for the crypto_pwhash_scryptsalsa208sha256 API.
[See the libsodium crypto_pwhash_scryptsalsa208sha256 docs for more information](https://download.libsodium.org/doc/advanced/scrypt).
#### `crypto_pwhash_scryptsalsa208sha256(output, password, salt, opslimit, memlimit)`
Create a password hash.
* `output` should be a buffer with length within `crypto_pwhash_scryptsalsa208sha256_BYTES_MIN` - `crypto_pwhash_scryptsalsa208sha256_BYTES_MAX`.
* `password` should be a buffer of any size.
* `salt` should be a buffer with length `crypto_pwhash_scryptsalsa208sha256_SALTBYTES`.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
Available default ops and mem limits are
* `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE`
* `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE`
* `crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE`
* `crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE`
The generated hash will be stored in `output` and the entire `output` buffer will be used.
#### `crypto_pwhash_scryptsalsa208sha256_str(output, password, opslimit, memlimit)`
Create a password hash with a random salt.
* `output` should be a buffer with length `crypto_pwhash_scryptsalsa208sha256_STRBYTES`.
* `password` should be a buffer of any size.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
The generated hash, settings, salt, version and algorithm will be stored in `output` and the entire `output` buffer will be used.
#### `var bool = crypto_pwhash_scryptsalsa208sha256_str_verify(str, password)`
Verify a password hash generated with the above method.
* `str` should be a buffer with length `crypto_pwhash_scryptsalsa208sha256_STRBYTES`.
* `password` should be a buffer of any size.
Returns `true` if the hash could be verified with the settings contained in `str`. Otherwise `false`.
#### `var bool = crypto_pwhash_scryptsalsa208sha256_str_needs_rehash(hash, opslimit, memlimit)`
Check if a password hash needs rehash, either because opslimit or memlimit
increased or because the hash is malformed.
* `hash` should be a buffer with length `crypto_pwhash_scryptsalsa208sha256_STRBYTES`.
* `opslimit` should a be number containing your ops limit setting in the range `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
* `memlimit` should a be number containing your mem limit setting in the range `crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN` - `crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX`.
Returns `true` if the hash should be rehashed the settings contained in `str`.
Otherwise `false` if it is still good.
#### `crypto_pwhash_scryptsalsa208sha256_async(output, password, salt, opslimit, memlimit, callback)`
Just like `crypto_pwhash_scryptsalsa208sha256` but will run password hashing on a separate worker so it will not block the event loop. `callback(err)` will receive any errors from the hashing but all argument errors will `throw`. The resulting hash is written to `output`. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_scryptsalsa208sha256_async`
#### `crypto_pwhash_scryptsalsa208sha256_str_async(output, password, opslimit, memlimit, callback)`
Just like `crypto_pwhash_scryptsalsa208sha256_str` but will run password hashing on a separate worker so it will not block the event loop. `callback(err)` will receive any errors from the hashing but all argument errors will `throw`. The resulting hash with parameters is written to `output`. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_scryptsalsa208sha256_str_async`
#### `crypto_pwhash_scryptsalsa208sha256_str_verify_async(str, password, callback)`
Just like `crypto_pwhash_scryptsalsa208sha256_str_verify` but will run password hashing on a separate worker so it will not block the event loop. `callback(err, bool)` will receive any errors from the hashing but all argument errors will `throw`. If the verification succeeds `bool` is `true`, otherwise `false`. Due to an issue with libsodium `err` is currently never set. This function also supports [`async_hook`s](https://nodejs.org/dist/latest/docs/api/async_hooks.html) as the type `sodium-native:crypto_pwhash_scryptsalsa208sha256_str_verify_async`
### Key exchange
Bindings for the crypto_kx API.
[See the libsodium crypto_kx docs for more information](https://download.libsodium.org/doc/key_exchange/).
#### `crypto_kx_keypair(publicKey, secretKey)`
Create a key exchange key pair.
* `publicKey` should be a buffer of length `crypto_kx_PUBLICKEYBYTES`.
* `secretKey` should be a buffer of length `crypto_kx_SECRETKEYBYTES`.
#### `crypto_kx_seed_keypair(publicKey, secretKey, seed)`
Create a key exchange key pair based on a seed.
* `publicKey` should be a buffer of length `crypto_kx_PUBLICKEYBYTES`.
* `secretKey` should be a buffer of length `crypto_kx_SECRETKEYBYTES`.
* `seed` should be a buffer of length `crypto_kx_SEEDBYTES`
#### `crypto_kx_client_session_keys(rx, tx, clientPublicKey, clientSecretKey, serverPublicKey)`
Generate a session receive and transmission key for a client.
The public / secret keys should be generated using the key pair method above.
* `rx` should be a buffer of length `crypto_kx_SESSIONKEYBYTES` or `null`.
* `tx` should be a buffer of length `crypto_kx_SESSIONKEYBYTES` or `null`.
You should use the `rx` to decrypt incoming data and `tx` to encrypt outgoing.
If you need to make a one-way or half-duplex channel you can give only one of
`rx` or `tx`.
#### `crypto_kx_server_session_keys(rx, tx, serverPublicKey, serverSecretKey, clientPublicKey)`
Generate a session receive and transmission key for a server.
The public / secret keys should be generated using the key pair method above.
* `rx` should be a buffer of length `crypto_kx_SESSIONKEYBYTES` or `null`.
* `tx` should be a buffer of length `crypto_kx_SESSIONKEYBYTES` or `null`.
You should use the `rx` to decrypt incoming data and `tx` to encrypt outgoing.
If you need to make a one-way or half-duplex channel you can give only one of
`rx` or `tx`.
### Diffie-Hellman
Bindings for the crypto_scalarmult API.
[See the libsodium crypto_scalarmult docs for more information](https://download.libsodium.org/doc/advanced/scalar_multiplication).
#### `crypto_scalarmult_base(publicKey, secretKey)`
Create a scalar multiplication public key based on a secret key
* `publicKey` should be a buffer of length `crypto_scalarmult_BYTES`.
* `secretKey` should be a buffer of length `crypto_scalarmult_SCALARBYTES`.
The generated public key is stored in `publicKey`.
#### `crypto_scalarmult(sharedSecret, secretKey, remotePublicKey)`
Derive a shared secret from a local secret key and a remote public key.
* `sharedSecret` shoudl be a buffer of length `crypto_scalarmult_BYTES`.
* `secretKey` should be a buffer of length `crypto_scalarmult_SCALARBYTES`.
* `remotePublicKey` should be a buffer of length `crypto_scalarmult_BYTES`.
The generated shared secret is stored in `sharedSecret`.
### Finite field aritmhetic
Bindings for the crypto_scalarmult_ed25519 and crypto_core_ed25519 API.
[See the libsodium docs for more information](https://download.libsodium.org/doc/advanced/point-arithmetic).
#### Constants
* `crypto_scalarmult_ed25519_BYTES`
* `crypto_scalarmult_ed25519_SCALARBYTES`
* `crypto_core_ed25519_BYTES`
* `crypto_core_ed25519_UNIFORMBYTES`
* `crypto_core_ed25519_SCALARBYTES`
* `crypto_core_ed25519_NONREDUCEDSCALARBYTES`
#### `var bool = crypto_core_ed25519_is_valid_point(p)`
> The crypto_core_ed25519_is_valid_point() function checks that p represents
> a point on the edwards25519 curve, in canonical form, on the main subgroup,
> and that the point doesn't have a small order.
* `p` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
Returns `true` or `false`
#### `crypto_core_ed25519_from_uniform(p, r)`
Maps a `crypto_core_ed25519_UNIFORMBYTES` bytes vector (usually the output of
a hash function) to a a valid curve point and stores its compressed
representation in `p`.
The point is guaranteed to be on the main subgroup.
* `p` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
* `r` must be `Buffer` of at least `crypto_core_ed25519_UNIFORMBYTES` bytes
#### `crypto_scalarmult_ed25519(q, n, p)`
Multiply point `p` by scalar `n` and store its compressed representation in `q`.
* `q` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
* `n` must be `Buffer` of at least `crypto_scalarmult_ed25519_SCALARBYTES` bytes
* `p` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
Note this function will throw if `n` is zero or `p` is an invalid curve point.
#### `crypto_scalarmult_ed25519_base(q, n)`
Multiply the basepoint by scalar `n` and store its compressed representation in
`q`. Note that `n` will be clamped.
* `q` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
* `n` must be `Buffer` of at least `crypto_scalarmult_ed25519_SCALARBYTES` bytes
Note this function will throw if `n` is zero
#### `crypto_scalarmult_ed25519_noclamp(q, n, p)`
Multiply point `p` by scalar `n` and store its compressed representation in `q`.
This version does not clamp.
* `q` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
* `n` must be `Buffer` of at least `crypto_scalarmult_ed25519_SCALARBYTES` bytes
* `p` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
Note this function will throw if `n` is zero or `p` is an invalid curve point.
#### `crypto_scalarmult_ed25519_base_noclamp(q, n)`
Multiply the basepoint by scalar `n` and store its compressed representation in
`q`. This version does not clamp.
* `q` must be `Buffer` of at least `crypto_scalarmult_ed25519_BYTES` bytes
* `n` must be `Buffer` of at least `crypto_scalarmult_ed25519_SCALARBYTES` bytes
Note this function will throw if `n` is zero
#### `crypto_core_ed25519_add(r, p, q)`
Add point `q` to `p`, storing the result to `r`.
* `r` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
* `p` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
* `q` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
Will throw if `p`, `q` are not valid curve points
#### `crypto_core_ed25519_sub(r, p, q)`
Subtract point `q` to `p`, storing the result to `r`.
* `r` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
* `p` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
* `q` must be `Buffer` of at least `crypto_core_ed25519_BYTES` bytes
Will throw if `p`, `q` are not valid curve points
#### `crypto_core_ed25519_scalar_random(r)`
Generate random scalar in `]0..L[`, storing it in `r`.
* `r` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_reduce(r, s)`
Reduce `s mod L`, storing it in `r`.
* `r` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `s` must be `Buffer` of at least `crypto_core_ed25519_NONREDUCEDSCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_invert(recip, s)`
Find `recip` such that `s * recip = 1 (mod L)`, storing it in `recip`.
* `recip` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `s` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_negate(neg, s)`
Find `neg` such that `s + neg = 0 (mod L)`, storing it in `recip`.
* `recip` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `s` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_complement(comp, s)`
Find `comp` such that `s + comp = 1 (mod L)`, storing it in `recip`.
* `comp` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `s` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_add(z, x, y)`
Add `x` and `y` such that `x + y = z (mod L)`, storing it in `z`.
* `x` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `y` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `z` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
#### `crypto_core_ed25519_scalar_sub(z, x, y)`
Subtract `x` and `y` such that `x - y = z (mod L)`, storing it in `z`.
* `x` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `y` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
* `z` must be `Buffer` of at least `crypto_core_ed25519_SCALARBYTES` bytes
### Short hashes
Bindings for the crypto_shorthash API.
[See the libsodium crypto_shorthash docs for more information](https://download.libsodium.org/doc/hashing/short-input_hashing).
#### `crypto_shorthash(output, input, key)`
Hash a value to a short hash based on a key.
* `output` should be a buffer of length `crypto_shorthash_BYTES`.
* `input` should be a buffer of any size.
* `key` should be a buffer of length `crypto_shorthash_KEYBYTES`.
The generated short hash is stored in `output`.
### Key derivation
Bindings for the crypto_kdf API.
[See the libsodium crypto_kdf docs for more information](https://download.libsodium.org/doc/key_derivation/).
#### `crypto_kdf_keygen(key)`
Generate a new master key.
* `key` should be a buffer of length `crypto_kdf_KEYBYTES`
#### `crypto_kdf_derive_from_key(subkey, subkeyId, context, key)`
Derive a new key from a master key.
* `subkey` should be a buffer between `crypto_kdf_BYTES_MIN` and `crypto_kdf_BYTES_MAX`.
* `subkeyId` should be an integer.
* `context` should be a buffer of length `crypto_kdf_CONTEXTBYTES`
* `key` should by a buffer of length `crypto_kdf_KEYBYTES`
### SHA
#### `crypto_hash_sha256(output, input)`
Hash a value to a short hash based on a key.
* `output` should be a buffer of length `crypto_hash_sha256_BYTES`.
* `input` should be a buffer of any size.
The generated short hash is stored in `output`.
#### `var instance = crypto_hash_sha256_instance()`
Create an instance that has stream of input data to sha256.
#### `instance.update(input)`
Update the instance with a new piece of data.
* `input` should be a buffer of any size.
#### `instance.final(output)`
Finalize the instance.
* `output` should be a buffer of length `crypto_hash_sha256_BYTES`.
The generated hash is stored in `output`.
#### `crypto_hash_sha512(output, input)`
Hash a value to a short hash based on a key.
* `output` should be a buffer of length `crypto_hash_sha512_BYTES`.
* `input` should be a buffer of any size.
The generated short hash is stored in `output`.
#### `var instance = crypto_hash_sha512_instance()`
Create an instance that has stream of input data to sha512.
#### `instance.update(input)`
Update the instance with a new piece of data.
* `input` should be a buffer of any size.
#### `instance.final(output)`
Finalize the instance.
* `output` should be a buffer of length `crypto_hash_sha512_BYTES`.
The generated hash is stored in `output`.
## License
MIT

@@ -32,3 +32,3 @@ var test = require('tape')

const l = Buffer.alloc(sodium.crypto_core_ed25519_NONREDUCEDSCALARBYTES)
l.set([ 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
l.set([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,

@@ -38,3 +38,3 @@ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

@@ -154,4 +154,5 @@ sodium.sodium_add(S, l)

} while (i-- > 0)
sodium.crypto_core_ed25519_scalar_reduce(sc64, sc64)
assert.ok(sodium.sodium_memcmp(sc64.subarray(0, sodium.crypto_core_ed25519_BYTES), sc))
const reduced = sodium.sodium_malloc(sodium.crypto_core_ed25519_SCALARBYTES)
sodium.crypto_core_ed25519_scalar_reduce(reduced, sc64)
assert.ok(sodium.sodium_memcmp(reduced, sc))

@@ -158,0 +159,0 @@ sodium.randombytes_buf(h.subarray(0, sodium.crypto_core_ed25519_UNIFORMBYTES))

@@ -11,3 +11,3 @@ var test = require('tape')

assert.equal(typeof sodium.crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX, 'number')
assert.equal(sodium.crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX, Number.MAX_SAFE_INTEGER) // to make sure, see note in binding.cc
assert.equal(sodium.crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX, 0xffffffef) // to make sure, see note in binding.cc

@@ -149,9 +149,10 @@ assert.end()

m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(c, null, c, null, nonce, firstkey)
var decrypted = sodium.sodium_malloc(c.byteLength - sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES)
m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(decrypted, null, c, null, nonce, firstkey)
assert.equal(m2len, mlen, 'm2len is properly set (adlen=0)')
assert.same(m, c.slice(0, mlen), 'm == c (adlen=0)')
assert.same(m, decrypted, 'm == c (adlen=0)')
sodium.crypto_aead_xchacha20poly1305_ietf_keygen(key2)
assert.throws(_ => m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(c, null, c, null, nonce, key2))
assert.throws(_ => m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(decrypted, null, c, null, nonce, key2))

@@ -158,0 +159,0 @@ assert.end()

@@ -28,4 +28,6 @@ var tape = require('tape')

var buf = Buffer.from('Hello, World!')
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES, 'lo')
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES)
key.fill('lo')
var out = Buffer.alloc(sodium.crypto_generichash_BYTES)

@@ -50,10 +52,12 @@ sodium.crypto_generichash(out, buf, key)

tape('crypto_generichash_instance', function (t) {
var instance = sodium.crypto_generichash_instance()
tape('crypto_generichash_state', function (t) {
var state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, sodium.crypto_generichash_BYTES)
var buf = Buffer.from('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) sodium.crypto_generichash_update(state, buf)
var out = Buffer.alloc(sodium.crypto_generichash_BYTES)
instance.final(out)
sodium.crypto_generichash_final(state, out)

@@ -64,12 +68,15 @@ t.same(out.toString('hex'), 'cbc20f347f5dfe37dc13231cbf7eaa4ec48e585ec055a96839b213f62bd8ce00', 'streaming hash')

tape('crypto_generichash_instance with key', function (t) {
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES, 'lo')
tape('crypto_generichash state with key', function (t) {
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES)
key.fill('lo')
var instance = sodium.crypto_generichash_instance(key)
var state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, key, sodium.crypto_generichash_BYTES)
var buf = Buffer.from('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) sodium.crypto_generichash_update(state, buf)
var out = Buffer.alloc(sodium.crypto_generichash_BYTES)
instance.final(out)
sodium.crypto_generichash_final(state, out)

@@ -80,10 +87,12 @@ t.same(out.toString('hex'), '405f14acbeeb30396b8030f78e6a84bab0acf08cb1376aa200a500f669f675dc', 'streaming keyed hash')

tape('crypto_generichash_instance with hash length', function (t) {
var instance = sodium.crypto_generichash_instance(null, sodium.crypto_generichash_BYTES_MIN)
tape('crypto_generichash state with hash length', function (t) {
var state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, sodium.crypto_generichash_BYTES_MIN)
var buf = Buffer.from('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) sodium.crypto_generichash_update(state, buf)
var out = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN)
instance.final(out)
sodium.crypto_generichash_final(state, out)

@@ -94,12 +103,15 @@ t.same(out.toString('hex'), 'decacdcc3c61948c79d9f8dee5b6aa99', 'streaming short hash')

tape('crypto_generichash_instance with key and hash length', function (t) {
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES, 'lo')
tape('crypto_generichash state with key and hash length', function (t) {
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES)
key.fill('lo')
var instance = sodium.crypto_generichash_instance(key, sodium.crypto_generichash_BYTES_MIN)
var state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, key, sodium.crypto_generichash_BYTES_MIN)
var buf = Buffer.from('Hej, Verden')
for (var i = 0; i < 10; i++) instance.update(buf)
for (var i = 0; i < 10; i++) sodium.crypto_generichash_update(state, buf)
var out = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN)
instance.final(out)
sodium.crypto_generichash_final(state, out)

@@ -123,3 +135,4 @@ t.same(out.toString('hex'), 'fb43f0ab6872cbfd39ec4f8a1bc6fb37', 'streaming short keyed hash')

tape('crypto_generichash_batch with key', function (t) {
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES, 'lo')
var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES)
key.fill('lo')

@@ -126,0 +139,0 @@ var buf = Buffer.from('Hej, Verden')

@@ -20,11 +20,14 @@ var tape = require('tape')

tape('crypto_hash_sha256_instance', function (t) {
tape('crypto_hash_sha256_state', function (t) {
var state = Buffer.alloc(sodium.crypto_hash_sha256_STATEBYTES)
sodium.crypto_hash_sha256_init(state)
var buf = Buffer.from('Hej, Verden!')
for (let i = 0; i < 10; i++) sodium.crypto_hash_sha256_update(state, buf)
var out = Buffer.alloc(sodium.crypto_hash_sha256_BYTES)
var inp = Buffer.from('Hej, Verden!')
sodium.crypto_hash_sha256_final(state, out)
var instance = sodium.crypto_hash_sha256_instance()
instance.update(inp)
instance.final(out)
var result = 'f0704b1e832b05d01223952fb2512181af4f843ce7bb6b443afd5ea028010e6c'
var result = '14207db33c6ac7d39ca5fe0e74432fa7a2ed15caf7f6ab5ef68d24017a899974'
t.same(out.toString('hex'), result, 'hashed the string')

@@ -31,0 +34,0 @@

@@ -20,11 +20,14 @@ var tape = require('tape')

tape('crypto_hash_sha256_instance', function (t) {
tape('crypto_hash_sha512_state', function (t) {
var state = Buffer.alloc(sodium.crypto_hash_sha512_STATEBYTES)
sodium.crypto_hash_sha512_init(state)
var buf = Buffer.from('Hej, Verden!')
for (let i = 0; i < 10; i++) sodium.crypto_hash_sha512_update(state, buf)
var out = Buffer.alloc(sodium.crypto_hash_sha512_BYTES)
var inp = Buffer.from('Hej, Verden!')
sodium.crypto_hash_sha512_final(state, out)
var instance = sodium.crypto_hash_sha512_instance()
instance.update(inp)
instance.final(out)
var result = 'bcf8e6d11dec2da6e93abb99a73c8e9c387886a5f84fbca5e25af85af26ee39161b7e0c9f9cf547f2aef40523f1aab80e26ec3c630db43ce78adc8c058dc5d16'
var result = 'a0a9b965c23be41fa8c344f483da39bedcf88b7f25cdc0bc9ea335fa264dc3db51f08c1d0f5f6f0ffb08a1d8643e2a1cd0ea8f03408ca03711c751d61787a229'
t.same(out.toString('hex'), result, 'hashed the string')

@@ -31,0 +34,0 @@

@@ -19,7 +19,8 @@ var tape = require('tape')

tape('crypto_onetimeauth_instance', function (t) {
tape('crypto_onetimeauth_state', function (t) {
var key = Buffer.alloc(sodium.crypto_onetimeauth_KEYBYTES, 'lo')
const state = Buffer.alloc(sodium.crypto_onetimeauth_STATEBYTES)
t.throws(function () {
sodium.crypto_onetimeauth_instance()
sodium.crypto_onetimeauth_init(state)
}, 'key required')

@@ -29,9 +30,9 @@

var instance = sodium.crypto_onetimeauth_instance(key)
sodium.crypto_onetimeauth_init(state, key)
var value = Buffer.from('Hello, World!')
for (var i = 0; i < 10; i++) instance.update(value)
for (var i = 0; i < 10; i++) sodium.crypto_onetimeauth_update(state, value)
var mac = Buffer.alloc(sodium.crypto_onetimeauth_BYTES)
instance.final(mac)
sodium.crypto_onetimeauth_final(state, mac)

@@ -38,0 +39,0 @@ t.same(mac.toString('hex'), 'ac35df70e6b95051e015de11a6cbf4ab', 'streaming mac')

@@ -67,3 +67,3 @@ var tape = require('tape')

var good = Buffer.alloc(sodium.crypto_pwhash_scryptsalsa208sha256_STRBYTES)
var weakAlg = Buffer.alloc(128)
var weakAlg = Buffer.alloc(sodium.crypto_pwhash_scryptsalsa208sha256_STRBYTES)
weakAlg.set('argon2i$p=2,v=19,m=1024$SGVsbG8=$SGVsbG8gd29ybA==')

@@ -147,3 +147,6 @@

}, 'should throw on large limits')
t.throws(function () {
sodium.crypto_pwhash_scryptsalsa208sha256_str(output, passwd, -1, -1)
}, 'should throw on negative limits')
t.end()
})

@@ -73,4 +73,4 @@ var tape = require('tape')

var good = Buffer.alloc(sodium.crypto_pwhash_STRBYTES)
var weakAlg = Buffer.alloc(128)
weakAlg.set('argon2i$p=2,v=19,m=1024$SGVsbG8=$SGVsbG8gd29ybA==')
var weakAlg = Buffer.alloc(sodium.crypto_pwhash_STRBYTES)
weakAlg.set(Buffer.from('$argon2id$v=19$m=8,t=1,p=1$DF4Tce8BK5di0gKeMBb2Fw$uNE4oyvyA0z68RPUom2NXu/KyGvpFppyUoN6pwFBtRU'))

@@ -154,3 +154,6 @@ sodium.crypto_pwhash_str(weakMem, passwd, sodium.crypto_pwhash_OPSLIMIT_MODERATE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE)

}, 'should throw on large limits')
t.throws(function () {
sodium.crypto_pwhash_str(output, passwd, -1, -1)
}, 'should throw on negative limits')
t.end()
})

@@ -14,3 +14,2 @@ var test = require('tape')

assert.ok(Buffer.isBuffer(sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL), 'crypto_secretstream_xchacha20poly1305_TAG_FINAL is Buffer')
assert.end()

@@ -20,3 +19,3 @@ })

test('crypto_secretstream', function (assert) {
var state = sodium.crypto_secretstream_xchacha20poly1305_state_new()
var state = Buffer.alloc(sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES)

@@ -23,0 +22,0 @@ var header = Buffer.alloc(sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES)

@@ -36,3 +36,3 @@ var tape = require('tape')

tape('crypto_stream_xor_instance', function (t) {
tape('crypto_stream_xor state', function (t) {
var message = Buffer.from('Hello, world!')

@@ -44,8 +44,10 @@ var nonce = random(sodium.crypto_stream_NONCEBYTES)

var inst = sodium.crypto_stream_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)
for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonce, key)

@@ -56,3 +58,3 @@ t.same(out, message, 'decrypted')

tape('crypto_stream_xor_instance with empty buffers', function (t) {
tape('crypto_stream_xor state with empty buffers', function (t) {
var message = Buffer.from('Hello, world!')

@@ -64,11 +66,13 @@ var nonce = random(sodium.crypto_stream_NONCEBYTES)

var inst = sodium.crypto_stream_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)
inst.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
inst.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
sodium.crypto_stream_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonce, key)

@@ -79,8 +83,11 @@ t.same(out, message, 'decrypted')

tape('crypto_stream_xor_instance long stream', function (t) {
tape('crypto_stream_xor state long stream', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var encrypt = sodium.crypto_stream_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
var plain = []

@@ -95,7 +102,7 @@ var encrypted = []

var enc = Buffer.alloc(61)
encrypt.update(enc, next)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(61)
decrypt.update(dec, enc)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)

@@ -112,8 +119,11 @@ }

tape('crypto_stream_xor_instance long stream (random chunks)', function (t) {
tape('crypto_stream_xor state long stream (random chunks)', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var encrypt = sodium.crypto_stream_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
var plain = []

@@ -129,7 +139,7 @@ var encrypted = []

var enc = Buffer.alloc(len)
encrypt.update(enc, next)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(len)
decrypt.update(dec, enc)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)

@@ -146,8 +156,11 @@ }

tape('crypto_stream_xor_instance long stream (random chunks) with empty buffers', function (t) {
tape('crypto_stream_xor state long stream (random chunks) with empty buffers', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var encrypt = sodium.crypto_stream_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(encState, nonce, key)
sodium.crypto_stream_xor_init(decState, nonce, key)
var plain = []

@@ -162,12 +175,12 @@ var encrypted = []

encrypt.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_xor_update(encState, Buffer.alloc(0), Buffer.alloc(0))
var enc = Buffer.alloc(len)
encrypt.update(enc, next)
sodium.crypto_stream_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(len)
decrypt.update(dec, enc)
sodium.crypto_stream_xor_update(decState, dec, enc)
decrypted.push(dec)
decrypt.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_xor_update(decState, Buffer.alloc(0), Buffer.alloc(0))
}

@@ -183,3 +196,3 @@

tape('crypto_stream_xor_instance after GC', function (t) {
tape('crypto_stream_xor state after GC', function (t) {
var message = Buffer.from('Hello, world!')

@@ -191,3 +204,4 @@ var nonce = random(sodium.crypto_stream_NONCEBYTES)

var inst = sodium.crypto_stream_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_xor_STATEBYTES)
sodium.crypto_stream_xor_init(state, nonce, key)

@@ -202,5 +216,6 @@ var nonceCopy = Buffer.from(nonce.toString('hex'), 'hex')

for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
sodium.crypto_stream_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_xor_final(state)
sodium.crypto_stream_xor(out, out, nonceCopy, keyCopy)

@@ -211,15 +226,17 @@ t.same(out, message, 'decrypted')

tape('crypto_stream_chacha20_xor_instance', function (t) {
tape('crypto_stream_chacha20_xor state', function (t) {
var message = Buffer.from('Hello, world!')
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var out = Buffer.alloc(message.length)
var inst = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(state, nonce, key)
for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
sodium.crypto_stream_chacha20_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_chacha20_xor_final(state)
sodium.crypto_stream_chacha20_xor(out, out, nonce, key)

@@ -230,18 +247,20 @@ t.same(out, message, 'decrypted')

tape('crypto_stream_chacha20_xor_instance with empty buffers', function (t) {
tape('crypto_stream_chacha20_xor state with empty buffers', function (t) {
var message = Buffer.from('Hello, world!')
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var out = Buffer.alloc(message.length)
var inst = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(state, nonce, key)
inst.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_chacha20_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
inst.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_chacha20_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
sodium.crypto_stream_chacha20_xor_update(state, Buffer.alloc(0), Buffer.alloc(0))
}
sodium.crypto_stream_chacha20_xor_final(state)
sodium.crypto_stream_chacha20_xor(out, out, nonce, key)

@@ -252,8 +271,11 @@ t.same(out, message, 'decrypted')

tape('crypto_stream_chacha20_xor_instance long stream', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
tape('crypto_stream_chacha20_xor state long stream', function (t) {
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var encrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(encState, nonce, key)
sodium.crypto_stream_chacha20_xor_init(decState, nonce, key)
var plain = []

@@ -268,7 +290,7 @@ var encrypted = []

var enc = Buffer.alloc(61)
encrypt.update(enc, next)
sodium.crypto_stream_chacha20_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(61)
decrypt.update(dec, enc)
sodium.crypto_stream_chacha20_xor_update(decState, dec, enc)
decrypted.push(dec)

@@ -285,8 +307,11 @@ }

tape('crypto_stream_chacha20_xor_instance long stream (random chunks)', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
tape('crypto_stream_chacha20_xor state long stream (random chunks)', function (t) {
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var encrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(encState, nonce, key)
sodium.crypto_stream_chacha20_xor_init(decState, nonce, key)
var plain = []

@@ -302,7 +327,7 @@ var encrypted = []

var enc = Buffer.alloc(len)
encrypt.update(enc, next)
sodium.crypto_stream_chacha20_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(len)
decrypt.update(dec, enc)
sodium.crypto_stream_chacha20_xor_update(decState, dec, enc)
decrypted.push(dec)

@@ -319,8 +344,11 @@ }

tape('crypto_stream_chacha20_xor_instance long stream (random chunks) with empty buffers', function (t) {
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
tape('crypto_stream_chacha20_xor state long stream (random chunks) with empty buffers', function (t) {
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var encrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var decrypt = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var encState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
var decState = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(encState, nonce, key)
sodium.crypto_stream_chacha20_xor_init(decState, nonce, key)
var plain = []

@@ -335,12 +363,12 @@ var encrypted = []

encrypt.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_chacha20_xor_update(encState, Buffer.alloc(0), Buffer.alloc(0))
var enc = Buffer.alloc(len)
encrypt.update(enc, next)
sodium.crypto_stream_chacha20_xor_update(encState, enc, next)
encrypted.push(enc)
var dec = Buffer.alloc(len)
decrypt.update(dec, enc)
sodium.crypto_stream_chacha20_xor_update(decState, dec, enc)
decrypted.push(dec)
decrypt.update(Buffer.alloc(0), Buffer.alloc(0))
sodium.crypto_stream_chacha20_xor_update(decState, Buffer.alloc(0), Buffer.alloc(0))
}

@@ -356,10 +384,11 @@

tape('crypto_stream_chacha20_xor_instance after GC', function (t) {
tape('crypto_stream_chacha20_xor state after GC', function (t) {
var message = Buffer.from('Hello, world!')
var nonce = random(sodium.crypto_stream_NONCEBYTES)
var key = random(sodium.crypto_stream_KEYBYTES)
var nonce = random(sodium.crypto_stream_chacha20_NONCEBYTES)
var key = random(sodium.crypto_stream_chacha20_KEYBYTES)
var out = Buffer.alloc(message.length)
var inst = sodium.crypto_stream_chacha20_xor_instance(nonce, key)
var state = Buffer.alloc(sodium.crypto_stream_chacha20_xor_STATEBYTES)
sodium.crypto_stream_chacha20_xor_init(state, nonce, key)

@@ -374,5 +403,6 @@ var nonceCopy = Buffer.from(nonce.toString('hex'), 'hex')

for (var i = 0; i < message.length; i++) {
inst.update(out.slice(i), message.slice(i, i + 1))
sodium.crypto_stream_chacha20_xor_update(state, out.slice(i, i + 1), message.slice(i, i + 1))
}
sodium.crypto_stream_chacha20_xor_final(state)
sodium.crypto_stream_chacha20_xor(out, out, nonceCopy, keyCopy)

@@ -390,7 +420,4 @@ t.same(out, message, 'decrypted')

function forceGC () {
var list = []
for (var i = 0; i < 1e6; i++) {
list.push({})
}
list = null
require('v8').setFlagsFromString('--expose-gc')
require('vm').runInNewContext('gc')()
}

@@ -5,2 +5,49 @@ var tape = require('tape')

tape('sodium_mprotect_noaccess', function (t) {
t.plan(1)
var p = fork(require.resolve('./fixtures/mprotect_noaccess'))
p.on('message', function () {
t.fail()
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode !== null || p.exitCode > 0)
})
})
tape('sodium_mprotect_readonly', function (t) {
t.plan(2)
var p = fork(require.resolve('./fixtures/mprotect_readonly'))
p.on('message', function (msg) {
t.ok(msg === 'read')
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode !== null || p.exitCode > 0)
})
})
tape('sodium_mprotect_readwrite', function (t) {
t.plan(4)
var p = fork(require.resolve('./fixtures/mprotect_readwrite'))
p.on('message', function (msg) {
switch (msg) {
case 'read': t.pass()
break
case 'write': t.pass()
break
case 'did_write': t.pass()
break
case 'did_not_write': t.fail()
break
default: t.fail()
break
}
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode === null || p.exitCode === 0)
})
})
tape('sodium_memzero', function (t) {

@@ -80,48 +127,1 @@ var buf = Buffer.alloc(10, 0xab)

})
tape('sodium_mprotect_noaccess', function (t) {
t.plan(1)
var p = fork(require.resolve('./fixtures/mprotect_noaccess'))
p.on('message', function () {
t.fail()
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode !== null || p.exitCode > 0)
})
})
tape('sodium_mprotect_readonly', function (t) {
t.plan(2)
var p = fork(require.resolve('./fixtures/mprotect_readonly'))
p.on('message', function (msg) {
t.ok(msg === 'read')
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode !== null || p.exitCode > 0)
})
})
tape('sodium_mprotect_readwrite', function (t) {
t.plan(4)
var p = fork(require.resolve('./fixtures/mprotect_readwrite'))
p.on('message', function (msg) {
switch (msg) {
case 'read': t.pass()
break
case 'write': t.pass()
break
case 'did_write': t.pass()
break
case 'did_not_write': t.fail()
break
default: t.fail()
break
}
})
p.on('exit', function (code, signal) {
t.ok(p.signalCode === null || p.exitCode === 0)
})
})

@@ -1,2 +0,1 @@

require('sodium-vectors')(require('../'))

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc