-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.min.js
15 lines (15 loc) · 1.69 KB
/
cipher.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'use strict';class Cipher{constructor(secret,props={alpha:'ABCDEFGHIJKLMNOPQRSTUVWXYZ',toUpper:true,stripCharacters:true,addSpaces:true,stripSpaces:true})
{this.props=props;this.alpha=props.alpha;this.trans=[];this.key=this._parse(secret)
this.key=this._distinct(this.key);this.cipher=this.key.split('');this.cipher=this.cipher.sort();this.width=this.cipher.length;this.height=Math.floor(this.alpha.length/this.cipher.length)+1;var kAlpha=this.key+this.alpha;kAlpha=this._distinct(kAlpha);const padding=Array(Math.abs(this.width*this.height-kAlpha.length)+1).join(" ");this.kAlpha=kAlpha+padding;this.cipher.forEach((_,i)=>{let letter=this.kAlpha[i];let cipherIndex=this.cipher.indexOf(letter);this.trans[cipherIndex]=cipherIndex-i;});this.encryptedAlpha=this._parse(this.kAlpha.split('').map((_,i)=>{let kAlphaIndex=this._max(this._flatIndex(i)-this.trans[this._flatIndex(i)%this.width],this.kAlpha.length)
return this.kAlpha[kAlphaIndex]}).join(''));}
_distinct=(val)=>val.split('').filter((item,i,self)=>self.indexOf(item)===i).join('')
_max=(i,max)=>i>max?max:i
_flatIndex=(i)=>Math.floor(i/this.height)+((i%this.height)*this.width)
_addSpaces=(s)=>this.props.addSpaces?s.replace(/.{5}/g,'$& '):s
_crypt=(s,alphaA,alphaB)=>this._addSpaces(s.split('').map(lttr=>alphaA[alphaB.indexOf(lttr)]).join(''))
_convertCase=(s)=>this.props.toUpper?s.toUpperCase():s;_stripCharacters=(s)=>this.props.stripCharacters?s.replace(/[^a-zA-Z]/g,''):s
_stripSpaces=(s)=>this.props.stripSpaces?s.replace(/ /g,''):s
_parse=(s)=>this._stripCharacters(this._stripSpaces(this._convertCase(s)))
encrypt=(s)=>this._crypt(this._parse(s),this.encryptedAlpha,this.alpha)
decrypt=(s)=>this._crypt(s,this.alpha,this.encryptedAlpha)}
module.exports=Cipher;