MCPcopy Create free account
hub / github.com/cryptii/cryptii / performTranslate

Method performTranslate

src/Encoder/VigenereCipher.js:110–195  ·  view source on GitHub ↗

* Performs encode or decode on given content. * @protected * @param {Chain} content * @param {boolean} isEncode True for encoding, false for decoding * @return {number[]|string|Uint8Array|Chain} Resulting content

(content, isEncode)

Source from the content-addressed store, hash-verified

108 * @return {number[]|string|Uint8Array|Chain} Resulting content
109 */
110 performTranslate (content, isEncode) {
111 const { variant, caseStrategy, includeForeignChars } =
112 this.getSettingValues()
113
114 // Prepare alphabet(s) depending on case strategy
115 let alphabet = this.getSettingValue('alphabet')
116 let uppercaseAlphabet
117 if (caseStrategy !== 'strict') {
118 alphabet = alphabet.toLowerCase()
119 uppercaseAlphabet = alphabet.toUpperCase()
120 }
121
122 // Choose key and key mode
123 let { key, keyMode } = this.getSettingValues()
124 if (variant === 'trithemius-cipher') {
125 key = alphabet
126 keyMode = 'repeat'
127 }
128
129 const inputLength = content.getLength()
130 const result = new Array(inputLength)
131
132 let charIndex, codePoint, uppercase, keyCodePoint, keyIndex
133 let j = 0
134 let k = 0
135
136 // Translate each character
137 for (let i = 0; i < inputLength; i++) {
138 codePoint = content.getCodePointAt(i)
139
140 // Match alphabet character
141 charIndex = alphabet.indexOfCodePoint(codePoint)
142 uppercase = false
143
144 // Match uppercase alphabet character (depending on case strategy)
145 if (charIndex === -1 && caseStrategy !== 'strict') {
146 charIndex = uppercaseAlphabet.indexOfCodePoint(codePoint)
147 uppercase = true
148 }
149
150 if (charIndex !== -1) {
151 // Calculate shift from key
152 keyCodePoint = key.getCodePointAt(MathUtil.mod(k, key.getLength()))
153 keyIndex = alphabet.indexOfCodePoint(keyCodePoint)
154
155 // Shift char index depending on variant
156 switch (variant) {
157 case 'beaufort-cipher':
158 charIndex = keyIndex - charIndex
159 break
160 case 'variant-beaufort-cipher':
161 charIndex = isEncode
162 ? charIndex - keyIndex
163 : charIndex + keyIndex
164 break
165 default:
166 charIndex = isEncode
167 ? charIndex + keyIndex

Callers

nothing calls this directly

Calls 10

getSettingValuesMethod · 0.80
getSettingValueMethod · 0.80
toLowerCaseMethod · 0.80
toUpperCaseMethod · 0.80
getCodePointAtMethod · 0.80
indexOfCodePointMethod · 0.80
modMethod · 0.80
joinMethod · 0.80
wrapMethod · 0.80
getLengthMethod · 0.45

Tested by

no test coverage detected