| 128 | |
| 129 | // h/t Adam Paynter http://stackoverflow.com/users/41619/ |
| 130 | private static byte[] process(byte[] input, |
| 131 | BufferedBlockCipher bufferedBlockCipher, |
| 132 | CipherParameters cipherParameters, boolean forEncryption) |
| 133 | throws InvalidCipherTextException { |
| 134 | bufferedBlockCipher.init(forEncryption, cipherParameters); |
| 135 | |
| 136 | int inputOffset = 0; |
| 137 | int inputLength = input.length; |
| 138 | |
| 139 | int maximumOutputLength = bufferedBlockCipher |
| 140 | .getOutputSize(inputLength); |
| 141 | byte[] output = new byte[maximumOutputLength]; |
| 142 | int outputOffset = 0; |
| 143 | int outputLength = 0; |
| 144 | |
| 145 | int bytesProcessed; |
| 146 | |
| 147 | bytesProcessed = bufferedBlockCipher.processBytes(input, inputOffset, |
| 148 | inputLength, output, outputOffset); |
| 149 | outputOffset += bytesProcessed; |
| 150 | outputLength += bytesProcessed; |
| 151 | |
| 152 | bytesProcessed = bufferedBlockCipher.doFinal(output, outputOffset); |
| 153 | outputOffset += bytesProcessed; |
| 154 | outputLength += bytesProcessed; |
| 155 | |
| 156 | if (outputLength == output.length) { |
| 157 | return output; |
| 158 | } else { |
| 159 | byte[] truncatedOutput = new byte[outputLength]; |
| 160 | System.arraycopy(output, 0, truncatedOutput, 0, outputLength); |
| 161 | return truncatedOutput; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Computes hashcash proof-of-work stamp for the given input and |