$ npm install kruptein
crypto (krip-toh); from kruptein to hide or conceal.
Encrypt a plaintext message using the default options.
const kruptein = require("kruptein")();
const secret = "S3cre+_Squ1rr3l";
const plaintext = "Some kind of wonderfully private message";
kruptein.set(secret, plaintext, (error, ciphertext) => {
if (error) {
throw new Error(error);
}
console.log(ciphertext);
});
Decrypt a ciphertext returned by kruptein.set() using the same secret.
const kruptein = require("kruptein")();
const secret = "S3cre+_Squ1rr3l";
const ciphertext = "<ciphertext returned by kruptein.set()>";
kruptein.get(secret, ciphertext, (error, plaintext) => {
if (error) {
throw new Error(error);
}
console.log(plaintext);
});
To install npm install kruptein
.set(secret, plaintext, [aad], callback).get(secret, ciphertext, [{at: auth_tag, aad: aad}], callback)Industry standards are used for the algorithm, hashing algorithm, key & IV sizes.
The default key derivation function is pbkdf2, however use of the scrypt or argon2 can be used as well.
algorithm: (Optional) Cipher algorithm from crypto.getCiphers(); AES or ARIA at 192 or 256 bits in gcm, ccm, ocb or cbc mode, or chacha20-poly1305. Default: aes-256-gcm.hashing: (Optional) Hash algorithm from crypto.getHashes(); SHA-2 & SHA-3 families only. Default: sha384.key_size: (Optional) Key size bytes (should match block size of algorithm). Default: 32iv_size: (Optional) IV size bytes. Default: 16.at_size: (Optional) Authentication tag size. Applicable to gcm & ocb cipher modes. Default: 128.use_scrypt: (Optional) Use .scrypt() to derive a key. Requires node > v10. Default/Fallback: .pbkdf2().use_argon2: (Optional) Use .argon2id() to derive a key. Requires node > v24. Default/Fallback: .pbkdf2().encodeas: (Optional) Output encoding. Currently supports binary, latin1, hex, base64 & base64url. Default: base64.use_asn1: (Optional) Disable the default ASN.1 encoding. Default: trueAn example of creating a new ciphertext object.
const opts = {
algorithm: 'aes-256-gcm',
hashing: 'sha384',
key_size: 64,
iv_size: 16,
at_size: 128,
use_argon2: true,
encodeas: 'base64',
use_asn1: true
}
const kruptein = require("kruptein")(opts || {});
When selecting an algorithm from crypto.getCiphers() the iv and key_size values are calculated auto-magically to make implementation easy.
You can always define your own if the defaults per algorithm and mode aren't what you would like; see the options section above.
The secret must meet complexity requirements
The .set() method output depends on three factors; the encodeas, algorithm and use_asn1.
For any algorithm that supports authentication (AEAD), the object structure includes the Authentication Tag and the Additional Authentication Data attribute and value.
When the use_asn1 option is enabled (default is true), the result is an ASN.1 value using the encodeas value. While this is a more complex encoding option, it helps standardize & minimize the size of the resulting ciphertext output.
The included test harness, invoked with npm test, makes every attempt to trap and handle errors. Some of which come from side channel or possible malability of the resultant ciphertext.
This can be seen within the test/index.js CI test harness under the HMAC, AT & AAD validation test cases.
npm test covers the default cipher, hash & encoding. npm run test:full (or KRUPTEIN_FULL_MATRIX=1) feeds every cipher & hash reported by crypto.getCiphers() & crypto.getHashes(), and every Buffer encoding, through the library; unsupported combinations must be rejected with an error and supported ones must round-trip.
This module conforms to industry recommendations regarding algorithm type, mode, key size, iv size & implementation, digests, key derivation & management etc. References used provided here:
RFC:
NIST:
FIPS:
Contributions are welcome & appreciated!
Refer to the contributing document to help facilitate pull requests.
This software is licensed under the MIT License.
Copyright Jason Gerfen, 2019.
© 2010 - cnpmjs.org x YWFE | Home | YWFE