// 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/util/StreamCipherFactory.java
// public abstract class StreamCipherFactory
// {
//   /** compatible factory for RC4Engine instances */
//   private static final StreamCipherFactory RC4_ENGINE_FACTORY;
//   static {
//     StreamCipherFactory factory = null;
//     try {
//       // first, attempt to load a 1.51+ compatible factory instance
//       factory = loadFactory("com.healthmarketscience.jackcess.crypt.util.RC4EngineCompat$Factory");
//     } catch(Throwable t) {
//       // failed, try legacy version
//     }
// 
//     if(factory == null) {
//       try {
//         // now, attempt to load a 1.50 and earlier compatible factory instance
//         factory = loadFactory("com.healthmarketscience.jackcess.crypt.util.RC4EngineLegacy$Factory");
//       } catch(Throwable e) {
//         // sorry, no dice
//         throw new IllegalStateException("Incompatible bouncycastle version", e);
//       }
//     }
// 
//     RC4_ENGINE_FACTORY = factory;
//   }
// 
//   protected StreamCipherFactory() {}
// 
//   public static StreamCipherCompat newRC4Engine() {
//     return RC4_ENGINE_FACTORY.newInstance();
//   }
// 
//   private static StreamCipherFactory loadFactory(String className)
//     throws Exception
//   {
//     Class<?> factoryClass = Class.forName(className);
//     StreamCipherFactory factory = (StreamCipherFactory)factoryClass.newInstance();
//     // verify that the engine is functional
//     if(factory.newInstance() == null) {
//       throw new IllegalStateException("EngineFactory " + className +
//                                       " not functional");
//     }
//     return factory;
//   }
// 
//   public abstract StreamCipherCompat newInstance();
// }


import java.nio.ByteBuffer;
import java.util.Arrays;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherCompat;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherFactory;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.PageChannel;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.params.KeyParameter;
/*
Copyright (c) 2013 James Ahlborn

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.office;



/**
 *
 * @author James Ahlborn
 */
public class OfficeBinaryDocRC4Provider extends StreamCipherProvider
{
  private final byte[] _encVerifier = new byte[16];
  private final byte[] _encVerifierHash = new byte[16];
  private final byte[] _baseHash;

  public OfficeBinaryDocRC4Provider(PageChannel channel, byte[] encodingKey,
                                    ByteBuffer encProvBuf, byte[] pwdBytes)
  {
    super(channel, encodingKey);

    // OC: 2.3.6.1
    byte[] salt = new byte[16];
    encProvBuf.get(salt);
    encProvBuf.get(_encVerifier);
    encProvBuf.get(_encVerifierHash);

    // OC: 2.3.6.2 (Part 1)
    byte[] fillHash = ByteUtil.concat(hash(getDigest(), pwdBytes, 5), salt);
    byte[] intBuf = new byte[336];
    for(int i = 0; i < intBuf.length; i += fillHash.length) {
      System.arraycopy(fillHash, 0, intBuf, i, fillHash.length);
    }

    _baseHash = hash(getDigest(), intBuf, 5);
  }

  @Override
  public boolean canEncodePartialPage() {
    // RC4 ciphers are not influenced by the page contents, so we can easily
    // encode part of the buffer.
    return true;
  }

  @Override
  protected Digest initDigest() {
    return new MD5Digest();
  }

  @Override
// 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/util/StreamCipherFactory.java
// public abstract class StreamCipherFactory
// {
//   /** compatible factory for RC4Engine instances */
//   private static final StreamCipherFactory RC4_ENGINE_FACTORY;
//   static {
//     StreamCipherFactory factory = null;
//     try {
//       // first, attempt to load a 1.51+ compatible factory instance
//       factory = loadFactory("com.healthmarketscience.jackcess.crypt.util.RC4EngineCompat$Factory");
//     } catch(Throwable t) {
//       // failed, try legacy version
//     }
// 
//     if(factory == null) {
//       try {
//         // now, attempt to load a 1.50 and earlier compatible factory instance
//         factory = loadFactory("com.healthmarketscience.jackcess.crypt.util.RC4EngineLegacy$Factory");
//       } catch(Throwable e) {
//         // sorry, no dice
//         throw new IllegalStateException("Incompatible bouncycastle version", e);
//       }
//     }
// 
//     RC4_ENGINE_FACTORY = factory;
//   }
// 
//   protected StreamCipherFactory() {}
// 
//   public static StreamCipherCompat newRC4Engine() {
//     return RC4_ENGINE_FACTORY.newInstance();
//   }
// 
//   private static StreamCipherFactory loadFactory(String className)
//     throws Exception
//   {
//     Class<?> factoryClass = Class.forName(className);
//     StreamCipherFactory factory = (StreamCipherFactory)factoryClass.newInstance();
//     // verify that the engine is functional
//     if(factory.newInstance() == null) {
//       throw new IllegalStateException("EngineFactory " + className +
//                                       " not functional");
//     }
//     return factory;
//   }
// 
//   public abstract StreamCipherCompat newInstance();
// }


// Path: src/main/java/com/healthmarketscience/jackcess/crypt/impl/office/OfficeBinaryDocRC4Provider.java
import java.nio.ByteBuffer;
import java.util.Arrays;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherCompat;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherFactory;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import com.healthmarketscience.jackcess.impl.PageChannel;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.params.KeyParameter;

/*
Copyright (c) 2013 James Ahlborn

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.office;



/**
 *
 * @author James Ahlborn
 */
public class OfficeBinaryDocRC4Provider extends StreamCipherProvider
{
  private final byte[] _encVerifier = new byte[16];
  private final byte[] _encVerifierHash = new byte[16];
  private final byte[] _baseHash;

  public OfficeBinaryDocRC4Provider(PageChannel channel, byte[] encodingKey,
                                    ByteBuffer encProvBuf, byte[] pwdBytes)
  {
    super(channel, encodingKey);

    // OC: 2.3.6.1
    byte[] salt = new byte[16];
    encProvBuf.get(salt);
    encProvBuf.get(_encVerifier);
    encProvBuf.get(_encVerifierHash);

    // OC: 2.3.6.2 (Part 1)
    byte[] fillHash = ByteUtil.concat(hash(getDigest(), pwdBytes, 5), salt);
    byte[] intBuf = new byte[336];
    for(int i = 0; i < intBuf.length; i += fillHash.length) {
      System.arraycopy(fillHash, 0, intBuf, i, fillHash.length);
    }

    _baseHash = hash(getDigest(), intBuf, 5);
  }

  @Override
  public boolean canEncodePartialPage() {
    // RC4 ciphers are not influenced by the page contents, so we can easily
    // encode part of the buffer.
    return true;
  }

  @Override
  protected Digest initDigest() {
    return new MD5Digest();
  }

  @Override
  protected StreamCipherCompat initCipher() {