- Source:
Cryptographic utilities
Members
(static, constant) chars :object
- Source:
different charsets available
Type:
- object
(static, constant) rot13 :Str.rot13
- Source:
Type:
(static, constant) rot47 :Str.rot47
- Source:
Type:
Methods
(static) base64Decode(string, toEncodingopt)
- Source:
Base64 Decode
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | |||
toEncoding |
string |
<optional> |
'binary'
|
(static) base64Encode(string, fromEncodingopt)
- Source:
Base64 Encode
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | |||
fromEncoding |
string |
<optional> |
'binary'
|
(static) base64UrlDecode(string, toEncodingopt)
- Source:
URL Safe Base64 Decode
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | |||
toEncoding |
string |
<optional> |
'binary'
|
(static) base64UrlEncode(string, fromEncodingopt)
- Source:
URL Safe Base64 Encode
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | |||
fromEncoding |
string |
<optional> |
'binary'
|
(static) baseConvert(str, baseIn, baseOut)
- Source:
convert arbitary long integer from one base to another
Taken from decimal.js
Parameters:
Name | Type | Default | Description |
---|---|---|---|
str |
string | number | number in base 'baseIn' |
|
baseIn |
number |
10
|
input base |
baseOut |
number |
62
|
output base |
(static) baseDecode(string, opts) → {string}
- Source:
Decode a string encoded using a given encoding.
Parameters:
Name | Type | Description |
---|---|---|
string |
string | Buffer | item to be decoded |
opts |
encodingConversion | string | object or string specifying the encoding(s) |
Returns:
decoded item
NOTE: If opts is a string, it is considered as the fromEncoding
(i.e that was used to encode the string).
- Type
- string
(static) baseDecodeToBuffer(string, fromEncoding) → {Buffer}
- Source:
Decode a string encoded using a given encoding to a buffer.
Parameters:
Name | Type | Description |
---|---|---|
string |
string | Buffer | item to be decoded |
fromEncoding |
string | encoding used to encode the string |
Returns:
decoded item
- Type
- Buffer
(static) baseEncode(string, opts) → {string|Buffer}
- Source:
Encode the string/buffer using a given encoding.
Supported Encodings:
'hex', 'binary' ('latin1'), 'ascii', 'base64', 'base64url',
'utf8', 'buffer', 'utf16le' ('ucs2')
Parameters:
Name | Type | Description |
---|---|---|
string |
string | Buffer | item to be encoded |
opts |
encodingConversion | string | object or string specifying the encoding(s) |
Returns:
encoded item
NOTE: If opts is a string, it is considered as the toEncoding.
The fromEncoding defaults to binary, while the toEncoding defaults to base64url.
- Type
- string | Buffer
(static) decrypt(string, key, optionsopt) → {string}
- Source:
Decrypt the given string with the given key encrypted using encrypt
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be decrypted |
||
key |
string | key to be used |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} (default: 'base64url') |
Returns:
decrypted string
- Type
- string
(static) decryptStatic(string, key, optionsopt) → {string}
- Source:
Decrypt the given string with the given key encrypted using encryptStatic
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be decrypted |
||
key |
string | key to be used |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} (default: 'base64url') |
Returns:
decrypted string
- Type
- string
(static) encrypt(string, key, optionsopt) → {string}
- Source:
Encrypt the given string with the given key using AES 256
Calling encrypt on the same string multiple times will return different encrypted strings
Optionally specify encoding in which you want to get the output
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be encrypted |
||
key |
string | key to be used |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} (default: 'base64url') |
Returns:
encrypted string
- Type
- string
(static) encryptedTimestampedId(options) → {string}
- Source:
Generate a random encrypted string that contains a timestamp.
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
number | object | length of the id or object of {length: int} Properties
|
Returns:
id
- Type
- string
(static) encryptStatic(string, key, optionsopt) → {string}
- Source:
Encrypt the given string with the given key using AES 256
Calling EncryptStatic on the same string multiple times will return same encrypted strings
this encryption is weaker than Encrypt but has the benefit of returing same encrypted string
for same string and key.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be encrypted |
||
key |
string | key to be used |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} (default: 'base64url') |
Returns:
encrypted string
- Type
- string
(static) hash(algo, string, optsopt) → {string}
- Source:
Compute hash of a string using given algorithm
encoding can be 'hex', 'binary' ('latin1'), 'ascii', 'base64', 'base64url', 'utf8', 'buffer'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
algo |
string | algo to be used for hashing |
||
string |
string | string to be hashed |
||
opts |
encodingOpts |
<optional> |
{}
|
Returns:
encoded hash value
- Type
- string
(static) hashPassword(password, optsopt) → {string}
- Source:
Hash a given password using cryptographically strong hash function
Parameters:
Name | Type | Attributes | Default | Description | ||||||
---|---|---|---|---|---|---|---|---|---|---|
password |
string | |||||||||
opts |
object |
<optional> |
{}
|
Properties
|
Returns:
50 character long hash
- Type
- string
(static) hmac(algo, string, optionsopt) → {string}
- Source:
Create cryptographic HMAC digests using given algo
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
algo |
string | algo to be used for hashing |
||
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded HMAC digest
- Type
- string
(static) md5(string, optionsopt) → {string}
- Source:
Compute hash of a string using md5
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded hash value
- Type
- string
(static) nanoSecondsAlpha(base36) → {string}
- Source:
Get nanoseconds in base62 or base36 format.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
base36 |
boolean |
false
|
use base36 format or not |
Returns:
nanoseconds
- Type
- string
(static) packNumbers(numbers) → {string}
- Source:
Pack many numbers into a single string
Parameters:
Name | Type | Description |
---|---|---|
numbers |
Array | array of numbers to be packed |
Returns:
packed string
- Type
- string
(static) random() → {number}
- Source:
Return a random number between 0 and 1
Returns:
A random number (float) between 0 and 1
- Type
- number
(static) randomInt(num, maxopt) → {number}
- Source:
Return a random integer between min and max (both inclusive)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
num |
number | If max is not passed, num is max and min is 0 |
|
max |
number |
<optional> |
max value of int, num will be min |
Returns:
A random integer between min and max
- Type
- number
(static) randomString(options) → {string}
- Source:
Generate a random string based on the options passed.
It can be treated as a Random UUID.
You can give length and charset in options.
If options is an integer it will treated as length.
By default, length is 20 and charset is ALPHA_NUMERIC
Parameters:
Name | Type | Description |
---|---|---|
options |
number | randomOpts | length of the id or options object |
Returns:
id
- Type
- string
(static) randomUUID()
- Source:
Get random ID in v4 UUID format.
(static) seededRandom(seed) → {randomFunctions}
- Source:
Parameters:
Name | Type | Description |
---|---|---|
seed |
number | integer |
Returns:
Object of all random functions
- Type
- randomFunctions
(static) sequentialID(options) → {string}
- Source:
Generate a sequential id based on current time in millisecond and some randomness.
It can be treated as a Sequential UUID. Ideal for use as a DB primary key.
NOTE: For best results use atleast 15 characters in base62 and 18 characters in base36 encoding
Parameters:
Name | Type | Description |
---|---|---|
options |
number | object | length of the id or object of {length: int, base36: bool} |
Returns:
id
- Type
- string
(static) sequentialUUID()
- Source:
Get sequential ID in v4 UUID format.
(static) sha1(string, optionsopt) → {string}
- Source:
Compute hash of a string using sha1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded hash value
- Type
- string
(static) sha1Hmac(string, optionsopt) → {string}
- Source:
Create cryptographic HMAC digests using sha1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded HMAC digest
- Type
- string
(static) sha256(string, optionsopt) → {string}
- Source:
Compute hash of a string using sha256
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded hash value
- Type
- string
(static) sha256Hmac(string, optionsopt) → {string}
- Source:
Create cryptographic HMAC digests using sha256
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded HMAC digest
- Type
- string
(static) sha384(string, optionsopt) → {string}
- Source:
Compute hash of a string using sha384
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded hash value
- Type
- string
(static) sha512(string, optionsopt) → {string}
- Source:
Compute hash of a string using sha512
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
string |
string | string to be hashed |
||
options |
encodingOpts |
<optional> |
{}
|
object of {encoding} |
Returns:
encoded hash value
- Type
- string
(static) shuffle(itemToShuffle, optionsopt) → {Array.<T>|string}
- Source:
Shuffle an array or a string.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
itemToShuffle |
Array.<T> | string | item which you want to shuffle |
|||||||||||
options |
object |
<optional> |
{}
|
object of {seed: number} Properties
|
Returns:
shuffled item
- Type
- Array.<T> | string
(static) sign(message, privateKey, opts) → {string}
- Source:
Sign a message using a private key.
NOTE: Generate a key pair using:
openssl ecparam -genkey -name secp256k1 | openssl ec -aes128 -out private.pem
openssl ec -in private.pem -pubout -out public.pem
Parameters:
Name | Type | Description |
---|---|---|
message |
string | the message to be signed |
privateKey |
string | object | |
opts |
encodingOpts | opts can have {encoding (default 'hex') |
Returns:
signature
NOTE: If privateKey is a string, it is treated as a raw key with no passphrase.
If privateKey is an object, it must contain the following property:
key:
- Type
- string
(static) signAndEncrypt(message, privateKey, publicKey) → {string}
- Source:
Convert a message into an encrypted token by:
signing it with privateKey + encrypting it with publicKey
you only need a publicKey to verify and decrypt this token
Parameters:
Name | Type | Description |
---|---|---|
message |
any | will be JSON.stringified |
privateKey |
string | object | |
publicKey |
string |
Returns:
- Type
- string
(static) unpackNumbers(str) → {Array}
- Source:
Unpack a string packed with packNumbers
Parameters:
Name | Type | Description |
---|---|---|
str |
string | string to be unpacked |
Returns:
array of unpacked numbers
- Type
- Array
(static) verify(message, signature, publicKey, optsopt) → {boolean}
- Source:
Verify a message using a public key
opts can have {encoding (default 'hex')}
NOTE: Generate a key pair using:
openssl ecparam -genkey -name secp256k1 | openssl ec -aes128 -out private.pem
openssl ec -in private.pem -pubout -out public.pem
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
message |
string | message to be verified |
||
signature |
string | |||
publicKey |
string | object | |||
opts |
encodingOpts |
<optional> |
{}
|
opts can have {encoding (default 'hex')} |
Returns:
true or false, depending on the validity of
the signature for the data and public key
- Type
- boolean
(static) verifyAndDecrypt(token, publicKey)
- Source:
Convert an encrypted token (generated by signAndEncrypt) into a message
Parameters:
Name | Type | Description |
---|---|---|
token |
string | generated by signAndEncrypt |
publicKey |
string |
(static) verifyPassword(password, hashed) → {boolean}
- Source:
Verify that given password and hashed password are same or not
Parameters:
Name | Type | Description |
---|---|---|
password |
string | |
hashed |
string |
Returns:
- Type
- boolean