fix: Encapsulate BASE64_FLAG

This commit is contained in:
J-Jamet
2023-05-14 22:05:08 +02:00
parent 09cf8aabf6
commit 05865fe8c3
8 changed files with 28 additions and 31 deletions

View File

@@ -27,6 +27,7 @@ import android.net.Uri
import android.os.IBinder
import android.util.Base64
import android.util.Log
import com.kunzisoft.keepass.database.element.binary.BinaryData.Companion.BASE64_FLAG
import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService
import com.kunzisoft.keepass.settings.PreferencesUtil
@@ -138,11 +139,11 @@ class CipherDatabaseAction(context: Context) {
this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri)
this.encryptedValue = Base64.decode(
cipherDatabaseEntity.encryptedValue,
Base64.NO_WRAP
BASE64_FLAG
)
this.specParameters = Base64.decode(
cipherDatabaseEntity.specParameters,
Base64.NO_WRAP
BASE64_FLAG
)
}
}
@@ -186,8 +187,8 @@ class CipherDatabaseAction(context: Context) {
val cipherDatabaseEntity = CipherDatabaseEntity(
databaseUri.toString(),
Base64.encodeToString(cipherEncryptDatabase.encryptedValue, Base64.NO_WRAP),
Base64.encodeToString(cipherEncryptDatabase.specParameters, Base64.NO_WRAP),
Base64.encodeToString(cipherEncryptDatabase.encryptedValue, BASE64_FLAG),
Base64.encodeToString(cipherEncryptDatabase.specParameters, BASE64_FLAG),
)
if (useTempDao) {

View File

@@ -23,7 +23,7 @@ import android.os.Parcelable
import android.util.Base64
import android.util.Log
import com.kunzisoft.encrypt.HashManager
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.binary.BinaryData.Companion.BASE64_FLAG
import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars
import com.kunzisoft.keepass.utils.StringUtil.toHexString
@@ -212,9 +212,7 @@ data class MasterCredential(var password: String? = null,
when (xmlKeyFileVersion) {
1F -> {
// No hash in KeyFile XML version 1
return Base64.decode(dataString,
DatabaseKDBX.BASE_64_FLAG
)
return Base64.decode(dataString, BASE64_FLAG)
}
2F -> {
return if (hashString != null

View File

@@ -21,11 +21,10 @@ package com.kunzisoft.keepass.database.element.binary
import android.os.Parcel
import android.os.Parcelable
import android.util.Base64
import android.util.Base64InputStream
import android.util.Base64OutputStream
import com.kunzisoft.keepass.utils.readAllBytes
import com.kunzisoft.keepass.database.element.binary.BinaryCache.Companion.UNKNOWN
import com.kunzisoft.keepass.utils.readAllBytes
import java.io.*
import java.util.zip.GZIPOutputStream
@@ -60,12 +59,12 @@ class BinaryByte : BinaryData {
@Throws(IOException::class)
override fun getInputDataStream(binaryCache: BinaryCache): InputStream {
return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), Base64.NO_WRAP)
return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), BASE64_FLAG)
}
@Throws(IOException::class)
override fun getOutputDataStream(binaryCache: BinaryCache): OutputStream {
return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), Base64.NO_WRAP))
return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), BASE64_FLAG))
}
@Throws(IOException::class)

View File

@@ -23,6 +23,7 @@ import android.app.ActivityManager
import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.util.Base64
import org.apache.commons.io.output.CountingOutputStream
import java.io.IOException
import java.io.InputStream
@@ -181,6 +182,7 @@ abstract class BinaryData : Parcelable {
companion object {
private val TAG = BinaryData::class.java.name
private const val MAX_BINARY_BYTE = 10485760 // 10 MB
const val BASE64_FLAG = Base64.NO_WRAP
fun canMemoryBeAllocatedInRAM(context: Context, memoryWanted: Long): Boolean {
if (memoryWanted > MAX_BINARY_BYTE)

View File

@@ -21,7 +21,6 @@ package com.kunzisoft.keepass.database.element.binary
import android.os.Parcel
import android.os.Parcelable
import android.util.Base64
import android.util.Base64InputStream
import android.util.Base64OutputStream
import com.kunzisoft.keepass.utils.readAllBytes
@@ -75,7 +74,7 @@ class BinaryFile : BinaryData {
return when {
file != null && file.length() > 0 -> {
cipherDecryption.init(Cipher.DECRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv))
Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), Base64.NO_WRAP)
Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), BASE64_FLAG)
}
else -> ByteArrayInputStream(ByteArray(0))
}
@@ -87,7 +86,7 @@ class BinaryFile : BinaryData {
return when {
file != null -> {
cipherEncryption.init(Cipher.ENCRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv))
BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), Base64.NO_WRAP))
BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), BASE64_FLAG))
}
else -> throw IOException("Unable to write in an unknown file")
}

View File

@@ -891,7 +891,5 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
private const val DEFAULT_HISTORY_MAX_ITEMS = 10 // -1 unlimited
private const val DEFAULT_HISTORY_MAX_SIZE = (6 * 1024 * 1024).toLong() // -1 unlimited
const val BASE_64_FLAG = Base64.NO_WRAP
}
}

