Search in sources :

Example 1 with JsonObjectCrypto

use of com.couchbase.client.java.json.JsonObjectCrypto 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)1 DefaultCryptoManager (com.couchbase.client.encryption.DefaultCryptoManager)1 Keyring (com.couchbase.client.encryption.Keyring)1 JsonObject (com.couchbase.client.java.json.JsonObject)1 JsonObjectCrypto (com.couchbase.client.java.json.JsonObjectCrypto)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Assertions (org.junit.jupiter.api.Assertions)1 Test (org.junit.jupiter.api.Test)1