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; 025 026import org.apache.commons.crypto.stream.CryptoInputStream; 027import org.apache.hadoop.hbase.io.crypto.Decryptor; 028import org.apache.yetus.audience.InterfaceAudience; 029import org.apache.yetus.audience.InterfaceStability; 030 031import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 032 033@InterfaceAudience.Private 034@InterfaceStability.Evolving 035public class CommonsCryptoAESDecryptor implements Decryptor { 036 037 private String cipherMode; 038 private Properties properties; 039 private Key key; 040 private byte[] iv; 041 042 public CommonsCryptoAESDecryptor(String cipherMode, Properties properties) { 043 this.cipherMode = cipherMode; 044 this.properties = properties; 045 } 046 047 @Override 048 public void setKey(Key key) { 049 Preconditions.checkNotNull(key, "Key cannot be null"); 050 this.key = key; 051 } 052 053 @Override 054 public int getIvLength() { 055 return CommonsCryptoAES.IV_LENGTH; 056 } 057 058 @Override 059 public int getBlockSize() { 060 return CommonsCryptoAES.BLOCK_SIZE; 061 } 062 063 @Override 064 public void setIv(byte[] iv) { 065 Preconditions.checkNotNull(iv, "IV cannot be null"); 066 Preconditions.checkArgument(iv.length == CommonsCryptoAES.IV_LENGTH, "Invalid IV length"); 067 this.iv = iv; 068 } 069 070 @Override 071 public InputStream createDecryptionStream(InputStream in) { 072 try { 073 return new CryptoInputStream(cipherMode, properties, in, key, new 074 IvParameterSpec(iv)); 075 } catch (IOException e) { 076 throw new RuntimeException(e); 077 } 078 } 079 080 @Override 081 public void reset() { 082 } 083}