// Path: src/main/java/com/healthmarketscience/jackcess/crypt/util/StreamCipherCompat.java
// public interface StreamCipherCompat 
// {
//   public String getAlgorithmName();
// 
//   public void init(boolean forEncryption, CipherParameters params);
// 
//   public byte returnByte(byte in);
// 
//   public int processStreamBytes(byte[] in, int inOff,
//                                 int len, byte[] out, int outOff);
// 
//   public void reset();
// }


import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherCompat;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.CodecHandler;
import com.healthmarketscience.jackcess.impl.PageChannel;
import com.healthmarketscience.jackcess.impl.TempBufferHolder;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.InvalidCipherTextException;
/*
Copyright (c) 2010 Vladimir Berezniker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.crypt.impl;





/**
 * Common CodecHandler support.
 *
 * @author Vladimir Berezniker
 */
public abstract class BaseCryptCodecHandler implements CodecHandler
{
  public static final boolean CIPHER_DECRYPT_MODE = false;
  public static final boolean CIPHER_ENCRYPT_MODE = true;

  private final PageChannel _channel;
  private final byte[] _encodingKey;
  private final KeyCache<CipherParameters> _paramCache =
    new KeyCache<CipherParameters>() {
      @Override protected CipherParameters computeKey(int pageNumber) {
        return computeCipherParams(pageNumber);
      }
    };
  private TempBufferHolder _tempBufH;

  protected BaseCryptCodecHandler(PageChannel channel, byte[] encodingKey) {
    _channel = channel;
    _encodingKey = encodingKey;
  }

  protected CipherParameters getCipherParams(int pageNumber) {
    return _paramCache.get(pageNumber);
  }

  protected byte[] getEncodingKey() {
    return _encodingKey;
  }

// Path: src/main/java/com/healthmarketscience/jackcess/crypt/util/StreamCipherCompat.java
// public interface StreamCipherCompat 
// {
//   public String getAlgorithmName();
// 
//   public void init(boolean forEncryption, CipherParameters params);
// 
//   public byte returnByte(byte in);
// 
//   public int processStreamBytes(byte[] in, int inOff,
//                                 int len, byte[] out, int outOff);
// 
//   public void reset();
// }


// Path: src/main/java/com/healthmarketscience/jackcess/crypt/impl/BaseCryptCodecHandler.java
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherCompat;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.CodecHandler;
import com.healthmarketscience.jackcess.impl.PageChannel;
import com.healthmarketscience.jackcess.impl.TempBufferHolder;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.InvalidCipherTextException;

/*
Copyright (c) 2010 Vladimir Berezniker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.crypt.impl;





/**
 * Common CodecHandler support.
 *
 * @author Vladimir Berezniker
 */
public abstract class BaseCryptCodecHandler implements CodecHandler
{
  public static final boolean CIPHER_DECRYPT_MODE = false;
  public static final boolean CIPHER_ENCRYPT_MODE = true;

  private final PageChannel _channel;
  private final byte[] _encodingKey;
  private final KeyCache<CipherParameters> _paramCache =
    new KeyCache<CipherParameters>() {
      @Override protected CipherParameters computeKey(int pageNumber) {
        return computeCipherParams(pageNumber);
      }
    };
  private TempBufferHolder _tempBufH;

  protected BaseCryptCodecHandler(PageChannel channel, byte[] encodingKey) {
    _channel = channel;
    _encodingKey = encodingKey;
  }

  protected CipherParameters getCipherParams(int pageNumber) {
    return _paramCache.get(pageNumber);
  }

  protected byte[] getEncodingKey() {
    return _encodingKey;
  }

  protected StreamCipherCompat getStreamCipher() {