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

Method performDecode

src/Encoder/ADFGXCipher.js:156–196  ·  view source on GitHub ↗

* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Decoded content

(content)

Source from the content-addressed store, hash-verified

154 * @return {number[]|string|Uint8Array|Chain} Decoded content
155 */
156 performDecode (content) {
157 const variantName = this.getSettingValue('variant')
158 const variant = this.constructor.getVariant(variantName)
159 const squarePositions = Chain.wrap(variant.squarePositions).getCodePoints()
160
161 // Prepare content by removing non-position characters
162 const codePoints = content.getCodePoints()
163 .filter(codePoint => squarePositions.indexOf(codePoint) !== -1)
164 const length = codePoints.length
165
166 // Derive column map from key
167 const key = this.getSettingValue('key')
168 const keyLength = key.getLength()
169 const columnMap = this.mapElementsToSortedPosition(key.getChars())
170
171 // Calculate the number of columns available in the last row as the
172 // plaintext content may not have filled up the entire grid
173 const lastRowColumns = (length % keyLength) || keyLength
174 const rows = Math.ceil(length / keyLength)
175
176 // Decode code points to Polybius square coordinates
177 const polybius = new Array(length)
178 let index = 0
179 let i, column, row, columnRows
180
181 // Go through columns
182 for (i = 0; i < keyLength; i++) {
183 column = columnMap[i]
184
185 // The last row of this column may be empty
186 columnRows = column >= lastRowColumns ? rows - 1 : rows
187
188 // Transpose each column character
189 for (row = 0; row < columnRows; row++) {
190 polybius[row * keyLength + column] = codePoints[index++]
191 }
192 }
193
194 // Decode coordinates using Polybius square
195 return this._polybiusSquare.decode(polybius)
196 }
197
198 /**
199 * Maps the given elements to their corresponding positions in a sorted array.

Callers

nothing calls this directly

Calls 9

getSettingValueMethod · 0.80
getVariantMethod · 0.80
getCodePointsMethod · 0.80
wrapMethod · 0.80
indexOfMethod · 0.80
getCharsMethod · 0.80
decodeMethod · 0.80
getLengthMethod · 0.45

Tested by

no test coverage detected