47 lines
1.6 KiB
Java
47 lines
1.6 KiB
Java
|
package org.gcube.informationsystem.resourceregistry.er;
|
||
|
|
||
|
import java.security.Key;
|
||
|
|
||
|
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
|
||
|
import org.gcube.informationsystem.model.reference.properties.Encrypted;
|
||
|
import org.gcube.informationsystem.resourceregistry.ScopedTest;
|
||
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
||
|
import org.junit.Assert;
|
||
|
import org.junit.Test;
|
||
|
|
||
|
public class EncryptionTest extends ScopedTest {
|
||
|
|
||
|
public static final String PLAIN_VALUE = "my-test";
|
||
|
|
||
|
@Test
|
||
|
public void test() throws Exception {
|
||
|
|
||
|
Encrypted encrypted = new EncryptedImpl();
|
||
|
String contextEncryptedValue = EncryptedImpl.encrypt(PLAIN_VALUE);
|
||
|
encrypted.setEncryptedValue(contextEncryptedValue);
|
||
|
|
||
|
String getContextEncryptedValue = encrypted.getEncryptedValue();
|
||
|
Assert.assertTrue(getContextEncryptedValue.compareTo(getContextEncryptedValue)==0);
|
||
|
|
||
|
|
||
|
// Decrypting with Context Key (default key)
|
||
|
String decryptedValue = EncryptedImpl.decrypt(contextEncryptedValue);
|
||
|
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
|
||
|
|
||
|
// Encrypting with DB Key
|
||
|
Key databaseKey = DatabaseEnvironment.getDatabaseKey();
|
||
|
String dbEncryptedValue = EncryptedImpl.encrypt(decryptedValue, databaseKey);
|
||
|
|
||
|
// Setting the value encrypted with DB key
|
||
|
encrypted.setEncryptedValue(dbEncryptedValue);
|
||
|
|
||
|
String getDBEncryptedValue = encrypted.getEncryptedValue();
|
||
|
Assert.assertTrue(getDBEncryptedValue.compareTo(dbEncryptedValue)==0);
|
||
|
|
||
|
decryptedValue = EncryptedImpl.decrypt(getDBEncryptedValue, databaseKey);
|
||
|
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|