Add encryption option

This commit is contained in:
2024-08-04 12:13:31 +02:00
parent c6f84224b1
commit ea0d46164f
13 changed files with 276 additions and 33 deletions

View File

@@ -0,0 +1,54 @@
package nl.astraeus.persistence
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.security.SecureRandom
class EncryptionTest {
@Test
fun testKeyGen() {
println(generateBase64Key())
}
@Test
fun testEncryptDecrypt() {
val random = SecureRandom()
val randomBytes = ByteArray(random.nextInt(10000))
random.nextBytes(randomBytes)
val base64Key = generateBase64Key()
val encryptor = Encryptor(
base64Key,
base64Key,
)
val encrypted = encryptor.encrypt(randomBytes)
val decrypted = encryptor.decrypt(encrypted)
assertArrayEquals(randomBytes, decrypted)
}
@Test
fun `test encryption-decryption streams`() {
val random = SecureRandom()
val key = generateBase64Key()
val baos = ByteArrayOutputStream()
val encryptionStream = EncryptingOutputStream(baos, key)
val bytes = ByteArray(random.nextInt(10000))
random.nextBytes(bytes)
encryptionStream.use {
it.write(bytes)
}
val bais = ByteArrayInputStream(baos.toByteArray())
val decryptingStream = DecryptingInputStream(bais, key)
val decryptedBytes = decryptingStream.readAllBytes()
assertArrayEquals(bytes, decryptedBytes)
}
}

View File

@@ -21,10 +21,12 @@ class TestOptimisticLocking {
val pst = Persistent(
directory = File("data", "test-locking"),
true,
null,
null,
arrayOf(
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
),
true
)
pst.transaction {

View File

@@ -22,7 +22,7 @@ class TestPersistence {
val pst = Persistent(
directory = File("data", "test-persistence"),
arrayOf(
indexes = arrayOf(
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
index<Person>("age") { p -> (p as? Person)?.age ?: -1 },
index<Person>("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },
@@ -131,7 +131,7 @@ class TestPersistence {
}
}
//pst.snapshot()
pst.snapshot()
pst.transaction {
store(
@@ -179,6 +179,6 @@ class TestPersistence {
}
pst.datastore.printStatus()
//pst.removeOldFiles()
pst.removeOldFiles()
}
}

View File

@@ -3,8 +3,8 @@ package nl.astraeus.persistence
import org.junit.jupiter.api.Test
import java.io.File
class TestPersistenceJavaInKotlin {
internal class Person(
var name: String,
var age: Int
@@ -23,19 +23,21 @@ class TestPersistenceJavaInKotlin {
val persistent = Persistent(
File("data", "java-kotlin-test"),
arrayOf(
enableOptimisticLocking = false,
indexes = arrayOf(
Index(
Person::class,
"name"
) { p -> (p as Person).name }
),
false
)
persistent.transaction {
val person = find(Person::class.java, 1L)
if (person != null) {
println("Person: ${person.name} is ${person.age} years old."
println(
"Person: ${person.name} is ${person.age} years old."
)
}
}

View File

@@ -22,7 +22,7 @@ class TestThreaded {
val pst = Persistent(
directory = File("data", "test-threaded"),
arrayOf(
indexes = arrayOf(
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
index<Person>("age") { p -> (p as? Person)?.age ?: -1 },
index<Person>("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },