Skip to content

Commit b498873

Browse files
caesar cipher
1 parent 875151a commit b498873

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
1. [Хеш-таблица / Hash-Table](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/hash-table/hash-table.js) | [Инфо](https://bitsofmind.wordpress.com/2008/07/28/introduction_in_hash_tables/)
2727
2. [Хеш-таблица c открытой адресацией / Linear hashing](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/hash-table/linear-hashing/linear-hashing.js) | [Инфо](http://algolist.manual.ru/ds/s_has.php)
2828

29+
## Шифрование / Cryptographic
30+
1. [Шифр Цезаря / Caesar Cipher](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/cryptographic/caesar-cipher/caesar-cipher.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A8%D0%B8%D1%84%D1%80_%D0%A6%D0%B5%D0%B7%D0%B0%D1%80%D1%8F)
31+
2932
## Другое / Other
3033
1. [Решето Эратосфена / Sieve Of Eratosthenes](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/other/sieve-of-eratosthenes.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A0%D0%B5%D1%88%D0%B5%D1%82%D0%BE_%D0%AD%D1%80%D0%B0%D1%82%D0%BE%D1%81%D1%84%D0%B5%D0%BD%D0%B0)

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<script src="./source/search/linear-search/linear-search.js" type="module"></script>
2121
<script src="./source/search/binary-search/binary-search.js" type="module"></script>
2222
<script src="./source/hash-table/hash-table.js" type="module"></script>
23+
<script src="./source/cryptographic/caesar-cipher/caesar-cipher.js" type="module"></script>
2324
<script src="./source/hash-table/linear-hashing/linear-hashing.js" type="module"></script>
2425
<script src="./source/other/sieve-of-eratosthenes.js" type="module"></script>
2526
<script src="./source/main.js" type="module"></script>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export default class CaesarCipher {
2+
constructor(alphabet, characters, shift) {
3+
this.alphabet = alphabet.toLowerCase();
4+
this.characters = characters;
5+
this.shift = shift;
6+
this._shiftedAlphabet = "";
7+
}
8+
_shiftAlhabet() {
9+
this._shiftedAlphabet = this.alphabet.slice(this.shift);
10+
this._shiftedAlphabet += this.alphabet.slice(0, this.shift);
11+
this._shiftedAlphabet += this.characters;
12+
this.alphabet += this.characters;
13+
}
14+
encrypt(message) {
15+
let encryptMessage = "";
16+
this._shiftAlhabet();
17+
for (let i = 0; i < message.length; i++) {
18+
let letterPositionInAlphabet = this.alphabet.indexOf(message[i]);
19+
encryptMessage += this._shiftedAlphabet[letterPositionInAlphabet];
20+
}
21+
return encryptMessage;
22+
}
23+
decrypt(message) {
24+
let decryptMessage = "";
25+
this._shiftAlhabet();
26+
for (let i = 0; i < message.length; i++) {
27+
let letterPositionInAlphabet = this._shiftedAlphabet.indexOf(
28+
message[i]
29+
);
30+
decryptMessage += this.alphabet[letterPositionInAlphabet];
31+
}
32+
return decryptMessage;
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import CaesarCipher from "./caesar-cipher";
2+
describe("caesar-cipher", () => {
3+
test("should return khoor zruog after encrypt", () => {
4+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5+
const characters = "/* ";
6+
const c = new CaesarCipher(alphabet, characters, 3);
7+
expect(c.encrypt("hello world")).toBe("khoor zruog");
8+
});
9+
test("should return hello world after decrypt", () => {
10+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
const characters = "/* ";
12+
const c = new CaesarCipher(alphabet, characters, 3);
13+
expect(c.decrypt("khoor zruog")).toBe("hello world");
14+
});
15+
});

0 commit comments

Comments
 (0)