mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-29 21:33:07 -08:00
Change Unsigned Long Implementation
This commit is contained in:
1 parent
453a29b81c
commit
088816dfab
16 files changed
+130
-99
No files matched your search
@@ -24,6 +24,7 @@ import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.crypto.CryptoUtil
|
||||
import com.kunzisoft.keepass.crypto.finalkey.AESKeyTransformerFactory
|
||||
import com.kunzisoft.keepass.stream.bytes16ToUuid
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.security.SecureRandom
|
||||
import java.util.*
|
||||
@@ -38,11 +39,11 @@ class AesKdf : KdfEngine() {
|
||||
get() {
|
||||
return KdfParameters(uuid!!).apply {
|
||||
setParamUUID()
|
||||
setUInt64(PARAM_ROUNDS, defaultKeyRounds)
|
||||
setUInt64(PARAM_ROUNDS, UnsignedLong(defaultKeyRounds))
|
||||
}
|
||||
}
|
||||
|
||||
override val defaultKeyRounds: Long = 500000L
|
||||
override val defaultKeyRounds = 500000L
|
||||
|
||||
override fun getName(resources: Resources): String {
|
||||
return resources.getString(R.string.kdf_AES)
|
||||
@@ -61,7 +62,7 @@ class AesKdf : KdfEngine() {
|
||||
currentMasterKey = CryptoUtil.hashSha256(currentMasterKey)
|
||||
}
|
||||
|
||||
val rounds = kdfParameters.getUInt64(PARAM_ROUNDS)
|
||||
val rounds = kdfParameters.getUInt64(PARAM_ROUNDS)?.toKotlinLong()
|
||||
|
||||
return AESKeyTransformerFactory.transformMasterKey(seed, currentMasterKey, rounds) ?: ByteArray(0)
|
||||
}
|
||||
@@ -76,11 +77,11 @@ class AesKdf : KdfEngine() {
|
||||
}
|
||||
|
||||
override fun getKeyRounds(kdfParameters: KdfParameters): Long {
|
||||
return kdfParameters.getUInt64(PARAM_ROUNDS) ?: defaultKeyRounds
|
||||
return kdfParameters.getUInt64(PARAM_ROUNDS)?.toKotlinLong() ?: defaultKeyRounds
|
||||
}
|
||||
|
||||
override fun setKeyRounds(kdfParameters: KdfParameters, keyRounds: Long) {
|
||||
kdfParameters.setUInt64(PARAM_ROUNDS, keyRounds)
|
||||
kdfParameters.setUInt64(PARAM_ROUNDS, UnsignedLong(keyRounds))
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -24,6 +24,7 @@ import androidx.annotation.StringRes
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.stream.bytes16ToUuid
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.security.SecureRandom
|
||||
import java.util.*
|
||||
@@ -48,7 +49,7 @@ class Argon2Kdf(private val type: Type) : KdfEngine() {
|
||||
}
|
||||
|
||||
override val defaultKeyRounds: Long
|
||||
get() = DEFAULT_ITERATIONS
|
||||
get() = DEFAULT_ITERATIONS.toKotlinLong()
|
||||
|
||||
override fun getName(resources: Resources): String {
|
||||
return resources.getString(type.nameId)
|
||||
@@ -61,10 +62,10 @@ class Argon2Kdf(private val type: Type) : KdfEngine() {
|
||||
val parallelism = kdfParameters.getUInt32(PARAM_PARALLELISM)?.let {
|
||||
UnsignedInt(it)
|
||||
}
|
||||
val memory = kdfParameters.getUInt64(PARAM_MEMORY)?.div(MEMORY_BLOCK_SIZE)?.let {
|
||||
val memory = kdfParameters.getUInt64(PARAM_MEMORY)?.toKotlinLong()?.div(MEMORY_BLOCK_SIZE)?.let {
|
||||
UnsignedInt.fromKotlinLong(it)
|
||||
}
|
||||
val iterations = kdfParameters.getUInt64(PARAM_ITERATIONS)?.let {
|
||||
val iterations = kdfParameters.getUInt64(PARAM_ITERATIONS)?.toKotlinLong()?.let {
|
||||
UnsignedInt.fromKotlinLong(it)
|
||||
}
|
||||
val version = kdfParameters.getUInt32(PARAM_VERSION)?.let {
|
||||
@@ -95,32 +96,32 @@ class Argon2Kdf(private val type: Type) : KdfEngine() {
|
||||
}
|
||||
|
||||
override fun getKeyRounds(kdfParameters: KdfParameters): Long {
|
||||
return kdfParameters.getUInt64(PARAM_ITERATIONS) ?: defaultKeyRounds
|
||||
return kdfParameters.getUInt64(PARAM_ITERATIONS)?.toKotlinLong() ?: defaultKeyRounds
|
||||
}
|
||||
|
||||
override fun setKeyRounds(kdfParameters: KdfParameters, keyRounds: Long) {
|
||||
kdfParameters.setUInt64(PARAM_ITERATIONS, keyRounds)
|
||||
kdfParameters.setUInt64(PARAM_ITERATIONS, UnsignedLong(keyRounds))
|
||||
}
|
||||
|
||||
override val minKeyRounds: Long
|
||||
get() = MIN_ITERATIONS
|
||||
get() = MIN_ITERATIONS.toKotlinLong()
|
||||
|
||||
override val maxKeyRounds: Long
|
||||
get() = MAX_ITERATIONS
|
||||
get() = MAX_ITERATIONS.toKotlinLong()
|
||||
|
||||
override fun getMemoryUsage(kdfParameters: KdfParameters): Long {
|
||||
return kdfParameters.getUInt64(PARAM_MEMORY) ?: defaultMemoryUsage
|
||||
return kdfParameters.getUInt64(PARAM_MEMORY)?.toKotlinLong() ?: defaultMemoryUsage
|
||||
}
|
||||
|
||||
override fun setMemoryUsage(kdfParameters: KdfParameters, memory: Long) {
|
||||
kdfParameters.setUInt64(PARAM_MEMORY, memory)
|
||||
kdfParameters.setUInt64(PARAM_MEMORY, UnsignedLong(memory))
|
||||
}
|
||||
|
||||
override val defaultMemoryUsage: Long
|
||||
get() = DEFAULT_MEMORY
|
||||
get() = DEFAULT_MEMORY.toKotlinLong()
|
||||
|
||||
override val minMemoryUsage: Long
|
||||
get() = MIN_MEMORY
|
||||
get() = MIN_MEMORY.toKotlinLong()
|
||||
|
||||
override val maxMemoryUsage: Long
|
||||
get() = MAX_MEMORY
|
||||
@@ -139,10 +140,10 @@ class Argon2Kdf(private val type: Type) : KdfEngine() {
|
||||
get() = DEFAULT_PARALLELISM.toKotlinLong()
|
||||
|
||||
override val minParallelism: Long
|
||||
get() = MIN_PARALLELISM
|
||||
get() = MIN_PARALLELISM.toKotlinLong()
|
||||
|
||||
override val maxParallelism: Long
|
||||
get() = MAX_PARALLELISM
|
||||
get() = MAX_PARALLELISM.toKotlinLong()
|
||||
|
||||
enum class Type(val CIPHER_UUID: UUID, @StringRes val nameId: Int) {
|
||||
ARGON2_D(bytes16ToUuid(
|
||||
@@ -194,21 +195,17 @@ class Argon2Kdf(private val type: Type) : KdfEngine() {
|
||||
private val MIN_VERSION = UnsignedInt(0x10)
|
||||
private val MAX_VERSION = UnsignedInt(0x13)
|
||||
|
||||
private const val MIN_SALT = 8
|
||||
private val MAX_SALT = UnsignedInt.MAX_VALUE.toKotlinLong()
|
||||
private val DEFAULT_ITERATIONS = UnsignedLong(2L)
|
||||
private val MIN_ITERATIONS = UnsignedLong(1L)
|
||||
private val MAX_ITERATIONS = UnsignedLong(4294967295L)
|
||||
|
||||
private const val MIN_ITERATIONS: Long = 1L
|
||||
private const val MAX_ITERATIONS = 4294967295L
|
||||
|
||||
private const val MIN_MEMORY = (1024 * 8).toLong()
|
||||
private val DEFAULT_MEMORY = UnsignedLong((1024L * 1024L))
|
||||
private val MIN_MEMORY = UnsignedLong(1024L * 8L)
|
||||
private val MAX_MEMORY = UnsignedInt.MAX_VALUE.toKotlinLong()
|
||||
private const val MEMORY_BLOCK_SIZE: Long = 1024L
|
||||
|
||||
private const val MIN_PARALLELISM: Long = 1L
|
||||
private const val MAX_PARALLELISM: Long = ((1 shl 24) - 1).toLong()
|
||||
|
||||
private const val DEFAULT_ITERATIONS: Long = 2L
|
||||
private const val DEFAULT_MEMORY = (1024 * 1024).toLong()
|
||||
private val DEFAULT_PARALLELISM = UnsignedInt(2)
|
||||
private val MIN_PARALLELISM = UnsignedInt.fromKotlinLong(1L)
|
||||
private val MAX_PARALLELISM = UnsignedInt.fromKotlinLong(((1 shl 24) - 1))
|
||||
}
|
||||
}
|
||||
@@ -338,8 +338,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
|
||||
override fun touch(modified: Boolean, touchParents: Boolean) {
|
||||
super.touch(modified, touchParents)
|
||||
// TODO unsigned long
|
||||
usageCount = UnsignedLong(usageCount.toKotlinLong() + 1)
|
||||
usageCount.plusOne()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -248,9 +248,9 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader(
|
||||
|
||||
private fun setTransformRound(roundsByte: ByteArray) {
|
||||
assignAesKdfEngineIfNotExists()
|
||||
val rounds = bytes64ToLong(roundsByte)
|
||||
val rounds = bytes64ToULong(roundsByte)
|
||||
databaseV4.kdfParameters?.setUInt64(AesKdf.PARAM_ROUNDS, rounds)
|
||||
databaseV4.numberKeyEncryptionRounds = rounds
|
||||
databaseV4.numberKeyEncryptionRounds = rounds.toKotlinLong()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
@@ -325,7 +325,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader(
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun computeHeaderHmac(header: ByteArray, key: ByteArray): ByteArray {
|
||||
val blockKey = HmacBlockStream.getHmacKey64(key, UnsignedLong.MAX_VALUE)
|
||||
val blockKey = HmacBlockStream.getHmacKey64(key, UnsignedLong.MAX)
|
||||
|
||||
val hmac: Mac
|
||||
try {
|
||||
|
||||
@@ -17,36 +17,30 @@
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.file;
|
||||
package com.kunzisoft.keepass.database.file
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.Duration;
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.DateTimeZone
|
||||
import org.joda.time.Duration
|
||||
import java.util.*
|
||||
|
||||
import java.util.Date;
|
||||
object DateKDBXUtil {
|
||||
|
||||
public class DateKDBXUtil {
|
||||
private static final DateTime dotNetEpoch = new DateTime(1, 1, 1, 0, 0, 0, DateTimeZone.UTC);
|
||||
private static final DateTime javaEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC);
|
||||
private val dotNetEpoch = DateTime(1, 1, 1, 0, 0, 0, DateTimeZone.UTC)
|
||||
private val javaEpoch = DateTime(1970, 1, 1, 0, 0, 0, DateTimeZone.UTC)
|
||||
private val epochOffset = (javaEpoch.millis - dotNetEpoch.millis) / 1000L
|
||||
|
||||
private static final long epochOffset;
|
||||
|
||||
static {
|
||||
epochOffset = (javaEpoch.getMillis() - dotNetEpoch.getMillis()) / 1000L;
|
||||
}
|
||||
|
||||
public static Date convertKDBX4Time(long seconds) {
|
||||
DateTime dt = dotNetEpoch.plus(seconds * 1000L);
|
||||
fun convertKDBX4Time(seconds: Long): Date {
|
||||
val dt = dotNetEpoch.plus(seconds * 1000L)
|
||||
// Switch corrupted dates to a more recent date that won't cause issues on the client
|
||||
if (dt.isBefore(javaEpoch)) {
|
||||
return javaEpoch.toDate();
|
||||
}
|
||||
return dt.toDate();
|
||||
return if (dt.isBefore(javaEpoch)) {
|
||||
javaEpoch.toDate()
|
||||
} else dt.toDate()
|
||||
}
|
||||
|
||||
public static long convertDateToKDBX4Time(DateTime dt) {
|
||||
Duration duration = new Duration( javaEpoch, dt );
|
||||
long seconds = ( duration.getMillis() / 1000L );
|
||||
return seconds + epochOffset;
|
||||
fun convertDateToKDBX4Time(dt: DateTime?): Long {
|
||||
val duration = Duration(javaEpoch, dt)
|
||||
val seconds = duration.millis / 1000L
|
||||
return seconds + epochOffset
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ import com.kunzisoft.keepass.crypto.CipherFactory
|
||||
import com.kunzisoft.keepass.crypto.StreamCipherFactory
|
||||
import com.kunzisoft.keepass.crypto.engine.CipherEngine
|
||||
import com.kunzisoft.keepass.database.element.*
|
||||
import com.kunzisoft.keepass.database.element.binary.LoadedKey
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.database.element.binary.LoadedKey
|
||||
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ constructor(private val databaseKDBX: DatabaseKDBX,
|
||||
val hmac: Mac
|
||||
try {
|
||||
hmac = Mac.getInstance("HmacSHA256")
|
||||
val signingKey = SecretKeySpec(HmacBlockStream.getHmacKey64(hmacKey, UnsignedLong.MAX_VALUE), "HmacSHA256")
|
||||
val signingKey = SecretKeySpec(HmacBlockStream.getHmacKey64(hmacKey, UnsignedLong.MAX), "HmacSHA256")
|
||||
hmac.init(signingKey)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
throw DatabaseOutputException(e)
|
||||
|
||||
@@ -82,8 +82,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
|
||||
|
||||
header = outputHeader(mOutputStream)
|
||||
|
||||
val osPlain: OutputStream
|
||||
osPlain = if (header!!.version.toKotlinLong() < DatabaseHeaderKDBX.FILE_VERSION_32_4.toKotlinLong()) {
|
||||
val osPlain: OutputStream = if (header!!.version.toKotlinLong() < DatabaseHeaderKDBX.FILE_VERSION_32_4.toKotlinLong()) {
|
||||
val cos = attachStreamEncryptor(header!!, mOutputStream)
|
||||
cos.write(header!!.streamStartBytes)
|
||||
|
||||
|
||||
@@ -110,9 +110,8 @@ class HashedBlockOutputStream : OutputStream {
|
||||
throw IOException("SHA-256 not implemented here.")
|
||||
}
|
||||
|
||||
val hash: ByteArray
|
||||
messageDigest.update(buffer, 0, bufferPos)
|
||||
hash = messageDigest.digest()
|
||||
val hash: ByteArray = messageDigest.digest()
|
||||
baseStream.write(hash)
|
||||
|
||||
} else {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package com.kunzisoft.keepass.stream
|
||||
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.security.InvalidKeyException
|
||||
@@ -32,7 +33,7 @@ class HmacBlockInputStream(private val baseStream: InputStream, private val veri
|
||||
|
||||
private var buffer: ByteArray = ByteArray(0)
|
||||
private var bufferPos = 0
|
||||
private var blockIndex: Long = 0
|
||||
private var blockIndex = UnsignedLong(0L)
|
||||
private var endOfStream = false
|
||||
|
||||
@Throws(IOException::class)
|
||||
@@ -66,8 +67,6 @@ class HmacBlockInputStream(private val baseStream: InputStream, private val veri
|
||||
}
|
||||
|
||||
val copy = (buffer.size - bufferPos).coerceAtMost(remaining)
|
||||
assert(copy > 0)
|
||||
|
||||
System.arraycopy(buffer, bufferPos, outBuffer, offset, copy)
|
||||
offset += copy
|
||||
bufferPos += copy
|
||||
@@ -92,7 +91,7 @@ class HmacBlockInputStream(private val baseStream: InputStream, private val veri
|
||||
throw IOException("File corrupted")
|
||||
}
|
||||
|
||||
val pbBlockIndex = longTo8Bytes(blockIndex)
|
||||
val pbBlockIndex = uLongTo8Bytes(blockIndex)
|
||||
val pbBlockSize = baseStream.readBytesLength(4)
|
||||
if (pbBlockSize.size != 4) {
|
||||
throw IOException("File corrupted")
|
||||
@@ -126,13 +125,13 @@ class HmacBlockInputStream(private val baseStream: InputStream, private val veri
|
||||
cmpHmac = hmac.doFinal()
|
||||
Arrays.fill(blockKey, 0.toByte())
|
||||
|
||||
if (!Arrays.equals(cmpHmac, storedHmac)) {
|
||||
if (!cmpHmac.contentEquals(storedHmac)) {
|
||||
throw IOException("Invalid Hmac")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
blockIndex++
|
||||
blockIndex.plusOne()
|
||||
|
||||
if (blockSize.toKotlinLong() == 0L) {
|
||||
endOfStream = true
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package com.kunzisoft.keepass.stream
|
||||
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.security.InvalidKeyException
|
||||
@@ -34,7 +35,7 @@ class HmacBlockOutputStream(private val baseStream: OutputStream,
|
||||
|
||||
private val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
private var bufferPos = 0
|
||||
private var blockIndex: Long = 0
|
||||
private var blockIndex = UnsignedLong(0L)
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun close() {
|
||||
@@ -85,7 +86,7 @@ class HmacBlockOutputStream(private val baseStream: OutputStream,
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writeSafeBlock() {
|
||||
val bufBlockIndex = longTo8Bytes(blockIndex)
|
||||
val bufBlockIndex = uLongTo8Bytes(blockIndex)
|
||||
val blockSizeBuf = uIntTo4Bytes(UnsignedInt(bufferPos))
|
||||
|
||||
val blockHmac: ByteArray
|
||||
@@ -118,7 +119,7 @@ class HmacBlockOutputStream(private val baseStream: OutputStream,
|
||||
baseStream.write(buffer, 0, bufferPos)
|
||||
}
|
||||
|
||||
blockIndex++
|
||||
blockIndex.plusOne()
|
||||
bufferPos = 0
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,14 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.stream
|
||||
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.security.DigestOutputStream
|
||||
import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
|
||||
object HmacBlockStream {
|
||||
fun getHmacKey64(key: ByteArray, blockIndex: Long): ByteArray {
|
||||
fun getHmacKey64(key: ByteArray, blockIndex: UnsignedLong): ByteArray {
|
||||
val hash: MessageDigest
|
||||
try {
|
||||
hash = MessageDigest.getInstance("SHA-512")
|
||||
|
||||
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.stream
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.utils.StringDatabaseKDBUtils.bytesToString
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
@@ -119,18 +120,32 @@ fun InputStream.readBytesLength(length: Int): ByteArray {
|
||||
return buf
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.write4BytesUInt(value: UnsignedInt) {
|
||||
this.write(uIntTo4Bytes(value))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.writeBooleanByte(value: Boolean) {
|
||||
this.writeByte(if (value) 1.toByte() else 0.toByte())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.writeByte(byte: Byte) {
|
||||
this.write(byte.toInt())
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.write8BytesLong(value: Long) {
|
||||
this.write(longTo8Bytes(value))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.write8BytesLong(value: UnsignedLong) {
|
||||
this.write(uLongTo8Bytes(value))
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun OutputStream.write2BytesUShort(value: Int) {
|
||||
this.write(uShortTo2Bytes(value))
|
||||
}
|
||||
@@ -143,6 +158,20 @@ fun bytes2ToUShort(buf: ByteArray): Int {
|
||||
+ (buf[1].toInt() and 0xFF shl 8))
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a 64 bit to unsigned long
|
||||
*/
|
||||
fun bytes64ToULong(buf: ByteArray): UnsignedLong {
|
||||
return UnsignedLong((buf[0].toLong() and 0xFF)
|
||||
+ (buf[1].toLong() and 0xFF shl 8)
|
||||
+ (buf[2].toLong() and 0xFF shl 16)
|
||||
+ (buf[3].toLong() and 0xFF shl 24)
|
||||
+ (buf[4].toLong() and 0xFF shl 32)
|
||||
+ (buf[5].toLong() and 0xFF shl 40)
|
||||
+ (buf[6].toLong() and 0xFF shl 48)
|
||||
+ (buf[7].toLong() and 0xFF shl 56))
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a 64 bit long
|
||||
*/
|
||||
@@ -213,6 +242,17 @@ fun bytes5ToDate(buf: ByteArray, calendar: Calendar = Calendar.getInstance()): D
|
||||
return DateInstant(calendar.time)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write an unsigned 16-bit value
|
||||
*/
|
||||
fun uShortTo2Bytes(value: Int): ByteArray {
|
||||
val buf = ByteArray(2)
|
||||
buf[0] = (value and 0x00FF).toByte()
|
||||
buf[1] = (value and 0xFF00 shr 8).toByte()
|
||||
return buf
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 32-bit Int value.
|
||||
*/
|
||||
@@ -224,14 +264,8 @@ fun uIntTo4Bytes(value: UnsignedInt): ByteArray {
|
||||
return buf
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an unsigned 16-bit value
|
||||
*/
|
||||
fun uShortTo2Bytes(value: Int): ByteArray {
|
||||
val buf = ByteArray(2)
|
||||
buf[0] = (value and 0x00FF).toByte()
|
||||
buf[1] = (value and 0xFF00 shr 8).toByte()
|
||||
return buf
|
||||
fun uLongTo8Bytes(value: UnsignedLong): ByteArray {
|
||||
return longTo8Bytes(value.toKotlinLong())
|
||||
}
|
||||
|
||||
fun longTo8Bytes(value: Long): ByteArray {
|
||||
|
||||
@@ -19,10 +19,12 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.utils
|
||||
|
||||
class UnsignedLong(private var unsignedValue: Long) {
|
||||
class UnsignedLong(value: Long) {
|
||||
|
||||
private var unsignedValue: Long = value
|
||||
|
||||
/**
|
||||
* Convert an unsigned Integer to Long
|
||||
* Convert an unsigned Long to Kotlin Long
|
||||
*/
|
||||
fun toKotlinLong(): Long {
|
||||
return unsignedValue
|
||||
@@ -43,7 +45,15 @@ class UnsignedLong(private var unsignedValue: Long) {
|
||||
return unsignedValue.hashCode()
|
||||
}
|
||||
|
||||
fun plusOne() {
|
||||
if (unsignedValue >= 0L)
|
||||
unsignedValue++
|
||||
else
|
||||
unsignedValue--
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val MAX_VALUE: Long = -1
|
||||
val MAX = UnsignedLong(MAX_VALUE)
|
||||
}
|
||||
}
|
||||
@@ -53,12 +53,12 @@ open class VariantDictionary {
|
||||
return dict[name]?.value as UnsignedInt?
|
||||
}
|
||||
|
||||
fun setUInt64(name: String, value: Long) {
|
||||
fun setUInt64(name: String, value: UnsignedLong) {
|
||||
putType(VdType.UInt64, name, value)
|
||||
}
|
||||
|
||||
fun getUInt64(name: String): Long? {
|
||||
return dict[name]?.value as Long?
|
||||
fun getUInt64(name: String): UnsignedLong? {
|
||||
return dict[name]?.value as UnsignedLong?
|
||||
}
|
||||
|
||||
fun setBool(name: String, value: Boolean) {
|
||||
@@ -156,7 +156,7 @@ open class VariantDictionary {
|
||||
dictionary.setUInt32(name, bytes4ToUInt(valueBuf))
|
||||
}
|
||||
VdType.UInt64 -> if (valueLen == 8) {
|
||||
dictionary.setUInt64(name, bytes64ToLong(valueBuf))
|
||||
dictionary.setUInt64(name, bytes64ToULong(valueBuf))
|
||||
}
|
||||
VdType.Bool -> if (valueLen == 1) {
|
||||
dictionary.setBool(name, valueBuf[0] != 0.toByte())
|
||||
@@ -185,7 +185,7 @@ open class VariantDictionary {
|
||||
outputStream.write2BytesUShort(VdVersion)
|
||||
for ((name, vd) in variantDictionary.dict) {
|
||||
val nameBuf = name.toByteArray(UTF8Charset)
|
||||
outputStream.write(vd.type.toInt())
|
||||
outputStream.writeByte(vd.type)
|
||||
outputStream.write4BytesUInt(UnsignedInt(nameBuf.size))
|
||||
outputStream.write(nameBuf)
|
||||
var buf: ByteArray
|
||||
@@ -196,12 +196,11 @@ open class VariantDictionary {
|
||||
}
|
||||
VdType.UInt64 -> {
|
||||
outputStream.write4BytesUInt(UnsignedInt(8))
|
||||
outputStream.write8BytesLong(vd.value as Long)
|
||||
outputStream.write8BytesLong(vd.value as UnsignedLong)
|
||||
}
|
||||
VdType.Bool -> {
|
||||
outputStream.write4BytesUInt(UnsignedInt(1))
|
||||
val bool = if (vd.value as Boolean) 1.toByte() else 0.toByte()
|
||||
outputStream.write(bool.toInt())
|
||||
outputStream.writeBooleanByte(vd.value as Boolean)
|
||||
}
|
||||
VdType.Int32 -> {
|
||||
outputStream.write4BytesUInt(UnsignedInt(4))
|
||||
|
||||
Reference in new issue
Block a user