View Javadoc

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.security.Key;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.conf.Configurable;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.util.MD5Hash;
27  
28  import com.google.common.base.Preconditions;
29  
30  /**
31   * Crypto context. Encapsulates an encryption algorithm and its key material.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public class Context implements Configurable {
36    private Configuration conf;
37    private Cipher cipher;
38    private Key key;
39    private String keyHash;
40  
41    Context(Configuration conf) {
42      this.conf = conf;
43    }
44  
45    Context() {
46      this(HBaseConfiguration.create());
47    }
48  
49    @Override
50    public Configuration getConf() {
51      return conf;
52    }
53  
54    @Override
55    public void setConf(Configuration conf) {
56      this.conf = conf;
57    }
58  
59    @Override
60    public String toString() {
61      return "cipher=" + (cipher != null ? cipher.getName() : "NONE")
62          + " keyHash=" + (keyHash != null ? keyHash.substring(0, 8) + "..." : "NONE");
63    }
64  
65    public Cipher getCipher() {
66      return cipher;
67    }
68  
69    public Context setCipher(Cipher cipher) {
70      this.cipher = cipher;
71      return this;
72    }
73  
74    public byte[] getKeyBytes() {
75      return key.getEncoded();
76    }
77  
78    public String getKeyBytesHash() {
79      return keyHash;
80    }
81  
82    public String getKeyFormat() {
83      return key.getFormat();
84    }
85  
86    public Key getKey() {
87      return key;
88    }
89  
90    public Context setKey(Key key) {
91      Preconditions.checkNotNull(cipher, "Context does not have a cipher");
92      // validate the key length
93      byte[] encoded = key.getEncoded();
94      if (encoded.length != cipher.getKeyLength()) {
95        throw new RuntimeException("Illegal key length, have=" + encoded.length +
96          ", want=" + cipher.getKeyLength());
97      }
98      this.key = key;
99      this.keyHash = MD5Hash.getMD5AsHex(encoded);
100     return this;
101   }
102 }