Skip to content

Commit

Permalink
Isaac network support, cache table index changes, xtea decryption cha…
Browse files Browse the repository at this point in the history
…nges, write boolean bits
  • Loading branch information
GregHib committed Apr 1, 2019
1 parent 4703a96 commit bb3e6f0
Show file tree
Hide file tree
Showing 29 changed files with 854 additions and 356 deletions.
78 changes: 45 additions & 33 deletions src/main/kotlin/world/gregs/hestia/core/cache/CacheStore.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package world.gregs.hestia.core.cache

import io.netty.buffer.Unpooled
import world.gregs.hestia.core.Settings
import world.gregs.hestia.core.cache.crypto.Rsa
import world.gregs.hestia.core.cache.crypto.Whirlpool
import world.gregs.hestia.core.cache.store.Index
import world.gregs.hestia.core.cache.store.Store
import java.io.ByteArrayOutputStream
import java.io.DataOutputStream
import java.math.BigInteger
import java.nio.Buffer
import java.nio.ByteBuffer

class CacheStore @Throws(IllegalArgumentException::class) constructor(path: String = Settings.getString("cachePath") ?: throw IllegalArgumentException()) : Cache {
class CacheStore @Throws(IllegalArgumentException::class) constructor(path: String = Settings.getString("cachePath") ?: throw IllegalArgumentException(), modulus: BigInteger? = BigInteger(Settings.getString("rsaModulus"), 16), private: BigInteger? = BigInteger(Settings.getString("rsaPrivate"), 16)) : Cache {

private val store = Store(path)
private var versionTable = createVersionTable()
private var versionTable = createVersionTable(true, modulus, private)

override fun getFile(index: Int, archive: Int, file: Int): ByteArray? {
return getIndex(index).getFile(archive, file)
Expand Down Expand Up @@ -39,37 +44,44 @@ class CacheStore @Throws(IllegalArgumentException::class) constructor(path: Stri
return store.indexes.size
}

private fun createVersionTable(): ByteArray {
val buffer = Unpooled.directBuffer()
store.run {
buffer.writeByte(indexes.size)
for (i in 0 until indexes.size) {
buffer.writeInt(indexes[i].crc)
buffer.writeInt(indexes[i].table?.revision ?: 0)
buffer.writeBytes(indexes[i].whirlpool ?: ByteArray(64))
private fun createVersionTable(whirlpool: Boolean, modulus: BigInteger?, private: BigInteger?): ByteArray {
val bout = ByteArrayOutputStream()
DataOutputStream(bout).use { buffer ->
store.run {
if(whirlpool) {
buffer.writeByte(indexes.size)
}

for (i in 0 until indexes.size) {
buffer.writeInt(indexes[i].crc)
buffer.writeInt(indexes[i].table?.revision ?: 0)
if(whirlpool) {
buffer.write(indexes[i].whirlpool ?: ByteArray(64))
//keys?
}
}
}
}

//Clone data
val mainFileData = ByteArray(buffer.writerIndex())
buffer.readBytes(mainFileData)

//Write whirlpool hash
buffer.resetReaderIndex()
buffer.writeByte(0)
buffer.writeBytes(Whirlpool.whirlpool(mainFileData, 0, mainFileData.size))

//Extra data for CacheArchiveData
val extra = Unpooled.directBuffer(5)
extra.writeByte(0)
extra.writeInt(buffer.readableBytes())

//Cloned to remove excess data
val bytes = ByteArray(buffer.readableBytes() + extra.readableBytes())
buffer.readBytes(bytes, extra.readableBytes(), buffer.readableBytes())
extra.readBytes(bytes, 0, extra.readableBytes())
buffer.release()
extra.release()
return bytes
if(whirlpool) {
val bytes = bout.toByteArray()
var temp = ByteBuffer.allocate(65)
temp.put(1)
temp.put(Whirlpool.whirlpool(bytes, 0, bytes.size))
(temp as Buffer).flip()

if (modulus != null && private != null) {
temp = Rsa.crypt(temp, modulus, private)
}

buffer.write(temp.array())
}

val data = bout.toByteArray()
val out = ByteBuffer.allocate(5 + data.size)
out.put(0)
out.putInt(data.size)
out.put(data)
return out.array()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package world.gregs.hestia.core.cache.crypto

interface Cipher {

fun nextInt(): Int

}
Loading

0 comments on commit bb3e6f0

Please sign in to comment.