|
1 | 1 | /* |
2 | | - * Copyright 2018 Google Inc. |
| 2 | + * Copyright 2018 Google LLC |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -53,27 +53,20 @@ public static CryptoKey createAsymmetricKey(String projectId, String locationId, |
53 | 53 | String cryptoKeyId) |
54 | 54 | throws IOException { |
55 | 55 |
|
56 | | - // Create the Cloud KMS client. |
57 | 56 | try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { |
58 | | - // The resource name of the location associated with the KeyRing. |
59 | 57 | String parent = KeyRingName.format(projectId, locationId, keyRingId); |
60 | 58 |
|
61 | | - // Choose a purpose (ASYMMETRIC_DECRYPT or ASYMMETRIC_SIGN). |
62 | 59 | CryptoKeyPurpose purpose = CryptoKeyPurpose.ASYMMETRIC_DECRYPT; |
63 | | - |
64 | | - // Choose an algorithm. |
65 | 60 | CryptoKeyVersionAlgorithm algorithm = CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256; |
| 61 | + |
66 | 62 | CryptoKeyVersionTemplate version = CryptoKeyVersionTemplate.newBuilder() |
67 | 63 | .setAlgorithm(algorithm) |
68 | 64 | .build(); |
69 | | - |
70 | | - // Build the key template |
71 | 65 | CryptoKey cryptoKey = CryptoKey.newBuilder() |
72 | 66 | .setPurpose(purpose) |
73 | 67 | .setVersionTemplate(version) |
74 | 68 | .build(); |
75 | 69 |
|
76 | | - // Create the CryptoKey for your project. |
77 | 70 | CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey); |
78 | 71 |
|
79 | 72 | return createdKey; |
@@ -156,7 +149,9 @@ public static byte[] encryptRSA(String keyName, byte[] plaintext) |
156 | 149 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey); |
157 | 150 | PublicKey rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec); |
158 | 151 |
|
159 | | - // Encrypt the plaintext |
| 152 | + // Encrypt plaintext for the 'RSA_DECRYPT_OAEP_2048_SHA256' key. |
| 153 | + // For other key algorithms: |
| 154 | + // https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html |
160 | 155 | Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); |
161 | 156 | OAEPParameterSpec oaepParams = new OAEPParameterSpec( |
162 | 157 | "SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT); |
@@ -217,7 +212,9 @@ public static boolean verifySignatureRSA(String keyName, byte[] message, byte[] |
217 | 212 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey); |
218 | 213 | PublicKey rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec); |
219 | 214 |
|
220 | | - // Verify the signature |
| 215 | + // Verify the 'RSA_SIGN_PKCS1_2048_SHA256' signature. |
| 216 | + // For other key algorithms: |
| 217 | + // http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Signature |
221 | 218 | Signature rsaVerify = Signature.getInstance("SHA256withRSA"); |
222 | 219 | rsaVerify.initVerify(rsaKey); |
223 | 220 | rsaVerify.update(message); |
@@ -249,7 +246,9 @@ public static boolean verifySignatureEC(String keyName, byte[] message, byte[] s |
249 | 246 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey); |
250 | 247 | PublicKey ecKey = KeyFactory.getInstance("EC").generatePublic(keySpec); |
251 | 248 |
|
252 | | - // Verify the signature |
| 249 | + // Verify the 'EC_SIGN_P256_SHA256' signature |
| 250 | + // For other key algorithms: |
| 251 | + // http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Signature |
253 | 252 | Signature ecVerify = Signature.getInstance("SHA256withECDSA"); |
254 | 253 | ecVerify.initVerify(ecKey); |
255 | 254 | ecVerify.update(message); |
|
0 commit comments