1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Unstable
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
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 }