@mezgoodle/caesar-and-vigenere-ciphers
v2.1.0
Published
NPM package of caesar-and-vigenere-ciphers
Downloads
6
Readme
Caesar and Vigenere ciphers
Hello everyone! This is the repository of my package on JavaScript "Caesar and Vigenere ciphers"
Table of contents
- Motivation
- Build status
- Code style
- Dependencies
- Features
- Installation
- Importing
- Fast usage
- API
- Code Example
- Tests
- Contributing
- License
Motivation
I've always been interested in the topic of data encryption, but I didn't delve deeply into it. So I decided to do it on. At first there was an idea to do a CLI or an Electron application. But later it turned into a package for npm.
Build status
Here you can see build status of continuous integration/continuous deployment:
Code style
I'm using Codacy for automate my code quality.
Dependencies
I'm using only jest for testing, but that's enough for unit-testing. You may not install it.
You can see all dependencies from
package-lock.json
here.
Features
With my package you can encrypt / decrypt text with Caesar and Wiegener algorithms with latin, cyrillic or greek alphabet based on UTF-16.
If you see incorrect work with alphabet, please write an issue.
Installation
First install Node.js. Then:
npm i @mezgoodle/caesar-and-vigenere-ciphers --save
Importing
// Using Node.js `require()`
const { caesarEncrypt, caesarDecrypt, vigenereEncryptText, vigenereDecryptText } = require("@mezgoodle/caesar-and-vigenere-ciphers");
Fast usage
// Latin
console.log(caesarEncrypt("pellentesque", 12, "lat"));
// expected output: bqxxqzfqecgq
console.log(caesarDecrypt("pellentesque", -12, "lat"));
// expected output: bqxxqzfqecgq
console.log(vigenereEncrypt("unopinionated", "express", "lat"));
// expected output: ykdgmfaskpkiv
console.log(vigenereDecrypt("ykdgmfaskpkiv", "express", "lat"));
// expected output: unopinionated
// Cyrillic
console.log(caesarEncrypt("привет", 2, "cyr"));
// expected output: сткдзф
console.log(caesarDecrypt("сткдзф", 2, "cyr"));
// expected output: привет
console.log(vigenereEncrypt("Карл у Клары украл кораллы", "кларнет", "cyr"));
// expected output: Флры а Пэкыы гчхтх хоанрэе
console.log(vigenereDecrypt("Флры а Пэкыы гчхтх хоанрэе", "кларнет", "cyr"));
// expected output: Карл у Клары украл кораллы
// Greek
console.log(caesarEncrypt("αβ", 1, "gre"));
// expected output: βγ
console.log(caesarDecrypt("βγ", 1, "gre"));
// expected output: αβ
console.log(vigenereEncrypt("ΓειΑ", "βι", "gre"));
// expected output: ΔνκΙ
console.log(vigenereDecrypt("ΔνκΙ", "βι", "gre"));
// expected output: ΓειΑ
API
caesarEncrypt( value, amount, type )
Name | Type | Argument | Default | Description
--------|----------|--------------|---------|------------
value | string
| <required>
| null
| the message to encrypt
amount | number
| <required>
| null
| the key to encrypt the message with
type | string
| <required>
| null
| the type of language: latin or cyrillic
caesarDecrypt( value, amount, type )
Name | Type | Argument | Default | Description
--------|----------|--------------|---------|------------
value | string
| <required>
| null
| the message to decrypt
amount | number
| <required>
| null
| the key to decrypt the message with
type | string
| <required>
| null
| the type of language: latin or cyrillic
vigenereEncrypt( text, key, type )
Name | Type | Argument | Default | Description
--------|----------|--------------|---------|------------
text | string
| <required>
| null
| the message to encrypt
key | string
| <required>
| null
| the key to encrypt the message with
type | string
| <required>
| null
| the type of language: latin or cyrillic
vigenereDecrypt( text, key, type )
Name | Type | Argument | Default | Description
--------|----------|--------------|---------|------------
text | string
| <required>
| null
| the message to decrypt
key | string
| <required>
| null
| the key to decrypt the message with
type | string
| <required>
| null
| the type of language: latin or cyrillic
Code Example
Here you can see small example of Vigenere worker
const worker = (str, key, type, lang) => {
if (typeof(key) !== 'string' || typeof(str) !== 'string')
throw Error('Text and Key must be string');
if (!Object.prototype.hasOwnProperty.call(typeDefine, lang))
throw Error('Type must be "lat" or "cyr"');
if (str === null || key === null) throw Error('Message and key should be not empty');
key = keepLetters(key);
let result = '',
keyIndex = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charAt(i);
if (isLetter(char)) {
const key_el = key.charAt(keyIndex++ % key.length);
const temp = workerChar(char, key_el, type, lang);
result += temp;
} else {
result += char;
}
}
return result;
};
Tests
Unit-testing with assert and jest:
Early here were screenshots
Number of my tests is more than 50, so I can't just do screenshot :smile:. I give you the link to Travis CI, where you can see all my tests.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
MIT © mezgoodle