* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Decoded content
(content)
| 130 | * @return {number[]|string|Uint8Array|Chain} Decoded content |
| 131 | */ |
| 132 | async performDecode (content) { |
| 133 | const key = this.getSettingValue('key') |
| 134 | const separator = this.getSettingValue('separator') |
| 135 | const values = content.getString().split(separator) |
| 136 | |
| 137 | const contentLength = values.length |
| 138 | const keyLength = key.getLength() |
| 139 | |
| 140 | const keyPolybius = await this._polybiusSquare.encode(key) |
| 141 | |
| 142 | let value, plaintextValue, keyIndex, keyValue |
| 143 | let plaintextPolybius = '' |
| 144 | |
| 145 | for (let i = 0; i < contentLength; i++) { |
| 146 | // Read next value |
| 147 | value = parseInt(values[i]) |
| 148 | if (isNaN(value)) { |
| 149 | throw new InvalidInputError( |
| 150 | `Block at index ${i + 1} is not a number.`) |
| 151 | } |
| 152 | |
| 153 | // Compose key number |
| 154 | keyIndex = MathUtil.mod(i, keyLength) * 2 |
| 155 | keyValue = parseInt( |
| 156 | keyPolybius.getCharAt(keyIndex) + |
| 157 | keyPolybius.getCharAt(keyIndex + 1) |
| 158 | ) |
| 159 | |
| 160 | // Compute plaintext number |
| 161 | plaintextValue = value - keyValue |
| 162 | if (!plaintextValue.toString().match(/^[1-5]{2}$/)) { |
| 163 | throw new InvalidInputError( |
| 164 | `Block at index ${i + 1} results in invalid ` + |
| 165 | `Polybius square coordinates '${plaintextValue}'.`) |
| 166 | } |
| 167 | |
| 168 | plaintextPolybius += plaintextValue.toString() |
| 169 | } |
| 170 | |
| 171 | // Unwrap Polybius square encoded content |
| 172 | return this._polybiusSquare.decode(plaintextPolybius) |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Triggered when a setting field has changed. |