Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,21 @@ export class Cypher {
'o',
];

public decrypt(message: string): string {
private _translate(dictSource: string[], dictTarget: string[], message: string): string {
let result: string = '';
for (let i = 0; i < message.length; i++) {
const char: string = message.charAt(i);
const index: number = this._decryptionKey.indexOf(char);
result += this._alphabet[index];
const index: number = dictSource.indexOf(char);
result += dictTarget[index];
}
return result;
}

public decrypt(message: string): string {
return this._translate(this._decryptionKey, this._alphabet, message);
}

public encrypt(message: string): string {
return this._translate(this._alphabet, this._decryptionKey, message);
}
}
8 changes: 8 additions & 0 deletions tests/cypher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ describe('Cypher', () => {
const cypher: Cypher = new Cypher();
expect(cypher.decrypt('ldga(')).toBe('world');
});
it('should encrypt hello', () => {
const cypher: Cypher = new Cypher();
expect(cypher.encrypt('hello')).toBe('&£aad');
});
it('should encrypt world', () => {
const cypher: Cypher = new Cypher();
expect(cypher.encrypt('world')).toBe('ldga(');
});
});
Loading