Adds convenience methods to the native Node.js crypto module. It is a drop in replacement, and extends the original module functionality.
Why?
The native crypto
module can be a pain to work with, and requires a lot of boilerplate to do things such as randomizing and encryption. This abstracts all of that.
Getting Started
$ npm install crypto-extra --save
To use in your project, simply require into your project as you would the crypto
module.
const crypto = require("crypto-extra")
crypto.randomString()
crypto.hash("hello")
API
.encrypt (value, secretKey)
Encrypts a value with a secret key using AES-256-CTR.
-
value - The value you want to encrypt. Everything (except objects) is converted to a string before encryption for consistency. Objects are stringified using JSON.stringify
.
Type: any
-
secretKey - The key used in the encryption. If not supplied, the lib will fallback to the environment variable ENCRYPTION_KEY
.
Type: string
Default: process.env.ENCRYPTION_KEY
.decrypt (value, secretKey)
Decrypts a value using AES-256-CTR.
-
value - The encrypted value you want to decrypt. Will automatically parse objects that were encrypted.
Type: string
-
secretKey - The key used in the encryption. If not supplied, the lib will fallback to the environment variable ENCRYPTION_KEY
.
Type: string
Default: process.env.ENCRYPTION_KEY
.hash (value, options)
Hashes a string with the provided algorithm.
.randomKey (length)
Generates a random 256-bit key that can be used as an encryption key.
.randomString (length, charset)
Returns a random string of a defined length.
.randomNumber (options)
Returns a random string within a defined range.
-
options
-
min - Minimum number of range. Must be a positive integer.
Type: integer
Default: 0
-
max - Maximum number of range. This cannot be higher than 9007199254740991
due to Javascript integer limits (http://mzl.la/1A1nVyU). If you need a number higher than this, consider using randomString with the charset 0123456789
instead.
Type: integer
Default: 9007199254740991
License
MIT © Jason Maurer