Skip to content

Commit

Permalink
Merge branch 'algo/affinity-cipher'
Browse files Browse the repository at this point in the history
  • Loading branch information
theofilis committed Aug 7, 2018
2 parents 73bebf3 + 49b91ab commit 7bfdbf4
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 12 deletions.
74 changes: 72 additions & 2 deletions coverage.lcov
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
TN:
SF:D:\Projects\cryptography\src\AffineCipher.ts
FN:10,(anonymous_0)
FN:22,(anonymous_1)
FN:24,(anonymous_2)
FN:25,(anonymous_3)
FN:28,(anonymous_4)
FN:30,(anonymous_5)
FN:31,(anonymous_6)
FN:34,(anonymous_7)
FN:39,(anonymous_8)
FN:14,(anonymous_9)
FN:18,(anonymous_10)
FNF:11
FNH:9
FNDA:1,(anonymous_0)
FNDA:1,(anonymous_1)
FNDA:11,(anonymous_2)
FNDA:10,(anonymous_3)
FNDA:1,(anonymous_4)
FNDA:11,(anonymous_5)
FNDA:10,(anonymous_6)
FNDA:11,(anonymous_7)
FNDA:1,(anonymous_8)
FNDA:0,(anonymous_9)
FNDA:0,(anonymous_10)
DA:3,1
DA:5,1
DA:7,1
DA:8,1
DA:11,1
DA:14,1
DA:15,0
DA:18,1
DA:19,0
DA:22,1
DA:23,1
DA:24,11
DA:25,10
DA:28,1
DA:29,1
DA:30,11
DA:31,10
DA:34,1
DA:35,11
DA:36,11
DA:39,1
DA:40,1
DA:41,75
DA:42,75
DA:43,75
DA:45,75
DA:46,75
DA:47,75
DA:49,0
DA:55,1
LF:30
LH:27
BRDA:24,0,0,11
BRDA:24,0,1,1
BRDA:30,1,0,11
BRDA:30,1,1,1
BRDA:36,2,0,1
BRDA:36,2,1,10
BRDA:46,3,0,75
BRDA:46,3,1,0
BRF:8
BRH:7
end_of_record
TN:
SF:D:\Projects\cryptography\src\CaesarCipher.ts
FN:4,(anonymous_0)
FN:24,(anonymous_1)
Expand Down Expand Up @@ -138,8 +207,9 @@ FNH:0
DA:1,1
DA:2,1
DA:4,1
LF:3
LH:3
DA:5,1
LF:4
LH:4
BRF:0
BRH:0
end_of_record
46 changes: 46 additions & 0 deletions dist/AffineCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class AffineCipher {
constructor(a, b) {
this.ciphers = [];
this.N = 48;
this.M = 75;
this.encrypt = (text) => {
return text.split('')
.map(c => this.ciphers[c.charCodeAt(0) - this.M] || ' ')
.reduce((p, n) => p + n);
};
this.decrypt = (text) => {
return text.split('')
.map(c => this.decryptChar(c) || ' ')
.reduce((p, n) => p + n);
};
this.decryptChar = (cipher) => {
const index = this.ciphers.indexOf(cipher);
return (index == -1) ? null : String.fromCharCode(index + this.M);
};
this.affine = (a, b) => {
for (let i = this.N; i < this.M + this.N; i++) {
const x = i - this.N;
const x1 = a * x + b;
const x2 = x1 % this.M;
const cipher = String.fromCharCode(x2 + this.N);
if (this.ciphers.indexOf(cipher) == -1) {
this.ciphers.push(cipher);
}
else {
throw Error(`The value of a has to be coprime with ${this.M}`);
}
}
};
this.affine(a, b);
}
}
AffineCipher.encrypt = (text) => {
return '';
};
AffineCipher.decrypt = (text) => {
return '';
};
exports.AffineCipher = AffineCipher;
exports.default = AffineCipher;
2 changes: 2 additions & 0 deletions dist/Cipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2 changes: 2 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ var CaesarCipher_1 = require("./CaesarCipher");
exports.CaesarCipher = CaesarCipher_1.CaesarCipher;
var MorseCipher_1 = require("./MorseCipher");
exports.MorseCipher = MorseCipher_1.MorseCipher;
var AffineCipher_1 = require("./AffineCipher");
exports.AffineCipher = AffineCipher_1.AffineCipher;
14 changes: 14 additions & 0 deletions dist/types/AffineCipher.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Cipher } from './Cipher';
export declare class AffineCipher implements Cipher {
private ciphers;
private N;
private M;
constructor(a: number, b: number);
static encrypt: (text: string) => string;
static decrypt: (text: string) => string;
encrypt: (text: string) => string;
decrypt: (text: string) => string;
private decryptChar;
private affine;
}
export default AffineCipher;
4 changes: 4 additions & 0 deletions dist/types/Cipher.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Cipher {
encrypt(text: string): string;
decrypt(text: string): string;
}
1 change: 1 addition & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Rot13Cipher } from './Rot13Cipher';
export { CaesarCipher } from './CaesarCipher';
export { MorseCipher } from './MorseCipher';
export { AffineCipher } from './AffineCipher';
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typescript-project-template",
"version": "2.1.0",
"description": "A TS project template",
"name": "cryptography",
"version": "2.2.0",
"description": "A cryptography algorithms",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
"keywords": [
Expand All @@ -10,7 +10,7 @@
],
"repository": {
"type": "git",
"url": "git@github.com:theofilis/typescript-project-template.git"
"url": "git@github.com:synaphea/cryptography.git"
},
"scripts": {
"prepare": "npm run build",
Expand Down
55 changes: 55 additions & 0 deletions src/AffineCipher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Cipher } from './Cipher';

