1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with this 4 * work for additional information regarding copyright ownership. The ASF 5 * licenses this file to you under the Apache License, Version 2.0 (the 6 * "License"); you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 package org.apache.hadoop.hbase.io.crypto; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.security.Key; 23 24 import org.apache.hadoop.hbase.classification.InterfaceAudience; 25 import org.apache.hadoop.hbase.classification.InterfaceStability; 26 27 /** 28 * A common interface for a cryptographic algorithm. 29 */ 30 @InterfaceAudience.Public 31 @InterfaceStability.Evolving 32 public abstract class Cipher { 33 34 private final CipherProvider provider; 35 36 public Cipher(CipherProvider provider) { 37 this.provider = provider; 38 } 39 40 /** 41 * Return the provider for this Cipher 42 */ 43 public CipherProvider getProvider() { 44 return provider; 45 } 46 47 /** 48 * Return this Cipher's name 49 */ 50 public abstract String getName(); 51 52 /** 53 * Return the key length required by this cipher, in bytes 54 */ 55 public abstract int getKeyLength(); 56 57 /** 58 * Return the expected initialization vector length, in bytes, or 0 if not applicable 59 */ 60 public abstract int getIvLength(); 61 62 /** 63 * Create a random symmetric key 64 * @return the random symmetric key 65 */ 66 public abstract Key getRandomKey(); 67 68 /** 69 * Get an encryptor for encrypting data. 70 */ 71 public abstract Encryptor getEncryptor(); 72 73 /** 74 * Return a decryptor for decrypting data. 75 */ 76 public abstract Decryptor getDecryptor(); 77 78 /** 79 * Create an encrypting output stream given a context and IV 80 * @param out the output stream to wrap 81 * @param context the encryption context 82 * @param iv initialization vector 83 * @return the encrypting wrapper 84 * @throws IOException 85 */ 86 public abstract OutputStream createEncryptionStream(OutputStream out, Context context, 87 byte[] iv) 88 throws IOException; 89 90 /** 91 * Create an encrypting output stream given an initialized encryptor 92 * @param out the output stream to wrap 93 * @param encryptor the encryptor 94 * @return the encrypting wrapper 95 * @throws IOException 96 */ 97 public abstract OutputStream createEncryptionStream(OutputStream out, Encryptor encryptor) 98 throws IOException; 99 100 /** 101 * Create a decrypting input stream given a context and IV 102 * @param in the input stream to wrap 103 * @param context the encryption context 104 * @param iv initialization vector 105 * @return the decrypting wrapper 106 * @throws IOException 107 */ 108 public abstract InputStream createDecryptionStream(InputStream in, Context context, 109 byte[] iv) 110 throws IOException; 111 112 /** 113 * Create a decrypting output stream given an initialized decryptor 114 * @param in the input stream to wrap 115 * @param decryptor the decryptor 116 * @return the decrypting wrapper 117 * @throws IOException 118 */ 119 public abstract InputStream createDecryptionStream(InputStream in, Decryptor decryptor) 120 throws IOException; 121 122 }