Revert "fix: Base64 encoder as external library"

This reverts commit 1b96747187.
This commit is contained in:
J-Jamet 2023-05-14 21:56:11 +02:00
parent c2cc0a77ab
commit 111a77d984
5 changed files with 42 additions and 33 deletions

View file

@ -20,8 +20,10 @@ package com.kunzisoft.keepass.database.element
import android.os.Parcel
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.hardware.HardwareKey
import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars
import com.kunzisoft.keepass.utils.StringUtil.toHexString
@ -29,7 +31,6 @@ import com.kunzisoft.keepass.utils.readByteArrayCompat
import com.kunzisoft.keepass.utils.readEnum
import com.kunzisoft.keepass.utils.writeByteArrayCompat
import com.kunzisoft.keepass.utils.writeEnum
import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.Hex
import org.w3c.dom.Node
import java.io.ByteArrayInputStream
@ -211,7 +212,9 @@ data class MasterCredential(var password: String? = null,
when (xmlKeyFileVersion) {
1F -> {
// No hash in KeyFile XML version 1
return Base64.decodeBase64(dataString)
return Base64.decode(dataString,
DatabaseKDBX.BASE_64_FLAG
)
}
2F -> {
return if (hashString != null

View file

@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.database.element.database
import android.util.Base64
import android.util.Log
import com.kunzisoft.encrypt.HashManager
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
@ -885,9 +886,12 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
}
companion object {
val TYPE = DatabaseKDBX::class.java
private val TAG = DatabaseKDBX::class.java.name
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

@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.database.file.input
import android.util.Base64
import android.util.Log
import com.kunzisoft.encrypt.StreamCipher
import com.kunzisoft.keepass.database.crypto.CipherEngine
@ -28,6 +29,7 @@ import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.database.element.binary.BinaryData
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
@ -45,7 +47,6 @@ import com.kunzisoft.keepass.stream.HashedBlockInputStream
import com.kunzisoft.keepass.stream.HmacBlockInputStream
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.*
import org.apache.commons.codec.binary.Base64
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
@ -54,10 +55,7 @@ import java.io.InputStream
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
import java.text.ParseException
import java.util.Arrays
import java.util.Date
import java.util.Stack
import java.util.UUID
import java.util.*
import java.util.zip.GZIPInputStream
import javax.crypto.Cipher
import javax.crypto.CipherInputStream
@ -348,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.decodeBase64(encodedHash)
val hash = Base64.decode(encodedHash, BASE_64_FLAG)
if (!Arrays.equals(hash, hashOfHeader)) {
throw DatabaseInputException()
}
@ -434,7 +432,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) {
val strData = readString(xpp)
if (strData.isNotEmpty()) {
customIconData = Base64.decodeBase64(strData)
customIconData = Base64.decode(strData, BASE_64_FLAG)
}
} else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) {
customIconName = readString(xpp)
@ -838,7 +836,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
// Catch with null test below
}
} else {
var buf = Base64.decodeBase64(sDate)
var buf = Base64.decode(sDate, BASE_64_FLAG)
if (buf.size != 8) {
val buf8 = ByteArray(8)
System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8))
@ -904,7 +902,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
}
return try {
val buf = Base64.decodeBase64(encoded)
val buf = Base64.decode(encoded, BASE_64_FLAG)
bytes16ToUuid(buf)
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 UUID, create a random one", e)
@ -1024,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.decodeBase64(base64))
outputStream.write(Base64.decode(base64, BASE_64_FLAG))
}
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 attachment", e)
@ -1057,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.decodeBase64(xpp.safeNextText())?.let { data ->
Base64.decode(xpp.safeNextText(), BASE_64_FLAG)?.let { data ->
return randomStream?.processBytes(data)
}
return ByteArray(0)

View file

@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.database.file.output
import android.util.Base64
import android.util.Log
import android.util.Xml
import com.kunzisoft.encrypt.StreamCipher
@ -27,6 +28,7 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
import com.kunzisoft.keepass.database.element.*
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
@ -43,13 +45,11 @@ import com.kunzisoft.keepass.database.file.DateKDBXUtil
import com.kunzisoft.keepass.stream.HashedBlockOutputStream
import com.kunzisoft.keepass.stream.HmacBlockOutputStream
import com.kunzisoft.keepass.utils.*
import org.apache.commons.codec.binary.Base64
import org.xmlpull.v1.XmlSerializer
import java.io.IOException
import java.io.OutputStream
import java.security.SecureRandom
import java.util.Stack
import java.util.UUID
import java.util.*
import java.util.zip.GZIPOutputStream
import javax.crypto.Cipher
import javax.crypto.CipherOutputStream
@ -216,7 +216,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName)
if (hashOfHeader != null) {
writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encodeBase64(hashOfHeader!!)))
writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encode(hashOfHeader!!, BASE_64_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.encodeBase64(buf))
val b64 = String(Base64.encode(buf, BASE_64_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.encodeBase64(data)))
writeString(name, String(Base64.encode(data, BASE_64_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.encodeBase64(buffer)))
xml.text(String(Base64.encode(buffer, BASE_64_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.encodeBase64(encoded)))
xml.text(String(Base64.encode(encoded, BASE_64_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.encodeBase64(customImageData)))
String(Base64.encode(customImageData, BASE_64_FLAG)))
}
if (iconCustom.name.isNotEmpty()) {
writeString(DatabaseKDBXXML.ElemName, iconCustom.name)