fix: targeted voucher code not working

This commit is contained in:
minjaesong
2025-02-15 23:56:53 +09:00
parent ad68e04b83
commit 37d6d3004b
4 changed files with 26 additions and 10 deletions

View File

@@ -33,9 +33,11 @@ class ActorValue : KVHashMap {
hashMap = newMap hashMap = newMap
} }
private fun isString(key: String) = (get(key) is String)
override fun set(key: String, value: Any) { override fun set(key: String, value: Any) {
// check if the key exists and is a blob // check if the key exists and is a blob
if (getAsString(key.toLowerCase())?.startsWith(BLOB) == true) { if (isString(key.toLowerCase()) && getAsString(key.toLowerCase())?.startsWith(BLOB) == true) {
throw IllegalStateException("Cannot write plain values to the blob object") throw IllegalStateException("Cannot write plain values to the blob object")
} }
else else

View File

@@ -106,7 +106,7 @@ object RedeemCodeMachine {
bytes[bytes.size - 1] = crc16.toByte() bytes[bytes.size - 1] = crc16.toByte()
val basePwd = initialPassword.random() val basePwd = initialPassword.random().copyOf()
val receiverPwd = receiver?.toByteArray() ?: ByteArray(16) // 128 bits of something val receiverPwd = receiver?.toByteArray() ?: ByteArray(16) // 128 bits of something
// xor basePWD with receiverPwd // xor basePWD with receiverPwd
@@ -124,11 +124,19 @@ object RedeemCodeMachine {
fun decode(codeStr: String, decoderUUID: UUID? = null): RedeemVoucher? { fun decode(codeStr: String, decoderUUID: UUID? = null): RedeemVoucher? {
val receiverPwd = decoderUUID?.toByteArray() ?: ByteArray(16) // 128 bits of something val receiverPwd = decoderUUID?.toByteArray() ?: ByteArray(16) // 128 bits of something
val passwords = initialPassword.map { basePwd -> // for decrypting targeted code
val passwords1 = initialPassword.map { basePwd ->
ByteArray(32) { i -> ByteArray(32) { i ->
basePwd[i] xor receiverPwd[i % 16] basePwd[i] xor receiverPwd[i % 16]
} }
} }
// for decrypting generic code
val passwords2 = initialPassword.map { basePwd ->
ByteArray(32) { i ->
basePwd[i]
}
}
val passwords = passwords1 + passwords2
// try to decode the input string by just trying all 8 possible keys // try to decode the input string by just trying all 8 possible keys
val decodeds = passwords.map { val decodeds = passwords.map {
@@ -149,7 +157,7 @@ object RedeemCodeMachine {
} }
// if all CRC fails... // if all CRC fails...
if (crcResults.none()) { if (crcResults.indexOf(true) < 0) {
return null return null
} }

View File

@@ -24,16 +24,18 @@ class UIRedeemCodeMachine : UICanvas(
override var width = Toolkit.drawWidth override var width = Toolkit.drawWidth
override var height = App.scr.height override var height = App.scr.height
private val codeCols = 12
val title = UIItemTextLabel(this, { "Enter the Code" }, val title = UIItemTextLabel(this, { "Enter the Code" },
(Toolkit.drawWidth - UIItemRedeemCodeArea.estimateWidth(14)) / 2, (Toolkit.drawWidth - UIItemRedeemCodeArea.estimateWidth(codeCols)) / 2,
App.scr.halfh - UIItemRedeemCodeArea.estimateHeight(4) - 48 - 48, App.scr.halfh - UIItemRedeemCodeArea.estimateHeight(4) - 48 - 48,
UIItemRedeemCodeArea.estimateWidth(14) UIItemRedeemCodeArea.estimateWidth(codeCols)
) )
val inputPanel = UIItemRedeemCodeArea(this, val inputPanel = UIItemRedeemCodeArea(this,
(Toolkit.drawWidth - UIItemRedeemCodeArea.estimateWidth(14)) / 2, (Toolkit.drawWidth - UIItemRedeemCodeArea.estimateWidth(codeCols)) / 2,
App.scr.halfh - UIItemRedeemCodeArea.estimateHeight(4) - 48, App.scr.halfh - UIItemRedeemCodeArea.estimateHeight(4) - 48,
14, 4) codeCols, 4)
init { init {
addUIitem(title) addUIitem(title)

View File

@@ -1,15 +1,19 @@
import net.torvald.terrarum.modulebasegame.redeemable.RedeemCodeMachine import net.torvald.terrarum.modulebasegame.redeemable.RedeemCodeMachine
import java.util.*
fun main() { fun main() {
val uuid = UUID.randomUUID()
val code = RedeemCodeMachine.encode( val code = RedeemCodeMachine.encode(
"item@basegame:65511", "item@basegame:65511",
6, 6,
true true,
null
) )
println(code) println(code)
val voucher = RedeemCodeMachine.decode(code) val voucher = RedeemCodeMachine.decode(code, uuid)
println(voucher) println(voucher)
} }