High-level utilities that combine under simple interfaces complexity of the cryptographic layer used in Detox project.
Essentially provides wrapper functions and objects for:
- Ed25519 and X25519 keys creation
- AEZ block cipher
- Noise protocol (Noise_NK_25519_ChaChaPoly_BLAKE2b, Noise_N_25519_ChaChaPoly_BLAKE2b)
npm install @detox/crypto
NOTE: In modern versions of Node.js (4.x and higher) Buffer
inherits Uint8Array
, so you can pass Buffer
directly whenever Uint8Array
is expected.
Node.js:
var detox_crypto = require('@detox/crypto')
detox_crypto.ready(function () {
// Do stuff
});
Browser:
requirejs(['@detox/crypto'], function (detox_crypto) {
detox_crypto.ready(function () {
// Do stuff
});
})
callback
- Callback function that is called when library is ready for use
Creates keypairs from specified seed (if not specified, random seed will be generated).
Returns an object with properties:
seed
(same as argument or random generate otherwise)ed25519
- Ed25519 keypairpublic
- Public keyprivate
- Private key (already hashed, as used in orlp/ed25519)
x25519
- X25519 keypairpublic
- Public keyprivate
- Private key
Converts Ed25519 public key into X25519/Curve25519 public key. Returns null
if conversion fails.
detox_crypto.sign(data : Uint8Array, ed25519_public_key : Uint8Array, ed25519_private_key : Uint8Array) : Uint8Array
Signs data and returns 64 bytes signature.
detox_crypto.verify(signature : Uint8Array, data : Uint8Array, ed25519_public_key : Uint8Array) : boolean
Verifies that signature corresponds to specified data and public key.
Constructor for Rewrapper object. Can be initialized with key (48 bytes, typically done on responder side) or key will be generated automatically (typically done on initiator side).
Uses AEZ block cipher.
Key that was specified during initialization or that was generated automatically.
Wraps plaintext into ciphertext.
Unwraps plaintext from ciphertext, inverse to wrap()
.
Constructor for Encryptor object. If initialized for initiator then key
will be public key (X25519) of the responder. If initialized for responder then key
will be private key (X25519) of the responder.
Uses Noise protocol (Noise_NK_25519_ChaChaPoly_BLAKE2b).
Constructor and all methods can throw Error
if something goes wrong, be ready to catch exceptions.
Quick check if handshake was finished and Encryptor is ready for encryption/decryption.
Get handshake message that should be send to another side.
Put handshake message that was received from another side.
Get rewrapper keys derived from handshake. 2 elements are Uint8Array
s of 48 bytes, first one most be used for sending data (wrapping) and second for receiving (unwrapping).
Encrypts plaintext into ciphertext for another side.
Decrypts plaintext from ciphertext from another side.
Destroys stateful data structures and makes Encryptor unusable.
One-way encryption for specified X25519 public key.
Uses Noise protocol (Noise_N_25519_ChaChaPoly_BLAKE2b).
Returns combined handshake message and ciphertext.
One-way decryption of the message for specified X25519 private key.
Uses Noise protocol (Noise_N_25519_ChaChaPoly_BLAKE2b).
Takes combined handshake message and ciphertext as input message and returns plaintext.
Returns Blake2b-256 hash of data
.
Take a look at src/index.ls
for JsDoc sections with arguments and return types as well as methods description, look at tests/index.ls
for usage examples.
Feel free to create issues and send pull requests (for big changes create an issue first and link it from the PR), they are highly appreciated!
When reading LiveScript code make sure to configure 1 tab to be 4 spaces (GitHub uses 8 by default), otherwise code might be hard to read.
Free Public License 1.0.0 / Zero Clause BSD License