diff --git a/src/cypher.ts b/src/cypher.ts index 9edd931..eec8389 100644 --- a/src/cypher.ts +++ b/src/cypher.ts @@ -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); + } } diff --git a/tests/cypher.test.ts b/tests/cypher.test.ts index e7abc60..0951aa1 100644 --- a/tests/cypher.test.ts +++ b/tests/cypher.test.ts @@ -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('); + }); });