/*
 * Copyright 2010 Srikanth Reddy Lingala
 *
 * 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 net.lingala.zip4j.crypto.engine;

import static net.lingala.zip4j.util.Zip4jUtil.convertCharArrayToByteArray;

public class ZipCryptoEngine {

  private final int[] keys = new int[3];
  private static final int[] CRC_TABLE = new int[256];

  static {
    for (int i = 0; i < 256; i++) {
      int r = i;
      for (int j = 0; j < 8; j++) {
        if ((r & 1) == 1) {
          r = (r >>> 1) ^ 0xedb88320;
        } else {
          r >>>= 1;
        }
      }
      CRC_TABLE[i] = r;
    }
  }

  public void initKeys(char[] password, boolean useUtf8ForPassword) {
    keys[0] = 305419896;
    keys[1] = 591751049;
    keys[2] = 878082192;
    byte[] bytes = convertCharArrayToByteArray(password, useUtf8ForPassword);
    for (byte b : bytes) {
      updateKeys((byte) (b & 0xff));
    }
  }

  public void updateKeys(byte charAt) {
    keys[0] = crc32(keys[0], charAt);
    keys[1] += keys[0] & 0xff;
    keys[1] = keys[1] * 134775813 + 1;
    keys[2] = crc32(keys[2], (byte) (keys[1] >> 24));
  }

  private int crc32(int oldCrc, byte charAt) {
    return ((oldCrc >>> 8) ^ CRC_TABLE[(oldCrc ^ charAt) & 0xff]);
  }

  public byte decryptByte() {
    int temp = keys[2] | 2;
    return (byte) ((temp * (temp ^ 1)) >>> 8);
  }
}
