Search in sources :

Example 1 with CryptoManager

use of com.couchbase.client.core.encryption.CryptoManager in project spring-data-couchbase by spring-projects.

the class AbstractCouchbaseConfiguration method couchbaseObjectMapper.

/**
 * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
 *
 * @return ObjectMapper
 */
public ObjectMapper couchbaseObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new JsonValueModule());
    CryptoManager cryptoManager = null;
    if (cryptoManager != null) {
        mapper.registerModule(new EncryptionModule(cryptoManager));
    }
    return mapper;
}
Also used : CryptoManager(com.couchbase.client.core.encryption.CryptoManager) EncryptionModule(com.couchbase.client.java.encryption.databind.jackson.EncryptionModule) JsonValueModule(com.couchbase.client.java.json.JsonValueModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CryptoManager

use of com.couchbase.client.core.encryption.CryptoManager in project java-couchbase-encryption by couchbase.

the class AeadAes256CbcHmacSha512ProviderTest method encryptAndDecrypt.

@Test
void encryptAndDecrypt() throws Exception {
    final byte[] plaintext = "\"The enemy knows the system.\"".getBytes(UTF_8);
    final Map<String, Object> encrypted = mapOf("alg", "AEAD_AES_256_CBC_HMAC_SHA512", "kid", "test-key", "ciphertext", "GvOMLcK5b/3YZpQJI0G8BLm98oj20ZLdqKDV3MfTuGlWL4R5p5Deykuv2XLW4LcDvnOkmhuUSRbQ8QVEmbjq43XHdOm3ColJ6LzoaAtJihk=");
    AeadAes256CbcHmacSha512Provider provider = EncryptionTestHelper.provider();
    CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter()).defaultEncrypter(provider.encrypterForKey("test-key")).build();
    assertEquals(encrypted, cryptoManager.encrypt(plaintext, null));
    assertArrayEquals(plaintext, cryptoManager.decrypt(encrypted));
}
Also used : CryptoManager(com.couchbase.client.core.encryption.CryptoManager) Test(org.junit.jupiter.api.Test)

Example 3 with CryptoManager

use of com.couchbase.client.core.encryption.CryptoManager in project java-couchbase-encryption by couchbase.

the class LegacyAesDecrypterTest method canDecrypt.

@Test
void canDecrypt() {
    Map<String, byte[]> keys = new HashMap<>();
    keys.put("aes256Key", fakeKey(32));
    keys.put("aes128Key", fakeKey(16));
    keys.put("hmacKey", fakeKey(7));
    Keyring keyring = Keyring.fromMap(keys);
    // Generated by Java FLE version 1
    JsonObject obj = JsonObject.fromJson("{\n" + "  \"__crypt_one\": {\n" + "    \"sig\": \"TkpyZnha4xd+FsX1aEGqB235d495oOUBfC4Y+Gbic4U=\",\n" + "    \"ciphertext\": \"MZI/xSiM7919UTM5CO/RWg==\",\n" + "    \"alg\": \"AES-128-HMAC-SHA256\",\n" + "    \"iv\": \"HIBdmnoQD4DUgkBj5LAMFw==\",\n" + "    \"kid\": \"aes128Key\"\n" + "  },\n" + "  \"__crypt_two\": {\n" + "    \"sig\": \"f0hWVdBM/pEcADl0eKx8Eq/KhP00/2oqXYXTJkt2xhA=\",\n" + "    \"ciphertext\": \"biERBQajaRxrfe8mXFqJag==\",\n" + "    \"alg\": \"AES-256-HMAC-SHA256\",\n" + "    \"iv\": \"03AUmzwQqnbs/JhkWGrIkw==\",\n" + "    \"kid\": \"aes256Key\"\n" + "  }\n" + "}");
    CryptoManager cryptoManager = DefaultCryptoManager.builder().encryptedFieldNamePrefix("__crypt_").legacyAesDecrypters(keyring, keyName -> "hmacKey").build();
    JsonObjectCrypto crypto = obj.crypto(cryptoManager);
    assertEquals(1, crypto.getInt("one"));
    assertEquals(2, crypto.getInt("two"));
}
Also used : CryptoManager(com.couchbase.client.core.encryption.CryptoManager) JsonObjectCrypto(com.couchbase.client.java.json.JsonObjectCrypto) Test(org.junit.jupiter.api.Test) DefaultCryptoManager(com.couchbase.client.encryption.DefaultCryptoManager) Map(java.util.Map) JsonObject(com.couchbase.client.java.json.JsonObject) Assertions(org.junit.jupiter.api.Assertions) HashMap(java.util.HashMap) Keyring(com.couchbase.client.encryption.Keyring) Keyring(com.couchbase.client.encryption.Keyring) HashMap(java.util.HashMap) JsonObject(com.couchbase.client.java.json.JsonObject) CryptoManager(com.couchbase.client.core.encryption.CryptoManager) DefaultCryptoManager(com.couchbase.client.encryption.DefaultCryptoManager) JsonObjectCrypto(com.couchbase.client.java.json.JsonObjectCrypto) Test(org.junit.jupiter.api.Test)

Aggregations

CryptoManager (com.couchbase.client.core.encryption.CryptoManager)3 Test (org.junit.jupiter.api.Test)2 DefaultCryptoManager (com.couchbase.client.encryption.DefaultCryptoManager)1 Keyring (com.couchbase.client.encryption.Keyring)1 EncryptionModule (com.couchbase.client.java.encryption.databind.jackson.EncryptionModule)1 JsonObject (com.couchbase.client.java.json.JsonObject)1 JsonObjectCrypto (com.couchbase.client.java.json.JsonObjectCrypto)1 JsonValueModule (com.couchbase.client.java.json.JsonValueModule)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Assertions (org.junit.jupiter.api.Assertions)1