001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.crypto.aes;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.security.Key;
023import java.util.Properties;
024import javax.crypto.spec.IvParameterSpec;
025import org.apache.commons.crypto.stream.CryptoInputStream;
026import org.apache.hadoop.hbase.io.crypto.Decryptor;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.yetus.audience.InterfaceStability;
029
030import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
031
032@InterfaceAudience.Private
033@InterfaceStability.Evolving
034public class CommonsCryptoAESDecryptor implements Decryptor {
035
036  private String cipherMode;
037  private Properties properties;
038  private Key key;
039  private byte[] iv;
040
041  public CommonsCryptoAESDecryptor(String cipherMode, Properties properties) {
042    this.cipherMode = cipherMode;
043    this.properties = properties;
044  }
045
046  @Override
047  public void setKey(Key key) {
048    Preconditions.checkNotNull(key, "Key cannot be null");
049    this.key = key;
050  }
051
052  @Override
053  public int getIvLength() {
054    return CommonsCryptoAES.IV_LENGTH;
055  }
056
057  @Override
058  public int getBlockSize() {
059    return CommonsCryptoAES.BLOCK_SIZE;
060  }
061
062  @Override
063  public void setIv(byte[] iv) {
064    Preconditions.checkNotNull(iv, "IV cannot be null");
065    Preconditions.checkArgument(iv.length == CommonsCryptoAES.IV_LENGTH, "Invalid IV length");
066    this.iv = iv;
067  }
068
069  @Override
070  public InputStream createDecryptionStream(InputStream in) {
071    try {
072      return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv));
073    } catch (IOException e) {
074      throw new RuntimeException(e);
075    }
076  }
077
078  @Override
079  public void reset() {
080  }
081}