- lc4
- .encrypt(settings) ⇒
String
- .decrypt(settings) ⇒
String
- .generateKey([mode]) ⇒
String
- .generateNonce([mode], [length]) ⇒
String
- .escapeToLC4(string) ⇒
String
- .escapeToLS47(string) ⇒
String
- .escapeString(string, [mode]) ⇒
String
- .encrypt(settings) ⇒
Encrypt a message with LC4 or LS47
Kind: static method of lc4
Returns: String
- the encrypted (and signed) message
Throws:
TypeError
Will throw a type error if settings are invalid or missing
Param | Type | Default | Description |
---|---|---|---|
settings | Object |
encryption settings | |
[settings.mode] | String |
"lc4" |
encryption algorithm. Can be either "lc4" or "ls47" |
settings.message | String | Array |
message or array of messages to encrypt. Invalid LC4 or LS47 strings are escaped with the escapeString method |
|
settings.key | String |
valid LC4 or LS47 key or password; If a password is passed, the key/state will be expanded from the password | |
[settings.nonce] | String |
|
valid LC4 or LS47 nonce (> 5 characters) |
[settings.headerData] | String |
|
header data |
[settings.signature] | String |
|
signature for signing the message (> 9 characters) |
[settings.verbose] | Boolean |
false |
boolean indicating whether verbose mode should be used (will print intermediate steps to console) |
Example (Encrypt a message with a random key)
const { encrypt, generateKey } = require("lc4");
encrypt({
message: "hello_world",
key: generateKey(),
nonce: "lorem_ipsum"
});
Example (Encrypt a multiline message with a random key and LS47)
const { encrypt, generateKey } = require("lc4");
encrypt({
message: [ "hello", "ls47" ],
key: generateKey("ls47"),
nonce: "lorem_ipsum",
mode: "ls47"
})
Example (Encrypt and sign a message)
const { encrypt, generateKey, generateNonce } = require("lc4");
encrypt({
message: "Lorem Ipsum", // will be escaped to lorem_ipsum
key: "my_super_secret_password",
nonce: generateNonce(),
signature: "#secret_signature",
verbose: true
});
Decrypt a message with LC4 or LS47
Kind: static method of lc4
Returns: String
- the encrypted (and signed) message
Throws:
Error
Will throw error "Invalid Signature" if message doesn't end with specified signatureTypeError
Will throw a type error if settings are invalid or missing
Param | Type | Default | Description |
---|---|---|---|
settings | Object |
decryption settings | |
[settings.mode] | String |
"lc4" |
decryption algorithm. Can be either "lc4" or "ls47" |
settings.message | String | Array |
message or array of multiline message to decrypt; When decrypting a multiline message with a signature the signature must be the last element of the array | |
settings.key | String |
valid LC4 or LS47 key or password; If a password is passed, the key/state will be expanded from the password | |
[settings.nonce] | String |
|
valid LC4 or LS47 nonce (> 5 characters) |
[settings.headerData] | String |
|
header data |
[settings.signature] | String |
|
signature of signed message (> 9 characters) |
[settings.verbose] | Boolean |
false |
boolean indicating whether verbose mode should be used (will print intermediate steps to console) |
Example (Decrypt a message with a given key)
const { decrypt } = require("lc4");
decrypt({
message: "v74hxj5pxmo",
key: "igqehmd48pvxrl7k36y95j2sfnbo#wc_ztau",
nonce: "lorem_ipsum"
});
//=> "hello_world"
Example (Decrypt a message with a given key and LS47)
const { decrypt } = require("lc4");
decrypt({
message: "8.bc-'suz+6l",
key: "4un)pj0c6(h!ms+_-5q*vkt,zi?9xoglw:18e'.dy/rba73f2",
mode: "ls47"
})
//=> "hello_world!"
Example (Decrypt a multiline, signed message)
const { decrypt } = require("lc4");
decrypt({
message: [ '6q4ij', 'p9597', 'bc8p325u2jc_d9xfw' ],
key: "notds7u_i3exc2wlbyzpa4g85#v9fqjkrmh6",
nonce: "r#39_4kgpz",
signature: "#secret_signature",
verbose: true
});
//=> ["lorem", "ipsum", "#secret_signature"]
Generate a valid random LC4 or LS47 key
Kind: static method of lc4
Returns: String
- a valid LC4 or LS47 key
Param | Type | Default | Description |
---|---|---|---|
[mode] | String |
"lc4" |
encryption/decryption mode. Can be either "lc4" or "ls47" |
Example (Generate a random key)
let { generateKey } = require("lc4");
generateKey();
Example (Generate a random LS47 key)
let { generateKey } = require("lc4");
generateKey("ls47");
Example (Encrypt a message with a random key)
const { encrypt, generateKey } = require("lc4");
encrypt({
message: "hello_world",
key: generateKey(),
});
Generate a valid random LC4 or LS47 nonce
Kind: static method of lc4
Returns: String
- a valid LC4 or LS47 nonce
Throws:
Error
Will throw an error if length is smaller than 6
Param | Type | Default | Description |
---|---|---|---|
[mode] | String |
"lc4" |
encryption/decryption mode. Can be either "lc4" or "ls47" |
[length] | Number |
10 |
length of nonce (at least 6) |
Example (Generate a random nonce)
let { generateNonce } = require("lc4");
generateNonce();
Example (Encrypt a message with LS47 and a random nonce)
const { encrypt, generateKey, generateNonce } = require("lc4");
encrypt({
message: "Lorem Ipsum!",
key: generateKey("ls47"),
nonce: generateNonce("ls47")
})
Escape string to valid LC4 string
Kind: static method of lc4
Returns: String
- valid LC4 string
Param | Type | Description |
---|---|---|
string | String |
(invalid) LC4 string |
Example
let { escapeToLC4 } = require("lc4");
escapeToLC4("Hello World! This is the 10th test!");
//=> "hello_world_this_is_the__#th_test"
Escape string to valid LS47 string
Kind: static method of lc4
Returns: String
- valid LS47 string
Param | Type | Description |
---|---|---|
string | String |
(invalid) LS47 string |
Example
let { escapeToLS47 } = require("lc4");
escapeToLS47("Hello World! This is the 10th test!");
//=> "hello_world!_this_is_the_10th_test!"
Escapes a string to a valid LC4 or LS47 string
Kind: static method of lc4
Returns: String
- valid LC4 or LS47 string
Param | Type | Default | Description |
---|---|---|---|
string | String |
(invalid) LC4 or LS47 string | |
[mode] | String |
"lc4" |
encryption/decryption mode. Can be either "lc4" or "ls47" |
Example
let { escapeString } = require("lc4");
escapeString("Hello World! This is the 10th test!", "ls47");
//=> "hello_world!_this_is_the_10th_test!"
Example
let { escapeString } = require("lc4");
escapeString("Hello World! This is the 10th test!", "lc4");
//=> "hello_world_this_is_the__#th_test"