UNPKG

shift-cipher

Version:

A simple package for encrypting and decrypting strings using Caesar cipher-like technique.

1 lines 693 B
export class ShiftCipher{constructor(){this.makeCipherMap({chars:"abcdefghijklmnopqrstuvqxyz",shift:13})}encode(str){return this.rotateChars(str,this.cipherMap)}decode(str){return this.rotateChars(str,this.reversedCipherMap)}makeCipherMap({chars:chars,shift:shift}){shift%=chars.length;let shiftedChars=chars.slice(shift)+chars.slice(0,shift);shiftedChars+=shiftedChars.toUpperCase(),chars+=chars.toUpperCase(),this.cipherMap=new Map(chars.split("").map((i,j)=>[i,shiftedChars[j]]))}get reversedCipherMap(){const reversedChars=[...this.cipherMap.entries()].map(i=>i.reverse());return new Map(reversedChars)}rotateChars(str,cipherMap){return str.split("").map(i=>cipherMap.get(i)||i).join("")}}