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

Method performEncode

src/Encoder/ADFGXCipher.js:111–148  ·  view source on GitHub ↗

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

(content)

Source from the content-addressed store, hash-verified

109 * @return {number[]|string|Uint8Array|Chain} Encoded content
110 */
111 async performEncode (content) {
112 // Prepare content
113 let contentString = content.getString()
114 const variantName = this.getSettingValue('variant')
115 if (variantName === 'adfgx') {
116 // As I and J shares the same alphabet position, replace all J chars
117 contentString = contentString.replace(/j/gi, 'i')
118 }
119
120 // Derive column map from key
121 const key = this.getSettingValue('key')
122 const keyLength = key.getLength()
123 const columnMap = this.mapElementsToSortedPosition(key.getChars())
124
125 // Encode content to coordinates using Polybius square
126 const polybius = await this._polybiusSquare.encode(contentString)
127 const polybiusLength = polybius.getLength()
128
129 // Encode Polybius square coordinates using substitution and transposition
130 const result = new Array(polybiusLength + keyLength)
131 let i, column, row, index
132 let j = 0
133
134 for (i = 0; i < keyLength; i++) {
135 // Derive column from key
136 column = columnMap[i]
137
138 // Go through each column row
139 for (row = 0; row * keyLength + column < polybiusLength; row++) {
140 // Translate 2D column and row coordintes to 1D index
141 index = row * keyLength + column
142 result[j++] = polybius.getCodePointAt(index)
143 }
144 }
145
146 // Group result in pairs of 5 characters each, separated by a space
147 return ArrayUtil.joinSlices(ArrayUtil.chunk(result.slice(0, j), 5), [32])
148 }
149
150 /**
151 * Performs decode on given content.

Callers

nothing calls this directly

Calls 9

getStringMethod · 0.80
getSettingValueMethod · 0.80
getCharsMethod · 0.80
encodeMethod · 0.80
getCodePointAtMethod · 0.80
joinSlicesMethod · 0.80
getLengthMethod · 0.45
chunkMethod · 0.45

Tested by

no test coverage detected