use of com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyResponse in project cos-java-sdk-v5 by tencentyun.
the class COSCryptoModuleBase method buildContentCryptoMaterial.
/**
* @param materials a non-null encryption material
*/
private ContentCryptoMaterial buildContentCryptoMaterial(EncryptionMaterials materials, Provider provider, CosServiceRequest req) {
byte[] iv = contentCryptoScheme.getIV();
if (iv == null) {
// Randomly generate the IV
iv = new byte[contentCryptoScheme.getIVLengthInBytes()];
cryptoScheme.getSecureRandom().nextBytes(iv);
}
if (materials.isKMSEnabled()) {
final Map<String, String> encryptionContext = ContentCryptoMaterial.mergeMaterialDescriptions(materials, req);
GenerateDataKeyRequest keyGenReq = new GenerateDataKeyRequest();
try {
ObjectMapper mapper = new ObjectMapper();
keyGenReq.setEncryptionContext(mapper.writeValueAsString(encryptionContext));
} catch (JsonProcessingException e) {
throw new CosClientException("generate datakey request set encryption context got json processing exception", e);
}
keyGenReq.setKeyId(materials.getCustomerMasterKeyId());
keyGenReq.setKeySpec(contentCryptoScheme.getKeySpec());
GenerateDataKeyResponse keyGenRes = kms.generateDataKey(keyGenReq);
byte[] key = Base64.decode(keyGenRes.getPlaintext());
final SecretKey cek = new SecretKeySpec(key, contentCryptoScheme.getKeyGeneratorAlgorithm());
byte[] keyBlob = keyGenRes.getCiphertextBlob().getBytes();
byte[] securedIV = ContentCryptoMaterial.encryptIV(iv, materials, cryptoScheme.getKeyWrapScheme(), cryptoScheme.getSecureRandom(), provider, kms, req);
return ContentCryptoMaterial.wrap(cek, iv, contentCryptoScheme, provider, new KMSSecuredCEK(keyBlob, encryptionContext), securedIV);
} else {
// Generate a one-time use symmetric key and initialize a cipher to encrypt object data
return ContentCryptoMaterial.create(generateCEK(materials, provider), iv, materials, cryptoScheme, provider, kms, req);
}
}
Aggregations