<commit_before>/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * 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.google.k2crypto.keyversions;

import com.google.k2crypto.exceptions.BuilderException;
import com.google.k2crypto.exceptions.EncryptionException;

import java.util.Arrays;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * This class represents a hash key version in K2. It is abstract and extended by specific hash key
 * implementations such as HMACKeyVersion
 *
 * @author <NAME> (<EMAIL>)
 */

public class HMACKeyVersion extends HashKeyVersion {

  /**
   * The key length in bytes (512 bits / 8 = 64 for SHA1 or MD5)
   */
  private int keyVersionLengthInBytes = 64;

  /**
   * The actual key matter of the HMAC key version. 
   */
  protected byte[] keyVersionMatter = new byte[keyVersionLengthInBytes];

  /**
   * SecretKey object representing the key matter in the HMAC key version
   */
  private SecretKey secretKey;

  /**
   * Private constructor to ensure people use generateSHA1HMAC or generateMD5HMAC to generate HMAC
   * key
   */
  private HMACKeyVersion() {
    // Do not put any code here
  }

  /**
   * String constants representing all supported hash algorithms.
   */
  public static final String HMAC_MD5 = "HmacMD5";
  public static final String HMAC_SHA1 = "HmacSHA1";
  public static final String HMAC_SHA256 = "HmacSHA256";
  public static final String HMAC_SHA384 = "HmacSHA384";
  public static final String HMAC_SHA512 = "HmacSHA512";

  /**
   * Hash algorithm for this HMAC key version
   */
  private String algorithm = HMAC_SHA1;

  /**
   * Generates a new HMAC using the SHA1 hash algorithm
   *
   * @return a new HMACKeyVersion using the SHA1 hash algorithm
   * @throws BuilderException
   */
  public static HMACKeyVersion generateHMAC(String hashAlgorithm) throws BuilderException {
    try {
      HMACKeyVersion hmac = new HMACKeyVersion();
      // Generate a key for the HMAC-SHA1 keyed-hashing algorithm
      KeyGenerator keyGen = KeyGenerator.getInstance(hashAlgorithm);
      hmac.secretKey = keyGen.generateKey();
      // save the byte array of the secret key
      hmac.keyVersionMatter = hmac.secretKey.getEncoded();
      return hmac;
    } catch (Exception e) {
      // throw builder exception if could not build key
      throw new BuilderException("Failed to build HMACKeyVersion", e);
    }
  }

  /**
   * Generates a new HMAC using the SHA1 hash algorithm from give keyversion matter
   *
   * @param keyVersionMatter The byte array representation of the HMAC key version
   * @return an HMACKeyVersion object representing the HMAC key based on the input key version
   *         matter
   * @throws BuilderException
   */
  public static HMACKeyVersion generateHMAC(String hashAlgorithm, byte[] keyVersionMatter)
      throws BuilderException {
    try {
      HMACKeyVersion hmac = new HMACKeyVersion();
      // save the byte array of the secret key
      hmac.keyVersionMatter = keyVersionMatter;
      // set the secret key based on the raw key matter
      hmac.secretKey =
          new SecretKeySpec(keyVersionMatter, 0, keyVersionMatter.length, hashAlgorithm);
      return hmac;
    } catch (Exception e) {
      // throw builder exception if could not build key
      throw new BuilderException("Failed to build HMACKeyVersion", e);
    }
  }

  /**
   * Public method to get the byte array of the HMAC key version matter
   *
   * @return The byte array representation of the HMAC key version matter
   */
  public byte[] getKeyVersionMatter() {
    return this.keyVersionMatter;
  }

  /**
   * Method to compute the raw HMAC on a piece of input data
   *<commit_msg>removed keyversionmatter byte array from HMACKeyVersion according to Cory 's suggestion<commit_after>/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * 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.google.k2crypto.keyversions;

import com.google.k2crypto.exceptions.BuilderException;
import com.google.k2crypto.exceptions.EncryptionException;

import java.util.Arrays;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * This class represents a hash key version in K2. It is abstract and extended by specific hash key
 * implementations such as HMACKeyVersion
 *
 * @author <NAME> (<EMAIL>)
 */

public class HMACKeyVersion extends HashKeyVersion {
  /**
   * SecretKey object representing the key matter in the HMAC key version
   */
  private SecretKey secretKey;

  /**
   * Private constructor to ensure people use generateSHA1HMAC or generateMD5HMAC to generate HMAC
   * key
   */
  private HMACKeyVersion() {
    // Do not put any code here
  }

  /**
   * String constants representing all supported hash algorithms.
   */
  public static final String HMAC_MD5 = "HmacMD5";
  public static final String HMAC_SHA1 = "HmacSHA1";
  public static final String HMAC_SHA256 = "HmacSHA256";
  public static final String HMAC_SHA384 = "HmacSHA384";
  public static final String HMAC_SHA512 = "HmacSHA512";

  /**
   * Hash algorithm for this HMAC key version
   */
  private String algorithm = HMAC_SHA1;

  /**
   * Generates a new HMAC using the SHA1 hash algorithm
   *
   * @return a new HMACKeyVersion using the SHA1 hash algorithm
   * @throws BuilderException
   */
  public static HMACKeyVersion generateHMAC(String hashAlgorithm) throws BuilderException {
    try {
      HMACKeyVersion hmac = new HMACKeyVersion();
      // Generate a key for the HMAC-SHA1 keyed-hashing algorithm
      KeyGenerator keyGen = KeyGenerator.getInstance(hashAlgorithm);
      hmac.secretKey = keyGen.generateKey();
      return hmac;
    } catch (Exception e) {
      // throw builder exception if could not build key
      throw new BuilderException("Failed to build HMACKeyVersion", e);
    }
  }

  /**
   * Generates a new HMAC using the SHA1 hash algorithm from give keyversion matter
   *
   * @param keyVersionMatter The byte array representation of the HMAC key version
   * @return an HMACKeyVersion object representing the HMAC key based on the input key version
   *         matter
   * @throws BuilderException
   */
  public static HMACKeyVersion generateHMAC(String hashAlgorithm, byte[] keyVersionMatter)
      throws BuilderException {
    try {
      HMACKeyVersion hmac = new HMACKeyVersion();
      // set the secret key based on the raw key matter
      hmac.secretKey =
          new SecretKeySpec(keyVersionMatter, 0, keyVersionMatter.length, hashAlgorithm);
      return hmac;
    } catch (Exception e) {
      // throw builder exception if could not build key
      throw new BuilderException("Failed to build HMACKeyVersion", e);
    }
  }

  /**
   * Public method to get the byte array of the HMAC key version matter
   *
   * @return The byte array representation of the HMAC key version matter
   */
  public byte[] getKeyVersionMatter() {
    return this.secretKey.getEncoded();
  }

  /**
   * Method to compute the raw HMAC on a piece of input data
   *
{'fraction_non_alphanumeric': 0.07127959759538707, 'fraction_numerical': 0.011532327321801006, 'mean_word_length': 3.3710455764075067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6542310', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2205, 'n_words': 885}