LC4 alphabet
Kind: static constant of lc4/config
LS47 alphabet
Kind: static constant of lc4/config
LC4 state grid size
Kind: static constant of lc4/config
LS47 state grid size
Kind: static constant of lc4/config
Default LC4/LS47 encryption/decryption settings
Kind: static constant of lc4/config
- lc4/helpers
- .escapeString(string, [mode]) ⇒
String - .shuffle(arr) ⇒
Array - .randomElement(arr) ⇒
* - .shiftRowRight(state, row, marker, [mode]) ⇒
Array - .shiftColumnDown(state, col, marker, [mode]) ⇒
Array - .position(char, state) ⇒
Array - .printState(state, chara, marker, [mode]) ⇒
undefined - .validString(input, [mode]) ⇒
Boolean
- .escapeString(string, [mode]) ⇒
Escape string to valid LC4 or LS47 string
Kind: static method of lc4/helpers
Returns: String - valid LC4 or LS47 string
| Param | Type | Default | Description |
|---|---|---|---|
| string | String |
(invalid) LC4 or LS47 string | |
| [mode] | String |
"lc4" |
Escape mode (either "lc4" or "ls47") |
Example
escapeString("Hello World! This is the 10th test!");
//=> "hello_world_this_is_the__#th_test"Fisher-Yates array Shuffle
Kind: static method of lc4/helpers
Returns: Array - shuffled array
| Param | Type | Description |
|---|---|---|
| arr | Array |
input array to be shuffled |
Pick a random element from an array
Kind: static method of lc4/helpers
Returns: * - a random element from the array
| Param | Type | Description |
|---|---|---|
| arr | Array |
input array to pick element from |
Shift given row in the state matrix and move the marker if needed
Kind: static method of lc4/helpers
Returns: Array - updated state matrix
| Param | Type | Default | Description |
|---|---|---|---|
| state | Array |
state matrix | |
| row | Number |
index of row to shift | |
| marker | Object |
marker object representing active element | |
| marker.i | Number |
row of the marker in the state | |
| marker.j | Number |
column of the marker in the state | |
| [mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Shift given column in the state matrix and move the marker if needed
Kind: static method of lc4/helpers
Returns: Array - updated state matrix
| Param | Type | Default | Description |
|---|---|---|---|
| state | Array |
state matrix | |
| col | Number |
index of column to shift | |
| marker | Object |
marker object representing active element | |
| marker.i | Number |
row of the marker in the state | |
| marker.j | Number |
column of the marker in the state | |
| [mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Return the coordinates of given search element in the state matrix
Kind: static method of lc4/helpers
Returns: Array - position vector in the form [row, column]
| Param | Type | Description |
|---|---|---|
| char | * |
search element |
| state | Array |
state matrix |
Print out state for verbose mode
Kind: static method of lc4/helpers
| Param | Type | Default | Description |
|---|---|---|---|
| state | Array |
state array to print out | |
| chara | Object |
input character reference being encrypted/decrypted | |
| chara.row | Number |
row of input character in the state matrix (-1 for no input character) | |
| chara.col | Number |
column of input character in the state matrix (-1 for no input character) | |
| marker | Object |
marker object representing active element | |
| marker.i | Number |
row of the marker in the state | |
| marker.j | Number |
column of the marker in the state | |
| [mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Determine if input contains only valid LC4 or LS47 characters
Kind: static method of lc4/helpers
Returns: Boolean - indicating if input is valid LC4 or LS47
| Param | Type | Default | Description |
|---|---|---|---|
| input | Array |
input array | |
| [mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
- lc4/lc4
- .generateKey([mode]) ⇒
String - .generateNonce([mode], [length]) ⇒
String - .initState(key, [mode]) ⇒
Array - .encryptMsg(env, msg, [verbose]) ⇒
String - .decryptMsg(env, msg, [verbose]) ⇒
String
- .generateKey([mode]) ⇒
Generate a valid random LC4 or LS47 key
Kind: static method of lc4/lc4
Returns: String - a valid LC4 or LS47 key
| Param | Type | Default | Description |
|---|---|---|---|
| [mode] | String |
"lc4" |
encryption/decryption algorithm. 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/lc4
Returns: String - a valid LC4 or LS47 nonce
Throws:
ErrorWill throw an error if length is smaller than 6
| Param | Type | Default | Description |
|---|---|---|---|
| [mode] | String |
"lc4" |
encryption/decryption algorithm. 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 (Generate a random LS47 nonce)
let { generateNonce } = require("lc4");
generateNonce("ls47");Example (Encrypt a message with a random nonce)
const { encrypt, generateKey, generateNonce } = require("lc4");
encrypt({
message: "Lorem Ipsum",
key: generateKey(),
nonce: generateNonce()
})Populate a state matrix by filling in a key row by row or by expanding a key
Kind: static method of lc4/lc4
Returns: Array - state matrix
| Param | Type | Default | Description |
|---|---|---|---|
| key | String | Array |
key string or array | |
| [mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Encrypt a cleartext message and change the environment
Kind: static method of lc4/lc4
Returns: String - ciphertext message
| Param | Type | Default | Description |
|---|---|---|---|
| env | Object |
environment object | |
| env.state | Array |
state matrix | |
| env.marker | Object |
marker object representing active element | |
| env.marker.i | Number |
row of the marker in the state | |
| env.marker.j | Number |
column of the marker in the state | |
| env.mode | String |
encryption algorithm. Can be either "lc4" or "ls47" | |
| msg | String |
cleartext message | |
| [verbose] | Boolean |
false |
boolean indicating wether verbose mode should be used (will print out intermediate steps) |
Decrypt a ciphertext message and change the environment
Kind: static method of lc4/lc4
Returns: String - cleartext message
| Param | Type | Default | Description |
|---|---|---|---|
| env | Object |
environment object | |
| env.state | Array |
state matrix | |
| env.marker | Object |
marker object representing active element | |
| env.marker.i | Number |
row of the marker in the state | |
| env.marker.j | Number |
column of the marker in the state | |
| env.mode | Strin |
decryption algorithm. Can be either "lc4" or "ls47" | |
| msg | String |
ciphertext message | |
| [verbose] | Boolean |
false |
boolean indicating wether verbose mode should be used (will print out intermediate steps) |
- 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:
TypeErrorWill 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:
ErrorWill throw error "Invalid Signature" if message doesn't end with specified signatureTypeErrorWill 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:
ErrorWill 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"- lc4/validate
- .validateMode(settings) ⇒
undefined - .validateMsg(settings) ⇒
undefined - .validateHeaderData(settings) ⇒
undefined - .validateKey(settings) ⇒
undefined - .validateNonce(settings) ⇒
undefined - .validateSignature(settings) ⇒
undefined - .validateSettings(settings) ⇒
undefined
- .validateMode(settings) ⇒
Validates the mode option of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen settings.mode is invalid
| Param | Type | Description |
|---|---|---|
| settings | Object |
settings object |
| settings.mode | String |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validates the message of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen no message is specified or the message is invalid
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
settings object | |
| settings.message | String | Array |
valid LC4 or LS47 message or array of valid strings | |
| [settings.mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validates the headerDate option of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen header data is specified but contains illegal characters
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
settings object | |
| [settings.headerData] | String |
|
optional valid header data |
| [settings.mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validates the key of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen key is not specified, too short or contains illegal characters
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
settings object | |
| settings.key | String |
valid key (no illegal characters, no duplicate characters if as long as alphabet) or password | |
| [settings.mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validates nonce option of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen nonce is specified and too short (< 6 characters) or contains illegal characters
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
settings object | |
| [settings.nonce] | String |
|
optional valid nonce |
| [settings.mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validates signature option of the settings
Kind: static method of lc4/validate
Throws:
TypeErrorwhen signature is specified and too short (< 10 characters) or contains illegal characters
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
settings object | |
| [settings.signature] | String |
|
optional valid signature |
| [settings.mode] | String |
"lc4" |
encryption/decryption algorithm. Can be either "lc4" or "ls47" |
Validate encryption/decryption LC4 settings
Kind: static method of lc4/validate
Throws:
TypeErrorWhen message and/or key and/or mode is missing or if invalid value (invalid LC4 or LS47 string) is passed
| Param | Type | Default | Description |
|---|---|---|---|
| settings | Object |
LC4 settings message | |
| settings.mode | String |
encryption/decryption algorithm. Can be either "lc4" or "ls47" | |
| settings.message | String |
valid LC4 or LS47 string | |
| settings.key | String |
valid LC4 or LS47 string | |
| [settings.signature] | String |
|
valid LC4 or LS47 string (at least 10 characters long) |
| [settings.headerData] | String |
|
valid LC4 or LS47 string |
| [settings.nonce] | String |
|
valid LC4 or LS47 string (at least 6 characters long) |