View File

@@ -27,9 +27,9 @@ import com.kunzisoft.keepass.database.crypto.CrsAlgorithm
import com.kunzisoft.keepass.database.crypto.HmacBlock
import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.database.element.binary.BinaryData
import com.kunzisoft.keepass.database.element.binary.BinaryData.Companion.BASE64_FLAG
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
import com.kunzisoft.keepass.database.element.database.DatabaseVersioned
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX
@@ -346,7 +346,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} else if (name.equals(DatabaseKDBXXML.ElemHeaderHash, ignoreCase = true)) {
val encodedHash = readString(xpp)
if (encodedHash.isNotEmpty() && hashOfHeader != null) {
val hash = Base64.decode(encodedHash, BASE_64_FLAG)
val hash = Base64.decode(encodedHash, BASE64_FLAG)
if (!Arrays.equals(hash, hashOfHeader)) {
throw DatabaseInputException()
}
@@ -432,7 +432,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) {
val strData = readString(xpp)
if (strData.isNotEmpty()) {
customIconData = Base64.decode(strData, BASE_64_FLAG)
customIconData = Base64.decode(strData, BASE64_FLAG)
}
} else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) {
customIconName = readString(xpp)
@@ -836,7 +836,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
// Catch with null test below
}
} else {
var buf = Base64.decode(sDate, BASE_64_FLAG)
var buf = Base64.decode(sDate, BASE64_FLAG)
if (buf.size != 8) {
val buf8 = ByteArray(8)
System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8))
@@ -902,7 +902,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
}
return try {
val buf = Base64.decode(encoded, BASE_64_FLAG)
val buf = Base64.decode(encoded, BASE64_FLAG)
bytes16ToUuid(buf)
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 UUID, create a random one", e)
@@ -1022,7 +1022,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
isRAMSufficient.invoke(base64.length.toLong()), compressed, protected, binaryId)
try {
binaryAttachment.getOutputDataStream(mDatabase.binaryCache).use { outputStream ->
outputStream.write(Base64.decode(base64, BASE_64_FLAG))
outputStream.write(Base64.decode(base64, BASE64_FLAG))
}
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 attachment", e)
@@ -1055,7 +1055,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
if (xpp.attributeCount > 0) {
val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected)
if (protect != null && protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)) {
Base64.decode(xpp.safeNextText(), BASE_64_FLAG)?.let { data ->
Base64.decode(xpp.safeNextText(), BASE64_FLAG)?.let { data ->
return randomStream?.processBytes(data)
}
return ByteArray(0)

View File

@@ -26,9 +26,9 @@ import com.kunzisoft.encrypt.StreamCipher
import com.kunzisoft.keepass.database.crypto.CrsAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.database.element.binary.BinaryData.Companion.BASE64_FLAG
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
import com.kunzisoft.keepass.database.element.database.DatabaseVersioned
import com.kunzisoft.keepass.database.element.entry.AutoType
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
@@ -216,7 +216,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName)
if (hashOfHeader != null) {
writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encode(hashOfHeader!!, BASE_64_FLAG)))
writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encode(hashOfHeader!!, BASE64_FLAG)))
}
if (!header!!.version.isBefore(FILE_VERSION_40)) {
@@ -417,7 +417,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
writeString(name, DatabaseKDBXXML.DateFormatter.format(date))
} else {
val buf = longTo8Bytes(DateKDBXUtil.convertDateToKDBX4Time(date))
val b64 = String(Base64.encode(buf, BASE_64_FLAG))
val b64 = String(Base64.encode(buf, BASE64_FLAG))
writeString(name, b64)
}
}
@@ -441,7 +441,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class)
private fun writeUuid(name: String, uuid: UUID) {
val data = uuidTo16Bytes(uuid)
writeString(name, String(Base64.encode(data, BASE_64_FLAG)))
writeString(name, String(Base64.encode(data, BASE64_FLAG)))
}
/*
@@ -490,7 +490,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
// Write the XML
binary.getInputDataStream(binaryCache).use { inputStream ->
inputStream.readAllBytes { buffer ->
xml.text(String(Base64.encode(buffer, BASE_64_FLAG)))
xml.text(String(Base64.encode(buffer, BASE64_FLAG)))
}
}
} catch (e: Exception) {
@@ -562,7 +562,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
xml.attribute(null, DatabaseKDBXXML.AttrProtected, DatabaseKDBXXML.ValTrue)
val data = value.toString().toByteArray()
val encoded = randomStream?.processBytes(data) ?: ByteArray(0)
xml.text(String(Base64.encode(encoded, BASE_64_FLAG)))
xml.text(String(Base64.encode(encoded, BASE64_FLAG)))
} else {
xml.text(value.toString())
}
@@ -723,7 +723,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
Log.e(TAG, "Unable to write custom icon", e)
} finally {
writeString(DatabaseKDBXXML.ElemCustomIconItemData,
String(Base64.encode(customImageData, BASE_64_FLAG)))
String(Base64.encode(customImageData, BASE64_FLAG)))
}
if (iconCustom.name.isNotEmpty()) {
writeString(DatabaseKDBXXML.ElemName, iconCustom.name)