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

@@ -2,9 +2,11 @@ package nl.astraeus.persistence
import java.io.File
import java.io.ObjectInputStream
import java.util.*
class TransactionLog(
directory: File,
val decryptionKey: String? = null,
) {
val fileManager = FileManager(directory)
@@ -14,14 +16,15 @@ class TransactionLog(
printer("Snapshot:")
snapshot?.inputStream()?.use { input ->
ObjectInputStream(input).use { ois ->
ObjectInputStream(DecryptingInputStream(input, decryptionKey)).use { ois ->
val versionNumber = ois.readInt()
check(versionNumber == 1) {
"Unsupported version number: $versionNumber"
}
val transactionNumber = ois.readLong()
printer("[$versionNumber] $transactionNumber")
val data = ois.readObject() as MutableMap<Class<*>, TypeData>
val dataObj = ois.readObject()
val data = dataObj as MutableMap<Class<*>, TypeData>
printer("Data:")
printer("\tClasses:")
for ((cls, entries) in data.entries) {
@@ -35,15 +38,26 @@ class TransactionLog(
printer("Transactions:")
transactions?.forEach { transaction ->
transaction.inputStream().use { input ->
ObjectInputStream(input).use { ois ->
ObjectInputStream(DecryptingInputStream(input, decryptionKey)).use { ois ->
val versionNumber = ois.readInt()
check(versionNumber == 1) {
"Unsupported version number: $versionNumber"
}
val transactionNumber = ois.readLong()
val actions = ois.readObject() as Set<Action>
val actions = ois.readObject()
val actionList = when(actions) {
is Set<*> -> {
LinkedList(actions as Set<Action>)
}
is List<*> -> {
actions as List<Action>
}
else -> {
emptyList()
}
}
printer("\t[$transactionNumber]")
for (action in actions) {
for (action in actionList) {
printer("\t\t- $action")
}
}