<commit_before>/*    Copyright 2014 <NAME>
 *
 * 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 org.cryptonode.jncryptor;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;
import java.io.PushbackInputStream;
import java.security.GeneralSecurityException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.io.IOUtils;

/**
 * Reads RNCryptor-format data in a stream fashion. This class only
 * supports the v3 data format.
 * 
 * @since 1.1.0
 */
public class AES256JNCryptorInputStream extends InputStream {

  private static final int END_OF_STREAM = -1;
  private final boolean isPasswordEncrypted;
  private final InputStream in;

  private char[] password;
  private SecretKey decryptionKey;
  private SecretKey hmacKey;
  private boolean endOfStreamHandled = false;

  private PushbackInputStream pushbackInputStream;
  private TrailerInputStream trailerIn;
  private Mac mac;

  /**
   * Creates an input stream for password-encrypted data.
   * 
   * @param in
   *          the {@code InputStream} to read
   * @param password
   *          the password
   */
  AES256JNCryptorInputStream(InputStream in, char[] password) {
    isPasswordEncrypted = true;
    this.password = password;
    this.in = in;
  }

  /**
   * Creates an input stream for key-encrypted data.
   * 
   * @param in
   *          the {@code InputStream} to read
   * @param decryptionKey
   *          the key to decrypt with
   * @param hmacKey
   *          the key to calculate the HMAC with
   */
  AES256JNCryptorInputStream(InputStream in, SecretKey decryptionKey,
      SecretKey hmacKey) {
    isPasswordEncrypted = false;
    this.decryptionKey = decryptionKey;
    this.hmacKey = hmacKey;
    this.in = in;
  }

  /**
   * Mark and reset methods are not supported in this input stream.
   * 
   * @return <code>false</code>
   */
  @Override
  public boolean markSupported() {
    return false;
  }

  /**
   * Reads the header data, derives keys if necessary and creates the input
   * streams.
   * 
   * @throws IOException<commit_msg>Constructors should be public.<commit_after>/*    Copyright 2014 <NAME>
 *
 * 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 org.cryptonode.jncryptor;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;
import java.io.PushbackInputStream;
import java.security.GeneralSecurityException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.io.IOUtils;

/**
 * Reads RNCryptor-format data in a stream fashion. This class only
 * supports the v3 data format.
 * 
 * @since 1.1.0
 */
public class AES256JNCryptorInputStream extends InputStream {

  private static final int END_OF_STREAM = -1;
  private final boolean isPasswordEncrypted;
  private final InputStream in;

  private char[] password;
  private SecretKey decryptionKey;
  private SecretKey hmacKey;
  private boolean endOfStreamHandled = false;

  private PushbackInputStream pushbackInputStream;
  private TrailerInputStream trailerIn;
  private Mac mac;

  /**
   * Creates an input stream for password-encrypted data.
   * 
   * @param in
   *          the {@code InputStream} to read
   * @param password
   *          the password
   */
  public AES256JNCryptorInputStream(InputStream in, char[] password) {
    isPasswordEncrypted = true;
    this.password = password;
    this.in = in;
  }

  /**
   * Creates an input stream for key-encrypted data.
   * 
   * @param in
   *          the {@code InputStream} to read
   * @param decryptionKey
   *          the key to decrypt with
   * @param hmacKey
   *          the key to calculate the HMAC with
   */
  public AES256JNCryptorInputStream(InputStream in, SecretKey decryptionKey,
      SecretKey hmacKey) {
    isPasswordEncrypted = false;
    this.decryptionKey = decryptionKey;
    this.hmacKey = hmacKey;
    this.in = in;
  }

  /**
   * Mark and reset methods are not supported in this input stream.
   * 
   * @return <code>false</code>
   */
  @Override
  public boolean markSupported() {
    return false;
  }

  /**
   * Reads the header data, derives keys if necessary and creates the input
   * streams.
   * 
   * @throws IOException
{'fraction_non_alphanumeric': 0.07983568494373995, 'fraction_numerical': 0.007858546168958742, 'mean_word_length': 3.311008468052348, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5218652', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1574, 'n_words': 552}