Skip to content

Latest commit

 

History

History
730 lines (548 loc) · 27 KB

dev.md

File metadata and controls

730 lines (548 loc) · 27 KB

Modules

lc4/config
lc4/helpers
lc4/lc4
lc4
lc4/validate

lc4/config

lc4/config.ALPHABET

LC4 alphabet

Kind: static constant of lc4/config

lc4/config.ALPHABET_LS47

LS47 alphabet

Kind: static constant of lc4/config

lc4/config.GRIDSIZE

LC4 state grid size

Kind: static constant of lc4/config

lc4/config.GRIDSIZE_LS47

LS47 state grid size

Kind: static constant of lc4/config

lc4/config.DEFAULT_SETTINGS

Default LC4/LS47 encryption/decryption settings

Kind: static constant of lc4/config

lc4/helpers

lc4/helpers.escapeString(string, [mode]) ⇒ String

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"

lc4/helpers.shuffle(arr) ⇒ Array

Fisher-Yates array Shuffle

Kind: static method of lc4/helpers
Returns: Array - shuffled array

Param Type Description
arr Array input array to be shuffled

lc4/helpers.randomElement(arr) ⇒ *

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

lc4/helpers.shiftRowRight(state, row, marker, [mode]) ⇒ Array

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"

lc4/helpers.shiftColumnDown(state, col, marker, [mode]) ⇒ Array

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"

lc4/helpers.position(char, state) ⇒ Array

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

lc4/helpers.printState(state, chara, marker, [mode]) ⇒ undefined

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"

lc4/helpers.validString(input, [mode]) ⇒ Boolean

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

lc4/lc4.generateKey([mode]) ⇒ String

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(),
});

lc4/lc4.generateNonce([mode], [length]) ⇒ String

Generate a valid random LC4 or LS47 nonce

Kind: static method of lc4/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 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()
})

lc4/lc4.initState(key, [mode]) ⇒ Array

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"

lc4/lc4.encryptMsg(env, msg, [verbose]) ⇒ String

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)

lc4/lc4.decryptMsg(env, msg, [verbose]) ⇒ String

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

lc4.encrypt(settings) ⇒ String

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
});

lc4.decrypt(settings) ⇒ String

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 signature
  • TypeError 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"]

lc4.generateKey([mode]) ⇒ String

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(),
});

lc4.generateNonce([mode], [length]) ⇒ String

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")
})

lc4.escapeToLC4(string) ⇒ String

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"

lc4.escapeToLS47(string) ⇒ String

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!"

lc4.escapeString(string, [mode]) ⇒ String

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

lc4/validate.validateMode(settings) ⇒ undefined

Validates the mode option of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when settings.mode is invalid
Param Type Description
settings Object settings object
settings.mode String encryption/decryption algorithm. Can be either "lc4" or "ls47"

lc4/validate.validateMsg(settings) ⇒ undefined

Validates the message of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when 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"

lc4/validate.validateHeaderData(settings) ⇒ undefined

Validates the headerDate option of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when 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"

lc4/validate.validateKey(settings) ⇒ undefined

Validates the key of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when 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"

lc4/validate.validateNonce(settings) ⇒ undefined

Validates nonce option of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when 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"

lc4/validate.validateSignature(settings) ⇒ undefined

Validates signature option of the settings

Kind: static method of lc4/validate
Throws:

  • TypeError when 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"

lc4/validate.validateSettings(settings) ⇒ undefined

Validate encryption/decryption LC4 settings

Kind: static method of lc4/validate
Throws:

  • TypeError When 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)