* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Encoded content
(content)
| 88 | * @return {number[]|string|Uint8Array|Chain} Encoded content |
| 89 | */ |
| 90 | async performEncode (content) { |
| 91 | const key = this.getSettingValue('key') |
| 92 | const separator = this.getSettingValue('separator') |
| 93 | |
| 94 | // Translate both plaintext and key using the Polybius square |
| 95 | const plaintextPolybius = await this._polybiusSquare.encode( |
| 96 | content.getString().replace(/j/gi, 'i')) |
| 97 | const keyPolybius = await this._polybiusSquare.encode(key) |
| 98 | |
| 99 | const contentLength = plaintextPolybius.getLength() / 2 |
| 100 | const keyLength = key.getLength() |
| 101 | |
| 102 | const values = new Array(contentLength) |
| 103 | let plaintextValue, keyIndex, keyValue |
| 104 | |
| 105 | for (let i = 0; i < contentLength; i++) { |
| 106 | // Compose plaintext number |
| 107 | plaintextValue = parseInt( |
| 108 | plaintextPolybius.getCharAt(i * 2) + |
| 109 | plaintextPolybius.getCharAt(i * 2 + 1) |
| 110 | ) |
| 111 | |
| 112 | // Compose key number |
| 113 | keyIndex = MathUtil.mod(i, keyLength) * 2 |
| 114 | keyValue = parseInt( |
| 115 | keyPolybius.getCharAt(keyIndex) + |
| 116 | keyPolybius.getCharAt(keyIndex + 1) |
| 117 | ) |
| 118 | |
| 119 | // Add plaintext and key values together |
| 120 | values[i] = plaintextValue + keyValue |
| 121 | } |
| 122 | |
| 123 | return Chain.join(values, separator) |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Performs decode on given content. |