33
44package com .azure .security .keyvault .keys .cryptography ;
55
6- import com .azure .core .credential .TokenCredential ;
76import com .azure .core .http .HttpClient ;
87import com .azure .core .http .HttpPipeline ;
98import com .azure .core .http .HttpPipelineBuilder ;
1211import com .azure .core .http .policy .RetryPolicy ;
1312import com .azure .identity .DefaultAzureCredentialBuilder ;
1413import com .azure .security .keyvault .keys .KeyAsyncClient ;
15- import com .azure .security .keyvault .keys .implementation .KeyVaultCredentialPolicy ;
1614import com .azure .security .keyvault .keys .cryptography .models .EncryptionAlgorithm ;
1715import com .azure .security .keyvault .keys .cryptography .models .KeyWrapAlgorithm ;
1816import com .azure .security .keyvault .keys .cryptography .models .SignatureAlgorithm ;
17+ import com .azure .security .keyvault .keys .implementation .KeyVaultCredentialPolicy ;
1918import com .azure .security .keyvault .keys .models .JsonWebKey ;
2019
2120import java .security .MessageDigest ;
2625 * This class contains code samples for generating javadocs through doclets for {@link KeyAsyncClient}.
2726 */
2827public final class CryptographyAsyncClientJavaDocCodeSnippets {
29- private String key1 = "key1" ;
30- private String key2 = "key2" ;
31- private String value1 = "val1" ;
32- private String value2 = "val2" ;
28+ private final String key1 = "key1" ;
29+ private final String key2 = "key2" ;
30+ private final String value1 = "val1" ;
31+ private final String value2 = "val2" ;
3332
3433 /**
3534 * Generates code sample for creating a {@link KeyAsyncClient}.
@@ -39,10 +38,11 @@ public final class CryptographyAsyncClientJavaDocCodeSnippets {
3938 public CryptographyAsyncClient createAsyncClient () {
4039 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.instantiation
4140 CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder ()
42- .keyIdentifier ("<YOUR-KEY-ID >" )
41+ .keyIdentifier ("<your-key-id >" )
4342 .credential (new DefaultAzureCredentialBuilder ().build ())
4443 .buildAsyncClient ();
4544 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.instantiation
45+
4646 return cryptographyAsyncClient ;
4747 }
4848
@@ -54,11 +54,11 @@ public CryptographyAsyncClient createAsyncClient() {
5454 public CryptographyAsyncClient createAsyncClientWithJsonWebKey () {
5555 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.withJsonWebKey.instantiation
5656 JsonWebKey jsonWebKey = new JsonWebKey ().setId ("SampleJsonWebKey" );
57-
5857 CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder ()
5958 .jsonWebKey (jsonWebKey )
6059 .buildAsyncClient ();
6160 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.withJsonWebKey.instantiation
61+
6262 return cryptographyAsyncClient ;
6363 }
6464
@@ -70,12 +70,13 @@ public CryptographyAsyncClient createAsyncClientWithJsonWebKey() {
7070 public CryptographyAsyncClient createAsyncClientWithHttpClient () {
7171 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.withHttpClient.instantiation
7272 CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder ()
73- .keyIdentifier ("<Your-Key-ID >" )
73+ .keyIdentifier ("<your-key-id >" )
7474 .httpLogOptions (new HttpLogOptions ().setLogLevel (HttpLogDetailLevel .BODY_AND_HEADERS ))
7575 .addPolicy (new KeyVaultCredentialPolicy (new DefaultAzureCredentialBuilder ().build ()))
7676 .httpClient (HttpClient .createDefault ())
7777 .buildAsyncClient ();
7878 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.withHttpClient.instantiation
79+
7980 return cryptographyAsyncClient ;
8081 }
8182
@@ -91,9 +92,10 @@ public CryptographyAsyncClient createAsyncClientWithPipeline() {
9192 .build ();
9293 CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder ()
9394 .pipeline (pipeline )
94- .keyIdentifier ("<YOUR-KEY-ID " )
95+ .keyIdentifier ("<your-key-id> " )
9596 .buildAsyncClient ();
9697 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.withPipeline.instantiation
98+
9799 return cryptographyAsyncClient ;
98100 }
99101
@@ -102,10 +104,12 @@ public CryptographyAsyncClient createAsyncClientWithPipeline() {
102104 */
103105 public void getKeySnippets () {
104106 CryptographyAsyncClient cryptographyAsyncClient = createAsyncClient ();
107+
105108 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.getKey
106109 cryptographyAsyncClient .getKey ()
107- .subscriberContext (reactor .util .context .Context .of (key1 , value1 , key2 , value2 ))
108- .subscribe (key -> System .out .printf ("Key is returned with name %s and id %s \n " , key .getName (), key .getId ()));
110+ .contextWrite (context -> context .put (key1 , value1 ).put (key2 , value2 ))
111+ .subscribe (key -> System .out .printf ("Key returned with name: %s, and id: %s.\n " , key .getName (),
112+ key .getId ()));
109113 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.getKey
110114 }
111115
@@ -114,10 +118,11 @@ public void getKeySnippets() {
114118 */
115119 public void getKeyWithResponseSnippets () {
116120 CryptographyAsyncClient cryptographyAsyncClient = createAsyncClient ();
121+
117122 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.getKeyWithResponse
118123 cryptographyAsyncClient .getKeyWithResponse ()
119- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
120- .subscribe (keyResponse -> System .out .printf ("Key is returned with name %s and id %s \n " ,
124+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
125+ .subscribe (keyResponse -> System .out .printf ("Key returned with name: %s, and id: %s. \n " ,
121126 keyResponse .getValue ().getName (), keyResponse .getValue ().getId ()));
122127 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.getKeyWithResponse
123128 }
@@ -134,27 +139,26 @@ public void encrypt() {
134139 new Random (0x1234567L ).nextBytes (plaintext );
135140
136141 cryptographyAsyncClient .encrypt (EncryptionAlgorithm .RSA_OAEP , plaintext )
137- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
142+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
138143 .subscribe (encryptResult ->
139- System .out .printf ("Received encrypted content of length %d with algorithm %s \n " ,
144+ System .out .printf ("Received encrypted content of length: %d, with algorithm: %s. \n " ,
140145 encryptResult .getCipherText ().length , encryptResult .getAlgorithm ().toString ()));
141146 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.encrypt#EncryptionAlgorithm-byte
142147
143148 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.encrypt#EncryptParameters
144149 byte [] plaintextBytes = new byte [100 ];
145-
146150 new Random (0x1234567L ).nextBytes (plaintextBytes );
147-
148151 byte [] iv = {
149152 (byte ) 0x1a , (byte ) 0xf3 , (byte ) 0x8c , (byte ) 0x2d , (byte ) 0xc2 , (byte ) 0xb9 , (byte ) 0x6f , (byte ) 0xfd ,
150153 (byte ) 0xd8 , (byte ) 0x66 , (byte ) 0x94 , (byte ) 0x09 , (byte ) 0x23 , (byte ) 0x41 , (byte ) 0xbc , (byte ) 0x04
151154 };
155+
152156 EncryptParameters encryptParameters = EncryptParameters .createA128CbcParameters (plaintextBytes , iv );
153157
154158 cryptographyAsyncClient .encrypt (encryptParameters )
155- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
159+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
156160 .subscribe (encryptResult ->
157- System .out .printf ("Received encrypted content of length %d with algorithm %s \n " ,
161+ System .out .printf ("Received encrypted content of length: %d, with algorithm: %s. \n " ,
158162 encryptResult .getCipherText ().length , encryptResult .getAlgorithm ().toString ()));
159163 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.encrypt#EncryptParameters
160164 }
@@ -168,30 +172,28 @@ public void decrypt() {
168172
169173 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.decrypt#EncryptionAlgorithm-byte
170174 byte [] ciphertext = new byte [100 ];
171-
172175 new Random (0x1234567L ).nextBytes (ciphertext );
173176
174177 cryptographyAsyncClient .decrypt (EncryptionAlgorithm .RSA_OAEP , ciphertext )
175- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
178+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
176179 .subscribe (decryptResult ->
177- System .out .printf ("Received decrypted content of length %d\n " , decryptResult .getPlainText ().length ));
180+ System .out .printf ("Received decrypted content of length: %d\n " , decryptResult .getPlainText ().length ));
178181 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.decrypt#EncryptionAlgorithm-byte
179182
180183 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.decrypt#DecryptParameters
181184 byte [] ciphertextBytes = new byte [100 ];
182-
183185 new Random (0x1234567L ).nextBytes (ciphertextBytes );
184-
185186 byte [] iv = {
186187 (byte ) 0x1a , (byte ) 0xf3 , (byte ) 0x8c , (byte ) 0x2d , (byte ) 0xc2 , (byte ) 0xb9 , (byte ) 0x6f , (byte ) 0xfd ,
187188 (byte ) 0xd8 , (byte ) 0x66 , (byte ) 0x94 , (byte ) 0x09 , (byte ) 0x23 , (byte ) 0x41 , (byte ) 0xbc , (byte ) 0x04
188189 };
190+
189191 DecryptParameters decryptParameters = DecryptParameters .createA128CbcParameters (ciphertextBytes , iv );
190192
191193 cryptographyAsyncClient .decrypt (decryptParameters )
192- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
194+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
193195 .subscribe (decryptResult ->
194- System .out .printf ("Received decrypted content of length %d\n " , decryptResult .getPlainText ().length ));
196+ System .out .printf ("Received decrypted content of length: %d. \n " , decryptResult .getPlainText ().length ));
195197 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.decrypt#DecryptParameters
196198 }
197199
@@ -203,24 +205,35 @@ public void decrypt() {
203205 */
204206 public void signVerify () throws NoSuchAlgorithmException {
205207 CryptographyAsyncClient cryptographyAsyncClient = createAsyncClient ();
206- byte [] signature = new byte [ 100 ];
208+
207209 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.sign#SignatureAlgorithm-byte
208210 byte [] data = new byte [100 ];
209211 new Random (0x1234567L ).nextBytes (data );
210212 MessageDigest md = MessageDigest .getInstance ("SHA-256" );
211213 md .update (data );
212214 byte [] digest = md .digest ();
215+
213216 cryptographyAsyncClient .sign (SignatureAlgorithm .ES256 , digest )
214- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
217+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
215218 .subscribe (signResult ->
216- System .out .printf ("Received signature of length %d with algorithm %s" , signResult .getSignature ().length ));
219+ System .out .printf ("Received signature of length: %d, with algorithm: %s.\n " ,
220+ signResult .getSignature ().length , signResult .getAlgorithm ()));
217221 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.sign#SignatureAlgorithm-byte
218222
223+ byte [] signature = new byte [100 ];
224+
219225 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.verify#SignatureAlgorithm-byte-byte
220- cryptographyAsyncClient .verify (SignatureAlgorithm .ES256 , digest , signature )
221- .subscriberContext (reactor .util .context .Context .of (key1 , value1 , key2 , value2 ))
226+ byte [] myData = new byte [100 ];
227+ new Random (0x1234567L ).nextBytes (myData );
228+ MessageDigest messageDigest = MessageDigest .getInstance ("SHA-256" );
229+ messageDigest .update (myData );
230+ byte [] myDigest = messageDigest .digest ();
231+
232+ // A signature can be obtained from the SignResult returned by the CryptographyAsyncClient.sign() operation.
233+ cryptographyAsyncClient .verify (SignatureAlgorithm .ES256 , myDigest , signature )
234+ .contextWrite (context -> context .put (key1 , value1 ).put (key2 , value2 ))
222235 .subscribe (verifyResult ->
223- System .out .printf ("Verification status %s" , verifyResult .isValid ()));
236+ System .out .printf ("Verification status: %s. \n " , verifyResult .isValid ()));
224237 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.verify#SignatureAlgorithm-byte-byte
225238 }
226239
@@ -231,25 +244,28 @@ public void signVerify() throws NoSuchAlgorithmException {
231244 */
232245 public void wrapKeyUnwrapKey () {
233246 CryptographyAsyncClient cryptographyAsyncClient = createAsyncClient ();
247+
234248 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.wrapKey#KeyWrapAlgorithm-byte
235249 byte [] key = new byte [100 ];
236250 new Random (0x1234567L ).nextBytes (key );
237251
238252 cryptographyAsyncClient .wrapKey (KeyWrapAlgorithm .RSA_OAEP , key )
239- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
240- .subscribe (keyWrapResult ->
241- System .out .printf ("Received encypted key of length %d with algorithm %s" ,
242- keyWrapResult .getEncryptedKey ().length , keyWrapResult .getAlgorithm ().toString ()));
253+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
254+ .subscribe (wrapResult ->
255+ System .out .printf ("Received encrypted key of length: %d, with algorithm: %s. \n " ,
256+ wrapResult .getEncryptedKey ().length , wrapResult .getAlgorithm ().toString ()));
243257 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.wrapKey#KeyWrapAlgorithm-byte
244258
245259 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.unwrapKey#KeyWrapAlgorithm-byte
246- byte [] wrappedKey = new byte [100 ];
260+ byte [] keyToWrap = new byte [100 ];
247261 new Random (0x1234567L ).nextBytes (key );
248262
249- cryptographyAsyncClient .unwrapKey (KeyWrapAlgorithm .RSA_OAEP , wrappedKey )
250- .subscriberContext (reactor .util .context .Context .of (key1 , value1 , key2 , value2 ))
251- .subscribe (keyUnwrapResult ->
252- System .out .printf ("Received key of length %d" , keyUnwrapResult .getKey ().length ));
263+ cryptographyAsyncClient .wrapKey (KeyWrapAlgorithm .RSA_OAEP , keyToWrap )
264+ .contextWrite (context -> context .put (key1 , value1 ).put (key2 , value2 ))
265+ .subscribe (wrapResult ->
266+ cryptographyAsyncClient .unwrapKey (KeyWrapAlgorithm .RSA_OAEP , wrapResult .getEncryptedKey ())
267+ .subscribe (keyUnwrapResult ->
268+ System .out .printf ("Received key of length: %d.\n " , keyUnwrapResult .getKey ().length )));
253269 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.unwrapKey#KeyWrapAlgorithm-byte
254270 }
255271
@@ -261,31 +277,29 @@ public void wrapKeyUnwrapKey() {
261277 */
262278 public void signDataVerifyData () throws NoSuchAlgorithmException {
263279 CryptographyAsyncClient cryptographyAsyncClient = createAsyncClient ();
264- byte [] signature = new byte [ 100 ];
280+
265281 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.signData#SignatureAlgorithm-byte
266282 byte [] data = new byte [100 ];
267283 new Random (0x1234567L ).nextBytes (data );
268284
269285 cryptographyAsyncClient .sign (SignatureAlgorithm .ES256 , data )
270- .subscriberContext ( reactor . util . context . Context . of (key1 , value1 , key2 , value2 ))
286+ .contextWrite ( context -> context . put (key1 , value1 ). put ( key2 , value2 ))
271287 .subscribe (signResult ->
272- System .out .printf ("Received signature of length %d with algorithm %s" , signResult .getSignature ().length ));
288+ System .out .printf ("Received signature of length: %d, with algorithm: %s.\n " ,
289+ signResult .getSignature ().length , signResult .getAlgorithm ()));
273290 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.signData#SignatureAlgorithm-byte
274291
292+ byte [] signature = new byte [100 ];
293+
275294 // BEGIN: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.verifyData#SignatureAlgorithm-byte-byte
276- cryptographyAsyncClient .verify (SignatureAlgorithm .ES256 , data , signature )
277- .subscriberContext (reactor .util .context .Context .of (key1 , value1 , key2 , value2 ))
295+ byte [] myData = new byte [100 ];
296+ new Random (0x1234567L ).nextBytes (myData );
297+
298+ // A signature can be obtained from the SignResult returned by the CryptographyAsyncClient.sign() operation.
299+ cryptographyAsyncClient .verify (SignatureAlgorithm .ES256 , myData , signature )
300+ .contextWrite (context -> context .put (key1 , value1 ).put (key2 , value2 ))
278301 .subscribe (verifyResult ->
279- System .out .printf ("Verification status %s" , verifyResult .isValid ()));
302+ System .out .printf ("Verification status: %s. \n " , verifyResult .isValid ()));
280303 // END: com.azure.security.keyvault.keys.cryptography.CryptographyAsyncClient.verifyData#SignatureAlgorithm-byte-byte
281304 }
282-
283- /**
284- * Implementation not provided for this method.
285- *
286- * @return {@code null}.
287- */
288- private TokenCredential getKeyVaultCredential () {
289- return null ;
290- }
291305}
0 commit comments