On Wed, 5 Jun 2024 14:21:57 GMT, Martin Balao <[email protected]> wrote:
>> What I like about this suggestion is that it allows unifying the repeated
>> logic: the two blocks inside `if (encrypt)` and the corresponding `else`
>> would become almost identical, allowing an additional abstraction. How about
>> the following?
>>
>>
>> private void convertCTSVariant(ByteBuffer ciphertextBuf,
>> byte[] ciphertextArr, int end) {
>> // [...]
>> // [...] omitted code
>> // [...]
>> if (ciphertextBuf != null) {
>> pad = pad == 0 ? blockSize : pad;
>> if (encrypt) {
>> // .... pp[pp] ffff -> .... ffff pp[pp]
>> swapLastTwoBlocks(ciphertextBuf, end, pad, blockSize);
>> } else {
>> // .... ffff pp[pp] -> .... pp[pp] ffff
>> swapLastTwoBlocks(ciphertextBuf, end, blockSize, pad);
>> }
>> }
>> }
>>
>> private static void swapLastTwoBlocks(ByteBuffer buffer, int end,
>> int prevBlockLen, int lastBlockLen) {
>> // .... prevBlock lastBlock -> .... lastBlock prevBlock
>> int prevBlockStart = end - prevBlockLen - lastBlockLen;
>> byte[] prevBlockBackup = new byte[prevBlockLen];
>> buffer.get(prevBlockStart, prevBlockBackup);
>> buffer.put(prevBlockStart, buffer, end - lastBlockLen, lastBlockLen);
>> buffer.put(end - prevBlockLen, prevBlockBackup);
>> }
>
> Looks good to me.
Yes, I prefer this.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/18898#discussion_r1628246471