export class AffineCipher implements Cipher {

private ciphers: Array<string> = [];

private N = 48;
private M = 75;

constructor(a: number, b: number) {
this.affine(a, b);
}

public static encrypt = (text: string) => {
return '';
}

public static decrypt = (text: string) => {
return '';
}

public encrypt = (text: string): string => {
return text.split('')
.map(c => this.ciphers[c.charCodeAt(0) - this.M] || ' ')
.reduce((p, n) => p + n);
}

public decrypt = (text: string) => {
return text.split('')
.map(c => this.decryptChar(c) || ' ')
.reduce((p, n) => p + n);
}

private decryptChar = (cipher: string) => {
const index = this.ciphers.indexOf(cipher);
return (index == -1) ? null : String.fromCharCode(index + this.M);
}

private affine = (a: number, b: number) => {
for (let i = this.N; i < this.M + this.N; i++) {
const x = i - this.N;
const x1 = a * x + b;
const x2 = x1 % this.M;

const cipher = String.fromCharCode(x2 + this.N);
if (this.ciphers.indexOf(cipher) == -1) {
this.ciphers.push(cipher);
} else {
throw Error(`The value of a has to be coprime with ${this.M}`);
}
}
}
}

export default AffineCipher;
4 changes: 4 additions & 0 deletions src/Cipher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Cipher {
encrypt(text: string): string;
decrypt(text: string): string;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { Rot13Cipher } from './Rot13Cipher';
export { CaesarCipher } from './CaesarCipher';

export { MorseCipher } from './MorseCipher';
export { AffineCipher } from './AffineCipher';
18 changes: 12 additions & 6 deletions test-results.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.001" tests="10" failures="0">
<testsuite name="Root Suite" timestamp="2018-07-23T06:43:49" tests="0" failures="0" time="0">
<testsuites name="Mocha Tests" time="0.001" tests="12" failures="0">
<testsuite name="Root Suite" timestamp="2018-08-07T13:03:23" tests="0" failures="0" time="0">
</testsuite>
<testsuite name="Test &quot;CaesarCipher&quot;" timestamp="2018-07-23T06:43:49" tests="4" file="D:\Projects\cryptography\test\CaesarCipher.spec.ts" failures="0" time="0.001">
<testcase name="Test &quot;CaesarCipher&quot; Check static &quot;CaesarCipher.encrypt&quot;" time="0.001" classname="Check static &quot;CaesarCipher.encrypt&quot;">
<testsuite name="Test &quot;AffineCipher&quot;" timestamp="2018-08-07T13:03:23" tests="2" file="D:\Projects\cryptography\test\AffineCipher.spec.ts" failures="0" time="0.001">
<testcase name="Test &quot;AffineCipher&quot; Check &quot;AffineCipher.encrypt&quot;" time="0.001" classname="Check &quot;AffineCipher.encrypt&quot;">
</testcase>
<testcase name="Test &quot;AffineCipher&quot; Check &quot;AffineCipher.decrypt&quot;" time="0" classname="Check &quot;AffineCipher.decrypt&quot;">
</testcase>
</testsuite>
<testsuite name="Test &quot;CaesarCipher&quot;" timestamp="2018-08-07T13:03:23" tests="4" file="D:\Projects\cryptography\test\CaesarCipher.spec.ts" failures="0" time="0">
<testcase name="Test &quot;CaesarCipher&quot; Check static &quot;CaesarCipher.encrypt&quot;" time="0" classname="Check static &quot;CaesarCipher.encrypt&quot;">
</testcase>
<testcase name="Test &quot;CaesarCipher&quot; Check static &quot;CaesarCipher.decrypt&quot;" time="0" classname="Check static &quot;CaesarCipher.decrypt&quot;">
</testcase>
Expand All @@ -12,13 +18,13 @@
<testcase name="Test &quot;CaesarCipher&quot; Check &quot;CaesarCipher.decrypt&quot;" time="0" classname="Check &quot;CaesarCipher.decrypt&quot;">
</testcase>
</testsuite>
<testsuite name="Test &quot;MorseCipher&quot;" timestamp="2018-07-23T06:43:49" tests="2" file="D:\Projects\cryptography\test\MorseCipher.spec.ts" failures="0" time="0">
<testsuite name="Test &quot;MorseCipher&quot;" timestamp="2018-08-07T13:03:23" tests="2" file="D:\Projects\cryptography\test\MorseCipher.spec.ts" failures="0" time="0">
<testcase name="Test &quot;MorseCipher&quot; Check &quot;MorseCipher.encrypt&quot;" time="0" classname="Check &quot;MorseCipher.encrypt&quot;">
</testcase>
<testcase name="Test &quot;MorseCipher&quot; Check &quot;MorseCipher.decrypt&quot;" time="0" classname="Check &quot;MorseCipher.decrypt&quot;">
</testcase>
</testsuite>
<testsuite name="Test &quot;Rot13Cipher&quot;" timestamp="2018-07-23T06:43:49" tests="4" file="D:\Projects\cryptography\test\Rot13Cipher.spec.ts" failures="0" time="0">
<testsuite name="Test &quot;Rot13Cipher&quot;" timestamp="2018-08-07T13:03:23" tests="4" file="D:\Projects\cryptography\test\Rot13Cipher.spec.ts" failures="0" time="0">
<testcase name="Test &quot;Rot13Cipher&quot; Check static &quot;Rot13Cipher.encrypt&quot;" time="0" classname="Check static &quot;Rot13Cipher.encrypt&quot;">
</testcase>
<testcase name="Test &quot;Rot13Cipher&quot; Check static &quot;Rot13Cipher.decrypt&quot;" time="0" classname="Check static &quot;Rot13Cipher.decrypt&quot;">
Expand Down
25 changes: 25 additions & 0 deletions test/AffineCipher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from 'chai';

import { AffineCipher } from '../src/index';

describe(`Test "AffineCipher"`, () => {
const encrypter = new AffineCipher(7, 10);
let encrypted = 'oZ@@U BUj@S';
let decrypted = 'hello world';

// it('Check static "AffineCipher.encrypt"', () => {
// expect(encrypted).to.be.equal(AffineCipher.encrypt(decrypted, 10));
// });

// it('Check static "AffineCipher.decrypt"', () => {
// expect(decrypted).to.be.equal(AffineCipher.decrypt(encrypted, 10));
// });

it('Check "AffineCipher.encrypt"', () => {
expect(encrypted).to.be.equal(encrypter.encrypt(decrypted));
});

it('Check "AffineCipher.decrypt"', () => {
expect(decrypted).to.be.equal(encrypter.decrypt(encrypted));
});
});

0 comments on commit 7bfdbf4

Please sign in to comment.