* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Encoded content
(content)
| 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. |
nothing calls this directly
no test coverage detected