Debian Codegraph @

owner-site/site/docs/crypto.md

raw symbols SCIP
1 ---
2 layout: docs
3 title: Crypto support
4 prev_section: singleton
5 next_section: features
6 permalink: /docs/crypto/
7 ---
8
9 What is this feature?
10 ---------------------
11
12 This is an experimental feature adding crypto support to OWNER.
13
14 With Crypto it is possible to declare, with a simple annotation, that a property contains an encrypted value
15 ( a value which has to be decrypted ). A `@DecryptorClass` can be specified for a class or for each property.
16 `@EncryptedValue(@DecryptorClass)` overrides a `@DecryptorClass` specified at class level.
17
18
19 Which crypto frameworks are supported?
20 --------------------------------------
21
22 Crypto support allows the use of any framework to decrypt values. You must supply a class
23 implementing the `Decryptor` interface, where you can use any framework you want in order to decrypt values.
24
25
26 How can I use it?
27 -----------------
28
29 Suppose you will use the same `@DecryptorClass` to decrypt all values in your configuration:
30
31 ```java
32 @DecryptorClass( MyDecryptor1.class )
33 public interface Sample extends Config {
34
35 @EncryptedValue
36 public String myEncryptedPassword1();
37
38 @EncryptedValue
39 public String myEncryptedPassword2();
40 }
41 ```
42
43 And now suppose that you will use different `@DecryptorClass` for the previous properties:
44
45 ```java
46 public interface Sample extends Config {
47
48 @EncryptedValue( MyDecryptor1.class )
49 public String myEncryptedPassword1();
50
51 @EncryptedValue( MyDecryptor2.class )
52 public String myEncryptedPassword2();
53 }
54 ```
55
56 Or if you plan to use the same `@DecryptorClass` for all `@EncryptedValue` properties except `myEncryptedPassword1`:
57
58 ```java
59 @DecryptorClass( MyDecryptor2.class )
60 public interface Sample extends Config {
61
62 @EncryptedValue( MyDecryptor1.class )
63 public String myEncryptedPassword1();
64
65 @EncryptedValue
66 public String myEncryptedPassword2();
67
68 @EncryptedValue
69 public String myEncryptedPassword3();
70 }
71 ```
72
73
74 It works with other annotations...
75 ----------------------------------
76
77 ... so you can write code like this:
78
79 ```java
80 @Key("crypto.list")
81 @EncryptedValue
82 @Separator(",")
83 @DefaultValue("Pfzoiet5E5zN2/7tfgrGLQ==")
84 List<String> cryptoList();
85 ```
86
87
88 Can you show me an example implementation of Decryptor?
89 -------------------------------------------------------
90
91 This is the source code of `IdentityDecryptor.java`, a no-op Decryptor returning the same value received for decrypting:
92
93 ```java
94 package org.aeonbits.owner.crypto;
95
96 public final class IdentityDecryptor
97 extends AbstractDecryptor {
98 @Override
99 public String decrypt( String value ) {
100 return value;
101 }
102 }
103 ```
104
105 It extends `AbstractDecryptor`, an abstract class that already implements the `Decrypt` interface and one of its
106 methods. To get our Decryptor working, we just have to implement the other method, `decrypt( String value )` .
107
108 Another example is `StandardDecryptor.java`, which uses the `javax.crypto` features available in JDK.
109
110 ```java
111 package org.aeonbits.owner.crypto;
112
113 import java.io.UnsupportedEncodingException;
114 import java.security.Key;
115 import java.security.MessageDigest;
116 import java.security.NoSuchAlgorithmException;
117 import java.util.Arrays;
118
119 import javax.crypto.Cipher;
120 import javax.crypto.spec.SecretKeySpec;
121
122 import javax.xml.bind.DatatypeConverter;
123
124
125 public class StandardEncryptor extends AbstractEncryptor {
126 private final String algorithm;
127 private final String encoding;
128 private final byte[] secretKey;
129 private final int secretKeySize;
130
131 public StandardEncryptor( String algorithm, String secretKey, String encoding, int secretKeySize ) {
132 try {
133 this.secretKeySize = secretKeySize;
134 this.algorithm = algorithm;
135 this.encoding = encoding;
136 this.secretKey = secretKey.getBytes( encoding );
137 } catch (UnsupportedEncodingException cause) {
138 throw new IllegalArgumentException( cause.getMessage(), cause);
139 }
140 }
141
142 public String getAlgorithm() {
143 return this.algorithm;
144 }
145
146
147 public String encrypt( String plainData ) {
148 try {
149 Key key = generateKey();
150 Cipher c = Cipher.getInstance( this.algorithm );
151 c.init(Cipher.ENCRYPT_MODE, key);
152 byte[] encVal = c.doFinal( plainData.getBytes( this.encoding ) );
153 String encryptedValue = DatatypeConverter.printBase64Binary( encVal );
154 return encryptedValue;
155 } catch ( Exception cause ) {
156 throw new IllegalArgumentException( cause.getMessage(), cause );
157 }
158 }
159
160 public String decrypt(String encryptedData) throws IllegalArgumentException {
161 try {
162 Key key = generateKey();
163 Cipher c = Cipher.getInstance( this.algorithm );
164 c.init(Cipher.DECRYPT_MODE, key);
165 byte[] decodedValue = DatatypeConverter.parseBase64Binary( encryptedData );
166 byte[] decValue = c.doFinal(decodedValue);
167 String decryptedValue = new String(decValue, this.encoding );
168 return decryptedValue;
169 } catch ( Exception cause ){
170 throw new IllegalArgumentException( cause.getMessage(), cause );
171 }
172 }
173
174 private Key generateKey() throws Exception {
175 return new SecretKeySpec( this.secretKey, this.getAlgorithm() );
176 }
177
178 public static final StandardEncryptor newInstance( String algorithm, String secretKey ) {
179 return newInstance( algorithm, secretKey, "UTF-8", secretKey.length() );
180 }
181
182 public static final StandardEncryptor newInstance( String algorithm, String secretKey, String encoding, int secretKeySize ) {
183 return new StandardEncryptor( algorithm, secretKey, encoding, secretKeySize );
184 }
185 }
186 ```