mirror of
https://github.com/ProtonMail/protoncore_android.git
synced 2026-05-15 09:50:41 +00:00
feat(crypto, key): Add parameter to trim spaces for detached signatures.
Add a parameter to control whether trailing spaces should be trimmed from content when generating and verifying text detached signatures. The default value of the parameter keeps the behavior as it was before.
This commit is contained in:
committed by
Marin Thiercelin
parent
db081c1ac5
commit
4e35f44e9f
+9
-5
@@ -189,7 +189,7 @@ class FakePGPCrypto : PGPCrypto {
|
||||
TODO("Not yet implemented: decryptSessionKeyWithPassword")
|
||||
}
|
||||
|
||||
override fun signText(plainText: String, unlockedKey: Unarmored): Signature {
|
||||
override fun signText(plainText: String, unlockedKey: Unarmored, trimTrailingSpaces: Boolean): Signature {
|
||||
return "${plainText.hashCode()}"
|
||||
}
|
||||
|
||||
@@ -204,7 +204,8 @@ class FakePGPCrypto : PGPCrypto {
|
||||
override fun signTextEncrypted(
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>
|
||||
encryptionKeys: List<Armored>,
|
||||
trimTrailingSpaces: Boolean
|
||||
): EncryptedSignature {
|
||||
TODO("Not yet implemented: signTextEncrypted")
|
||||
}
|
||||
@@ -229,7 +230,8 @@ class FakePGPCrypto : PGPCrypto {
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean {
|
||||
return "${plainText.hashCode()}" == signature
|
||||
}
|
||||
@@ -251,7 +253,8 @@ class FakePGPCrypto : PGPCrypto {
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Long? {
|
||||
TODO("Not yet implemented: getVerifiedTimestampOfText")
|
||||
}
|
||||
@@ -270,7 +273,8 @@ class FakePGPCrypto : PGPCrypto {
|
||||
encryptedSignature: EncryptedSignature,
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean {
|
||||
TODO("Not yet implemented: verifyTextEncrypted")
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ public final class me/proton/core/crypto/android/pgp/GOpenPGPCrypto : me/proton/
|
||||
public fun getPublicKey (Ljava/lang/String;)Ljava/lang/String;
|
||||
public fun getUnarmored (Ljava/lang/String;)[B
|
||||
public fun getVerifiedTimestampOfData ([BLjava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public fun getVerifiedTimestampOfText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public fun getVerifiedTimestampOfText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public fun isPrivateKey (Ljava/lang/String;)Z
|
||||
public fun isPublicKey (Ljava/lang/String;)Z
|
||||
public fun isValidKey (Ljava/lang/String;)Z
|
||||
@@ -90,8 +90,8 @@ public final class me/proton/core/crypto/android/pgp/GOpenPGPCrypto : me/proton/
|
||||
public fun signDataEncrypted ([B[BLjava/util/List;)Ljava/lang/String;
|
||||
public fun signFile (Ljava/io/File;[B)Ljava/lang/String;
|
||||
public fun signFileEncrypted (Ljava/io/File;[BLjava/util/List;)Ljava/lang/String;
|
||||
public fun signText (Ljava/lang/String;[B)Ljava/lang/String;
|
||||
public fun signTextEncrypted (Ljava/lang/String;[BLjava/util/List;)Ljava/lang/String;
|
||||
public fun signText (Ljava/lang/String;[BZ)Ljava/lang/String;
|
||||
public fun signTextEncrypted (Ljava/lang/String;[BLjava/util/List;Z)Ljava/lang/String;
|
||||
public fun unlock (Ljava/lang/String;[B)Lme/proton/core/crypto/common/pgp/UnlockedKey;
|
||||
public fun updatePrivateKeyPassphrase (Ljava/lang/String;[B[B)Ljava/lang/String;
|
||||
public fun updateTime (J)V
|
||||
@@ -99,8 +99,8 @@ public final class me/proton/core/crypto/android/pgp/GOpenPGPCrypto : me/proton/
|
||||
public fun verifyDataEncrypted ([BLjava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public fun verifyFile (Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public fun verifyFileEncrypted (Ljava/io/File;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public fun verifyText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public fun verifyTextEncrypted (Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public fun verifyText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public fun verifyTextEncrypted (Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/crypto/android/pgp/GOpenPGPCrypto$Companion {
|
||||
|
||||
+154
@@ -1006,4 +1006,158 @@ internal class GOpenPGPCryptoTest {
|
||||
fun isPublicKeyWithRandomData() {
|
||||
assertFalse(crypto.isPublicKey("RANDOM DATA"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun signDetachedTrimTrailingSpaces() {
|
||||
// given
|
||||
val plainText = "this is a test\nWith spaces: \nAnd trailing tabs:\t"
|
||||
val publicKey = TestKey.privateKeyPublicKey
|
||||
// when
|
||||
val signature = crypto.unlock(TestKey.privateKey, TestKey.privateKeyPassphrase).use { unlockedKey ->
|
||||
crypto.signText(
|
||||
plainText,
|
||||
unlockedKey.value,
|
||||
trimTrailingSpaces = true
|
||||
)
|
||||
}
|
||||
// then
|
||||
assertTrue {
|
||||
crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = true
|
||||
)
|
||||
}
|
||||
assertFalse {
|
||||
crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyDetachedGopenpgpv2_4_10() {
|
||||
// given
|
||||
crypto.updateTime(1671550000)
|
||||
val plainText = "This is a test\nWith trailing spaces: \n With leading spaces\nWith trailing tabs:\t\t\n\tWith leading tabs\nWith trailing carriage returns:\r\n\rWith leading carriage returns\n\t \r With a mix \t\r\n"
|
||||
val signature = """
|
||||
-----BEGIN PGP SIGNATURE-----
|
||||
Version: GopenPGP 2.4.10
|
||||
Comment: https://gopenpgp.org
|
||||
|
||||
wsBzBAABCgAnBQJjocOZCZARwx6OXgf00BYhBCDPNjtY7JnnIuU+xBHDHo5eB/TQ
|
||||
AACGgwf7Bx6J7JLZ2G6RFvr/wtl0DENZxUVS4H3wZPEIuVTh3/Lzd5BHfWN/mD+q
|
||||
Sz0BcjRNxAI+nDY2/J8HPIibNg1NDlUgrgxK0NPLS1DMWmtoW3JTF5sfFMyiVGxo
|
||||
RH4oluOe/UQcfxYTbMr8/EX8Gc9kdx4U7MqQNEc9CM5VIuxrfMpSZ2hvn5zlwexQ
|
||||
WdnWjVWePpbwpltX98wTlAtU93XARUgeIMrzkhEBc1sNSg6/ynECLENm8EMxWQmj
|
||||
9lpaROb2Fw50G7S1YjSUlc7WK+e4+IIP3Fqw/b21Kd1BasHS92OuHZNalbxyJA0F
|
||||
V6Zkmvzj3h9CucLSJw1Bo6ZJTDbkBQ==
|
||||
=fVs7
|
||||
-----END PGP SIGNATURE-----
|
||||
""".trimIndent()
|
||||
val publicKey = TestKey.privateKeyPublicKey
|
||||
// when
|
||||
val verifiedWithTrimming = crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = true
|
||||
)
|
||||
val verifiedWithoutTrimming = crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = false
|
||||
)
|
||||
// then
|
||||
assertTrue(verifiedWithTrimming)
|
||||
assertFalse(verifiedWithoutTrimming)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun verifyDetachedGopenpgpv2_5_0() {
|
||||
// given
|
||||
crypto.updateTime(1671550000)
|
||||
val plainText = "This is a test\n" +
|
||||
"With trailing spaces: \n" +
|
||||
" With leading spaces\n" +
|
||||
"With trailing tabs:\t\t\n" +
|
||||
"\tWith leading tabs\n" +
|
||||
"With trailing carriage returns:\r\n" +
|
||||
"\rWith leading carriage returns\n" +
|
||||
"\t \r With a mix \t\r\n"
|
||||
val signature = """
|
||||
-----BEGIN PGP SIGNATURE-----
|
||||
Version: GopenPGP 2.5.0
|
||||
Comment: https://gopenpgp.org
|
||||
|
||||
wsBzBAEBCgAnBQJjocO4CZARwx6OXgf00BYhBCDPNjtY7JnnIuU+xBHDHo5eB/TQ
|
||||
AACLDQgAiGesYiKYkZCiFvytCmsFa/yTaOh96YaOlGwdXErbwsmEu6ZJfjoLp+Bp
|
||||
bBfpWDIrr93J3J8r9GVLAPrr3Eln3H4gyTNGXsfoCBjAE/25Ly7UtxrXjOonwW49
|
||||
QrbtlZ+t8QzdVdLAppi1LNPgt3PEUQozhHF1PvJUgb97fHTnDydOUD1CKl5zskTl
|
||||
fgRmTojIVqmPkG9VMWdc1sYyPixqTvaXp/Si0YVuHrH/NAjX1VHBLbRanVnd+Gnv
|
||||
2FlchBhWOipboS9Z6wf/4i83ZdOW61xqquUXwNI/K1ZadmS8X/+ojRO93V3FNWmR
|
||||
27KgLumCX2j+vKvb6E3YMWTmTfrxsg==
|
||||
=kmgZ
|
||||
-----END PGP SIGNATURE-----
|
||||
""".trimIndent()
|
||||
val publicKey = TestKey.privateKeyPublicKey
|
||||
// when
|
||||
val verifiedWithTrimming = crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = true
|
||||
)
|
||||
val verifiedWithoutTrimming = crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = false
|
||||
)
|
||||
val timestamp = crypto.getVerifiedTimestampOfData(
|
||||
plainText.toByteArray(),
|
||||
signature,
|
||||
publicKey
|
||||
)
|
||||
// then
|
||||
assertFalse(verifiedWithTrimming)
|
||||
assertTrue(verifiedWithoutTrimming)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun signDetachedNoTrimTrailingSpaces() {
|
||||
// given
|
||||
val plainText = "this is a test\nWith spaces: \nAnd trailing tabs:\t"
|
||||
val publicKey = TestKey.privateKeyPublicKey
|
||||
// when
|
||||
val signature = crypto.unlock(TestKey.privateKey, TestKey.privateKeyPassphrase).use { unlockedKey ->
|
||||
crypto.signText(
|
||||
plainText,
|
||||
unlockedKey.value,
|
||||
trimTrailingSpaces = false
|
||||
)
|
||||
}
|
||||
// then
|
||||
assertFalse {
|
||||
crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = true
|
||||
)
|
||||
}
|
||||
assertTrue {
|
||||
crypto.verifyText(
|
||||
plainText,
|
||||
signature,
|
||||
publicKey,
|
||||
trimTrailingSpaces = false
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+69
-43
@@ -36,6 +36,7 @@ import com.proton.gopenpgp.helper.Helper
|
||||
import com.proton.gopenpgp.helper.Mobile2GoReader
|
||||
import com.proton.gopenpgp.helper.Mobile2GoWriter
|
||||
import com.proton.gopenpgp.srp.Srp
|
||||
import me.proton.core.crypto.common.pgp.trimLinesEnd
|
||||
import me.proton.core.crypto.common.keystore.use
|
||||
import me.proton.core.crypto.common.pgp.Armored
|
||||
import me.proton.core.crypto.common.pgp.DataPacket
|
||||
@@ -156,7 +157,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
private fun encryptMessageSessionKey(
|
||||
plainMessage: PlainMessage,
|
||||
sessionKey: SessionKey,
|
||||
signKeyRing: KeyRing? = null,
|
||||
signKeyRing: KeyRing? = null
|
||||
): DataPacket {
|
||||
return sessionKey.toInternalSessionKey().let { internalSessionKey ->
|
||||
if (signKeyRing != null) {
|
||||
@@ -227,7 +228,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
private fun encryptAndSignMessageSessionKey(
|
||||
plainMessage: PlainMessage,
|
||||
sessionKey: SessionKey,
|
||||
unlockedKey: Unarmored,
|
||||
unlockedKey: Unarmored
|
||||
): DataPacket {
|
||||
newKey(unlockedKey).use { key ->
|
||||
newKeyRing(key).use { keyRing ->
|
||||
@@ -263,7 +264,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
private fun decryptDataSessionKey(
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
sessionKey: SessionKey
|
||||
): PlainMessage {
|
||||
val internalSessionKey = sessionKey.toInternalSessionKey()
|
||||
return internalSessionKey.decrypt(data)
|
||||
@@ -364,7 +365,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
private fun signMessageDetachedEncrypted(
|
||||
plainMessage: PlainMessage,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeyRing: KeyRing,
|
||||
encryptionKeyRing: KeyRing
|
||||
): EncryptedSignature {
|
||||
newKey(unlockedKey).use { key ->
|
||||
newKeyRing(key).use { keyRing ->
|
||||
@@ -376,7 +377,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
private fun signFileDetachedEncrypted(
|
||||
source: File,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeyRing: KeyRing,
|
||||
encryptionKeyRing: KeyRing
|
||||
): EncryptedSignature {
|
||||
source.inputStream().use { fileInputStream ->
|
||||
val reader = Mobile2GoReader(fileInputStream.mobileReader())
|
||||
@@ -513,7 +514,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
override fun encryptData(
|
||||
data: ByteArray,
|
||||
sessionKey: SessionKey,
|
||||
sessionKey: SessionKey
|
||||
): DataPacket = runCatching {
|
||||
encryptMessageSessionKey(PlainMessage(data), sessionKey)
|
||||
}.getOrElse { throw CryptoException("Data cannot be encrypted.", it) }
|
||||
@@ -521,7 +522,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
override fun encryptFile(
|
||||
source: File,
|
||||
destination: File,
|
||||
sessionKey: SessionKey,
|
||||
sessionKey: SessionKey
|
||||
): EncryptedFile = runCatching {
|
||||
encryptFileSessionKey(source, destination, sessionKey)
|
||||
}.getOrElse { throw CryptoException("File cannot be encrypted.", it) }
|
||||
@@ -629,7 +630,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
message: EncryptedMessage,
|
||||
publicKeys: List<Armored>,
|
||||
unlockedKeys: List<Unarmored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): DecryptedText = runCatching {
|
||||
decryptAndVerifyMessage(message, publicKeys, unlockedKeys, time.toUtcSeconds()) {
|
||||
DecryptedText(
|
||||
@@ -643,7 +644,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
message: EncryptedMessage,
|
||||
publicKeys: List<Armored>,
|
||||
unlockedKeys: List<Unarmored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): DecryptedData = runCatching {
|
||||
decryptAndVerifyMessage(message, publicKeys, unlockedKeys, time.toUtcSeconds()) {
|
||||
DecryptedData(
|
||||
@@ -657,7 +658,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): DecryptedData = runCatching {
|
||||
decryptAndVerifyDataSessionKey(data, sessionKey, publicKeys, time.toUtcSeconds()).let {
|
||||
DecryptedData(
|
||||
@@ -672,7 +673,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
destination: File,
|
||||
sessionKey: SessionKey,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): DecryptedFile = runCatching {
|
||||
decryptAndVerifyFileSessionKey(source, destination, sessionKey, publicKeys, time.toUtcSeconds())
|
||||
}.getOrElse { throw CryptoException("File cannot be decrypted.", it) }
|
||||
@@ -701,9 +702,11 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
override fun signText(
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored
|
||||
unlockedKey: Unarmored,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Signature = runCatching {
|
||||
signMessageDetached(PlainMessage(plainText), unlockedKey)
|
||||
val plainTextTrimmed = plainText.trimLinesEndIf { trimTrailingSpaces }
|
||||
signMessageDetached(PlainMessage(plainTextTrimmed), unlockedKey)
|
||||
}.getOrElse { throw CryptoException("PlainText cannot be signed.", it) }
|
||||
|
||||
override fun signData(
|
||||
@@ -724,14 +727,17 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>,
|
||||
trimTrailingSpaces: Boolean
|
||||
): EncryptedSignature = runCatching {
|
||||
signMessageDetachedEncrypted(PlainMessage(plainText), unlockedKey, encryptionKeys.keyRing())
|
||||
val plainTextTrimmed = plainText.trimLinesEndIf { trimTrailingSpaces }
|
||||
signMessageDetachedEncrypted(PlainMessage(plainTextTrimmed), unlockedKey, encryptionKeys.keyRing())
|
||||
}.getOrElse { throw CryptoException("PlainText cannot be signed.", it) }
|
||||
|
||||
|
||||
override fun signDataEncrypted(
|
||||
data: ByteArray,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>,
|
||||
encryptionKeys: List<Armored>
|
||||
): EncryptedSignature = runCatching {
|
||||
signMessageDetachedEncrypted(PlainMessage(data), unlockedKey, encryptionKeys.keyRing())
|
||||
}.getOrElse { throw CryptoException("Data cannot be signed.", it) }
|
||||
@@ -739,7 +745,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
override fun signFileEncrypted(
|
||||
file: File,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>,
|
||||
encryptionKeys: List<Armored>
|
||||
): EncryptedSignature = runCatching {
|
||||
signFileDetachedEncrypted(file, unlockedKey, encryptionKeys.keyRing())
|
||||
}.getOrElse { throw CryptoException("InputStream cannot be signed.", it) }
|
||||
@@ -753,28 +759,41 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime,
|
||||
): Boolean = verifyMessageDetached(PlainMessage(plainText), signature, publicKey, time.toUtcSeconds())
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean {
|
||||
val plainTextTrimmed = plainText.trimLinesEndIf { trimTrailingSpaces }
|
||||
return verifyMessageDetached(PlainMessage(plainTextTrimmed), signature, publicKey, time.toUtcSeconds())
|
||||
}
|
||||
|
||||
override fun verifyData(
|
||||
data: ByteArray,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): Boolean = verifyMessageDetached(PlainMessage(data), signature, publicKey, time.toUtcSeconds())
|
||||
|
||||
override fun verifyFile(
|
||||
file: DecryptedFile,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): Boolean = verifyFileDetached(file.file, signature, publicKey, time.toUtcSeconds())
|
||||
|
||||
override fun getVerifiedTimestampOfText(
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime
|
||||
): Long? = getVerifiedTimestampMessageDetached(PlainMessage(plainText), signature, publicKey, time.toUtcSeconds())
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Long? {
|
||||
val plainTextTrimmed = plainText.trimLinesEndIf { trimTrailingSpaces }
|
||||
return getVerifiedTimestampMessageDetached(
|
||||
PlainMessage(plainTextTrimmed),
|
||||
signature,
|
||||
publicKey,
|
||||
time.toUtcSeconds()
|
||||
)
|
||||
}
|
||||
|
||||
override fun getVerifiedTimestampOfData(
|
||||
data: ByteArray,
|
||||
@@ -789,20 +808,24 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
): Boolean = verifyMessageDetachedEncrypted(
|
||||
PlainMessage(plainText),
|
||||
encryptedSignature,
|
||||
privateKey,
|
||||
publicKeys,
|
||||
time.toUtcSeconds()
|
||||
)
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean {
|
||||
val plainTextTrimmed = plainText.trimLinesEndIf { trimTrailingSpaces }
|
||||
return verifyMessageDetachedEncrypted(
|
||||
PlainMessage(plainTextTrimmed),
|
||||
encryptedSignature,
|
||||
privateKey,
|
||||
publicKeys,
|
||||
time.toUtcSeconds()
|
||||
)
|
||||
}
|
||||
|
||||
override fun verifyDataEncrypted(
|
||||
data: ByteArray,
|
||||
encryptedSignature: EncryptedSignature,
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): Boolean = verifyMessageDetachedEncrypted(
|
||||
PlainMessage(data),
|
||||
encryptedSignature,
|
||||
@@ -816,7 +839,7 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
encryptedSignature: EncryptedSignature,
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): Boolean = verifyFileDetachedEncrypted(file, encryptedSignature, privateKey, publicKeys, time.toUtcSeconds())
|
||||
|
||||
// endregion
|
||||
@@ -825,11 +848,13 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
override fun getArmored(
|
||||
data: Unarmored,
|
||||
header: PGPHeader,
|
||||
header: PGPHeader
|
||||
): Armored = runCatching {
|
||||
Armor.armorWithType(
|
||||
/* bytes */ data,
|
||||
/* header */ when (header) {
|
||||
/* bytes */
|
||||
data,
|
||||
/* header */
|
||||
when (header) {
|
||||
PGPHeader.Message -> Constants.PGPMessageHeader
|
||||
PGPHeader.Signature -> Constants.PGPSignatureHeader
|
||||
PGPHeader.PublicKey -> Constants.PublicKeyHeader
|
||||
@@ -872,13 +897,11 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
Helper.getJsonSHA256Fingerprints(key).toString(Charsets.UTF_8)
|
||||
}.getOrElse { throw CryptoException("SHA256 Fingerprints cannot be extracted from key.", it) }
|
||||
|
||||
override fun getBase64Encoded(array: ByteArray): String {
|
||||
return Base64.encodeToString(array, Base64.DEFAULT)
|
||||
}
|
||||
override fun getBase64Encoded(array: ByteArray): String =
|
||||
Base64.encodeToString(array, Base64.DEFAULT)
|
||||
|
||||
override fun getBase64Decoded(string: String): ByteArray {
|
||||
return Base64.decode(string, Base64.DEFAULT)
|
||||
}
|
||||
override fun getBase64Decoded(string: String): ByteArray =
|
||||
Base64.decode(string, Base64.DEFAULT)
|
||||
|
||||
override fun getPassphrase(
|
||||
password: ByteArray,
|
||||
@@ -895,9 +918,8 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
// region Public SessionKey/HashKey/PrivateKey/Token generation
|
||||
|
||||
override fun generateNewSessionKey(): SessionKey {
|
||||
return SessionKey(Crypto.generateSessionKey().key)
|
||||
}
|
||||
override fun generateNewSessionKey(): SessionKey =
|
||||
SessionKey(Crypto.generateSessionKey().key)
|
||||
|
||||
override fun generateNewHashKey(): HashKey {
|
||||
val secret = Crypto.randomToken(32)
|
||||
@@ -964,9 +986,13 @@ class GOpenPGPCrypto : PGPCrypto {
|
||||
|
||||
// endregion
|
||||
|
||||
private fun String.trimLinesEndIf(
|
||||
predicate: () -> Boolean
|
||||
): String = if (predicate.invoke()) trimLinesEnd() else this
|
||||
|
||||
companion object {
|
||||
// 32K is usually not far from the optimal buffer size on Android devices.
|
||||
const val DEFAULT_BUFFER_SIZE = 32768
|
||||
const val DEFAULT_BUFFER_SIZE = 32_768
|
||||
|
||||
const val KEY_CACHE_ENABLED = false
|
||||
const val KEY_CACHE_LRU_MAX_SIZE = 100
|
||||
|
||||
@@ -193,7 +193,7 @@ public abstract interface class me/proton/core/crypto/common/pgp/PGPCrypto {
|
||||
public abstract fun getPublicKey (Ljava/lang/String;)Ljava/lang/String;
|
||||
public abstract fun getUnarmored (Ljava/lang/String;)[B
|
||||
public abstract fun getVerifiedTimestampOfData ([BLjava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public abstract fun getVerifiedTimestampOfText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public abstract fun getVerifiedTimestampOfText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public abstract fun isPrivateKey (Ljava/lang/String;)Z
|
||||
public abstract fun isPublicKey (Ljava/lang/String;)Z
|
||||
public abstract fun isValidKey (Ljava/lang/String;)Z
|
||||
@@ -202,8 +202,8 @@ public abstract interface class me/proton/core/crypto/common/pgp/PGPCrypto {
|
||||
public abstract fun signDataEncrypted ([B[BLjava/util/List;)Ljava/lang/String;
|
||||
public abstract fun signFile (Ljava/io/File;[B)Ljava/lang/String;
|
||||
public abstract fun signFileEncrypted (Ljava/io/File;[BLjava/util/List;)Ljava/lang/String;
|
||||
public abstract fun signText (Ljava/lang/String;[B)Ljava/lang/String;
|
||||
public abstract fun signTextEncrypted (Ljava/lang/String;[BLjava/util/List;)Ljava/lang/String;
|
||||
public abstract fun signText (Ljava/lang/String;[BZ)Ljava/lang/String;
|
||||
public abstract fun signTextEncrypted (Ljava/lang/String;[BLjava/util/List;Z)Ljava/lang/String;
|
||||
public abstract fun unlock (Ljava/lang/String;[B)Lme/proton/core/crypto/common/pgp/UnlockedKey;
|
||||
public abstract fun updatePrivateKeyPassphrase (Ljava/lang/String;[B[B)Ljava/lang/String;
|
||||
public abstract fun updateTime (J)V
|
||||
@@ -211,8 +211,8 @@ public abstract interface class me/proton/core/crypto/common/pgp/PGPCrypto {
|
||||
public abstract fun verifyDataEncrypted ([BLjava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public abstract fun verifyFile (Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public abstract fun verifyFileEncrypted (Ljava/io/File;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public abstract fun verifyText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public abstract fun verifyTextEncrypted (Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public abstract fun verifyText (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public abstract fun verifyTextEncrypted (Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/crypto/common/pgp/PGPCrypto$DefaultImpls {
|
||||
@@ -224,13 +224,15 @@ public final class me/proton/core/crypto/common/pgp/PGPCrypto$DefaultImpls {
|
||||
public static synthetic fun generateRandomBytes$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;JILjava/lang/Object;)[B
|
||||
public static synthetic fun getArmored$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;[BLme/proton/core/crypto/common/pgp/PGPHeader;ILjava/lang/Object;)Ljava/lang/String;
|
||||
public static synthetic fun getVerifiedTimestampOfData$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;[BLjava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static synthetic fun signText$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[BZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static synthetic fun signTextEncrypted$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[BLjava/util/List;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static synthetic fun verifyData$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;[BLjava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyDataEncrypted$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;[BLjava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyFile$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyFileEncrypted$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/io/File;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;Ljava/lang/String;[BLjava/util/List;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/crypto/common/pgp/PGPCrypto$KeyType : java/lang/Enum {
|
||||
@@ -267,7 +269,8 @@ public final class me/proton/core/crypto/common/pgp/PGPCryptoOrNullKt {
|
||||
public static final fun lockOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;[B[B)Ljava/lang/String;
|
||||
public static final fun signDataOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;[B[B)Ljava/lang/String;
|
||||
public static final fun signFileOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/io/File;[B)Ljava/lang/String;
|
||||
public static final fun signTextOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[B)Ljava/lang/String;
|
||||
public static final fun signTextOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[BZ)Ljava/lang/String;
|
||||
public static synthetic fun signTextOrNull$default (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[BZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun unlockOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[B)Lme/proton/core/crypto/common/pgp/UnlockedKey;
|
||||
public static final fun updatePrivateKeyPassphraseOrNull (Lme/proton/core/crypto/common/pgp/PGPCrypto;Ljava/lang/String;[B[B)Ljava/lang/String;
|
||||
}
|
||||
@@ -301,6 +304,12 @@ public final class me/proton/core/crypto/common/pgp/SessionKey : java/io/Closeab
|
||||
public fun toString ()Ljava/lang/String;
|
||||
}
|
||||
|
||||
public final class me/proton/core/crypto/common/pgp/TrimStringKt {
|
||||
public static final fun isTrailingSpace (C)Z
|
||||
public static final fun trimLinesEnd (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Ljava/lang/String;
|
||||
public static synthetic fun trimLinesEnd$default (Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
|
||||
}
|
||||
|
||||
public abstract interface class me/proton/core/crypto/common/pgp/UnlockedKey : java/io/Closeable {
|
||||
public abstract fun getValue ()[B
|
||||
}
|
||||
|
||||
@@ -32,4 +32,9 @@ dependencies {
|
||||
implementation(
|
||||
project(Module.kotlinUtil)
|
||||
)
|
||||
testImplementation(
|
||||
junit,
|
||||
`kotlin-test`,
|
||||
mockk
|
||||
)
|
||||
}
|
||||
|
||||
@@ -328,11 +328,16 @@ interface PGPCrypto {
|
||||
/**
|
||||
* Sign [plainText] using [unlockedKey].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [plainText] cannot be signed.
|
||||
*
|
||||
* @see [verifyText]
|
||||
*/
|
||||
fun signText(plainText: String, unlockedKey: Unarmored): Signature
|
||||
fun signText(plainText: String, unlockedKey: Unarmored, trimTrailingSpaces: Boolean = true): Signature
|
||||
|
||||
/**
|
||||
* Sign [data] using [unlockedKey].
|
||||
@@ -355,6 +360,11 @@ interface PGPCrypto {
|
||||
/**
|
||||
* Sign [plainText] using [unlockedKey] and encrypt the signature using [encryptionKeys].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [plainText] cannot be signed.
|
||||
*
|
||||
* @see [verifyTextEncrypted]
|
||||
@@ -362,7 +372,8 @@ interface PGPCrypto {
|
||||
fun signTextEncrypted(
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>
|
||||
encryptionKeys: List<Armored>,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): EncryptedSignature
|
||||
|
||||
/**
|
||||
@@ -395,6 +406,10 @@ interface PGPCrypto {
|
||||
* Verify [signature] of [plainText] is correctly signed using [publicKey].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before verifying the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [signText]
|
||||
*/
|
||||
@@ -402,7 +417,8 @@ interface PGPCrypto {
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean
|
||||
|
||||
/**
|
||||
@@ -438,6 +454,10 @@ interface PGPCrypto {
|
||||
* Returns the timestamp of the signature, or null if the signature is invalid
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before verifying the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [signText]
|
||||
*/
|
||||
@@ -445,7 +465,8 @@ interface PGPCrypto {
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Long?
|
||||
|
||||
/**
|
||||
@@ -468,6 +489,10 @@ interface PGPCrypto {
|
||||
* and then verify it is a valid signature of [plainText] using [publicKeys].
|
||||
*
|
||||
* @param time time for encrypted signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before verifying the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [signTextEncrypted]
|
||||
*/
|
||||
@@ -476,7 +501,8 @@ interface PGPCrypto {
|
||||
encryptedSignature: EncryptedSignature,
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,7 +67,7 @@ fun PGPCrypto.decryptDataOrNull(
|
||||
*/
|
||||
fun PGPCrypto.decryptDataOrNull(
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
sessionKey: SessionKey
|
||||
): ByteArray? = runCatching { decryptData(data, sessionKey) }.getOrNull()
|
||||
|
||||
/**
|
||||
@@ -92,14 +92,20 @@ fun PGPCrypto.decryptSessionKeyOrNull(
|
||||
): SessionKey? = runCatching { decryptSessionKey(keyPacket, unlockedKey) }.getOrNull()
|
||||
|
||||
/**
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return [Signature], or `null` if [plainText] cannot be signed.
|
||||
*
|
||||
* @see [PGPCrypto.signText]
|
||||
*/
|
||||
fun PGPCrypto.signTextOrNull(
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored
|
||||
): Signature? = runCatching { signText(plainText, unlockedKey) }.getOrNull()
|
||||
unlockedKey: Unarmored,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Signature? = runCatching { signText(plainText, unlockedKey, trimTrailingSpaces) }.getOrNull()
|
||||
|
||||
/**
|
||||
* @return [Signature], or `null` if [data] cannot be signed.
|
||||
@@ -204,7 +210,7 @@ fun PGPCrypto.encryptAndSignDataWithCompressionOrNull(
|
||||
fun PGPCrypto.encryptAndSignDataOrNull(
|
||||
data: ByteArray,
|
||||
sessionKey: SessionKey,
|
||||
publicKey: Unarmored,
|
||||
publicKey: Unarmored
|
||||
): DataPacket? = runCatching { encryptAndSignData(data, sessionKey, publicKey) }.getOrNull()
|
||||
|
||||
/**
|
||||
@@ -246,7 +252,7 @@ fun PGPCrypto.decryptAndVerifyDataOrNull(
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): DecryptedData? = runCatching { decryptAndVerifyData(data, sessionKey, publicKeys, time) }.getOrNull()
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Proton Technologies AG
|
||||
* This file is part of Proton AG and ProtonCore.
|
||||
*
|
||||
* ProtonCore is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* ProtonCore is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package me.proton.core.crypto.common.pgp
|
||||
|
||||
fun Char.isTrailingSpace() = when (this) {
|
||||
' ', '\t', '\r' -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
fun String.trimLinesEnd(
|
||||
predicate: (Char) -> Boolean = { it.isTrailingSpace() }
|
||||
): String = split("\n").joinToString("\n") { line -> line.trimEnd(predicate) }
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Proton Technologies AG
|
||||
* This file is part of Proton AG and ProtonCore.
|
||||
*
|
||||
* ProtonCore is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* ProtonCore is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package me.proton.core.crypto.common.pgp
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class TrimStringKtTest {
|
||||
|
||||
@Test
|
||||
fun trimTrailingSpaces() {
|
||||
// given
|
||||
val plainText = "This is a test\n" +
|
||||
"With trailing spaces: \n" +
|
||||
" With leading spaces\n" +
|
||||
"With trailing tabs:\t\t\n" +
|
||||
"\tWith leading tabs\n" +
|
||||
"With trailing carriage returns:\r\n" +
|
||||
"\rWith leading carriage returns\n" +
|
||||
"\t \r With a mix \t\r\n"
|
||||
val expected = "This is a test\n" +
|
||||
"With trailing spaces:\n" +
|
||||
" With leading spaces\n" +
|
||||
"With trailing tabs:\n" +
|
||||
"\tWith leading tabs\n" +
|
||||
"With trailing carriage returns:\n" +
|
||||
"\rWith leading carriage returns\n" +
|
||||
"\t \r With a mix\n"
|
||||
// when
|
||||
val actual = plainText.trimLinesEnd()
|
||||
// then
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -89,17 +89,18 @@ public final class me/proton/core/key/domain/KeyHolderCryptoKt {
|
||||
public static final fun getUnarmored (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;)[B
|
||||
public static final fun getVerifiedTimestampOfData (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfData$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun signData (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[B)Ljava/lang/String;
|
||||
public static final fun signDataEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[BLme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static synthetic fun signDataEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[BLme/proton/core/key/domain/entity/key/PublicKeyRing;ILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signFile (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/io/File;)Ljava/lang/String;
|
||||
public static final fun signFileEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/io/File;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static synthetic fun signFileEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/io/File;Lme/proton/core/key/domain/entity/key/PublicKeyRing;ILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static synthetic fun signTextEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;ILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Z)Ljava/lang/String;
|
||||
public static synthetic fun signText$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Z)Ljava/lang/String;
|
||||
public static synthetic fun signTextEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun useKeys (Lme/proton/core/key/domain/entity/keyholder/KeyHolder;Lme/proton/core/crypto/common/context/CryptoContext;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun useKeysAs (Lme/proton/core/key/domain/entity/keyholder/KeyHolder;Lme/proton/core/crypto/common/context/CryptoContext;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
|
||||
public static final fun verifyData (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
@@ -110,10 +111,10 @@ public final class me/proton/core/key/domain/KeyHolderCryptoKt {
|
||||
public static synthetic fun verifyFile$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyFileEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyFileEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/keyholder/KeyHolderContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/key/domain/PrivateKeyCryptoKt {
|
||||
@@ -138,17 +139,20 @@ public final class me/proton/core/key/domain/PrivateKeyCryptoKt {
|
||||
public static final fun signDataEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signFile (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/io/File;)Ljava/lang/String;
|
||||
public static final fun signFileEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/PrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/PrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Z)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;Z)Ljava/lang/String;
|
||||
public static synthetic fun signText$default (Lme/proton/core/key/domain/entity/key/PrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static synthetic fun signText$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Z)Ljava/lang/String;
|
||||
public static synthetic fun signTextEncrypted$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun unlock (Lme/proton/core/key/domain/entity/key/PrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;)Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;
|
||||
public static final fun unlockOrNull (Lme/proton/core/key/domain/entity/key/PrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;)Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;
|
||||
public static final fun verifyDataEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyDataEncrypted$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyFileEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyFileEncrypted$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/key/PrivateKeyRing;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/key/domain/PublicAddressCryptoKt {
|
||||
@@ -157,13 +161,13 @@ public final class me/proton/core/key/domain/PublicAddressCryptoKt {
|
||||
public static final fun encryptText (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun getVerifiedTimestampOfData (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfData$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun publicKeyRing (Lme/proton/core/key/domain/entity/key/PublicAddress;)Lme/proton/core/key/domain/entity/key/PublicKeyRing;
|
||||
public static final fun verifyData (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyData$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicAddress;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/key/domain/PublicKeyCryptoKt {
|
||||
@@ -173,14 +177,14 @@ public final class me/proton/core/key/domain/PublicKeyCryptoKt {
|
||||
public static final fun fingerprint (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;)Ljava/lang/String;
|
||||
public static final fun getVerifiedTimestampOfData (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfData$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun verifyData (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyData$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyFile (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyFile$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/key/domain/PublicKeyRingCryptoKt {
|
||||
@@ -189,14 +193,14 @@ public final class me/proton/core/key/domain/PublicKeyRingCryptoKt {
|
||||
public static final fun encryptText (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun getVerifiedTimestampOfData (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfData$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun getVerifiedTimestampOfText (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Ljava/lang/Long;
|
||||
public static synthetic fun getVerifiedTimestampOfText$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Ljava/lang/Long;
|
||||
public static final fun verifyData (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyData$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyFile (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyFile$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Lme/proton/core/crypto/common/pgp/DecryptedFile;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyText (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyText$default (Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public final class me/proton/core/key/domain/SessionKeyCryptoKt {
|
||||
@@ -237,14 +241,16 @@ public final class me/proton/core/key/domain/UnlockedPrivateKeyCryptoKt {
|
||||
public static final fun signDataEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signFile (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;)Ljava/lang/String;
|
||||
public static final fun signFileEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;)Ljava/lang/String;
|
||||
public static final fun signText (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Z)Ljava/lang/String;
|
||||
public static synthetic fun signText$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun signTextEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Z)Ljava/lang/String;
|
||||
public static synthetic fun signTextEncrypted$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;ZILjava/lang/Object;)Ljava/lang/String;
|
||||
public static final fun verifyDataEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyDataEncrypted$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;[BLjava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyFileEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyFileEncrypted$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/io/File;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ILjava/lang/Object;)Z
|
||||
public static final fun verifyTextEncrypted (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;Z)Z
|
||||
public static synthetic fun verifyTextEncrypted$default (Lme/proton/core/key/domain/entity/key/UnlockedPrivateKey;Lme/proton/core/crypto/common/context/CryptoContext;Ljava/lang/String;Ljava/lang/String;Lme/proton/core/key/domain/entity/key/PublicKeyRing;Lme/proton/core/crypto/common/pgp/VerificationTime;ZILjava/lang/Object;)Z
|
||||
}
|
||||
|
||||
public abstract class me/proton/core/key/domain/entity/key/ArmoredKey {
|
||||
|
||||
@@ -297,12 +297,17 @@ fun KeyHolderContext.decryptAndVerifyHashKeyOrNull(
|
||||
/**
|
||||
* Sign [text] using [PrivateKeyRing].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [KeyHolderContext.verifyText]
|
||||
*/
|
||||
fun KeyHolderContext.signText(text: String): Signature =
|
||||
privateKeyRing.signText(text)
|
||||
fun KeyHolderContext.signText(text: String, trimTrailingSpaces: Boolean = true): Signature =
|
||||
privateKeyRing.signText(text, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Sign [data] using [PrivateKeyRing].
|
||||
@@ -328,6 +333,11 @@ fun KeyHolderContext.signFile(file: File): Signature =
|
||||
* Sign [text] using [PrivateKeyRing]
|
||||
* and then encrypt the signature with [encryptionKeyRing].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [KeyHolderContext.verifyTextEncrypted]
|
||||
@@ -335,11 +345,12 @@ fun KeyHolderContext.signFile(file: File): Signature =
|
||||
fun KeyHolderContext.signTextEncrypted(
|
||||
text: String,
|
||||
encryptionKeyRing: PublicKeyRing = publicKeyRing,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): EncryptedSignature =
|
||||
privateKeyRing.signTextEncrypted(
|
||||
context,
|
||||
text,
|
||||
encryptionKeyRing
|
||||
encryptionKeyRing,
|
||||
trimTrailingSpaces
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -352,7 +363,7 @@ fun KeyHolderContext.signTextEncrypted(
|
||||
*/
|
||||
fun KeyHolderContext.signDataEncrypted(
|
||||
data: ByteArray,
|
||||
encryptionKeyRing: PublicKeyRing = publicKeyRing,
|
||||
encryptionKeyRing: PublicKeyRing = publicKeyRing
|
||||
): EncryptedSignature =
|
||||
privateKeyRing.signDataEncrypted(
|
||||
context,
|
||||
@@ -370,7 +381,7 @@ fun KeyHolderContext.signDataEncrypted(
|
||||
*/
|
||||
fun KeyHolderContext.signFileEncrypted(
|
||||
file: File,
|
||||
encryptionKeyRing: PublicKeyRing = publicKeyRing,
|
||||
encryptionKeyRing: PublicKeyRing = publicKeyRing
|
||||
): EncryptedSignature =
|
||||
privateKeyRing.signFileEncrypted(
|
||||
context,
|
||||
@@ -383,6 +394,10 @@ fun KeyHolderContext.signFileEncrypted(
|
||||
* and then verify it is a valid signature of [text] using [verificationKeyRing]
|
||||
*
|
||||
* @param time time for [encryptedSignature] validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [KeyHolderContext.signTextEncrypted]
|
||||
*/
|
||||
@@ -390,13 +405,14 @@ fun KeyHolderContext.verifyTextEncrypted(
|
||||
text: String,
|
||||
encryptedSignature: EncryptedSignature,
|
||||
verificationKeyRing: PublicKeyRing = publicKeyRing,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = privateKeyRing.verifyTextEncrypted(
|
||||
context,
|
||||
text,
|
||||
encryptedSignature,
|
||||
verificationKeyRing,
|
||||
time
|
||||
time,
|
||||
trimTrailingSpaces
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -445,6 +461,10 @@ fun KeyHolderContext.verifyFileEncrypted(
|
||||
* Verify [signature] of [text] is correctly signed using [PublicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return true if at least one [PublicKey] verify [signature].
|
||||
*
|
||||
@@ -453,8 +473,9 @@ fun KeyHolderContext.verifyFileEncrypted(
|
||||
fun KeyHolderContext.verifyText(
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Boolean = publicKeyRing.verifyText(context, text, signature, time)
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = publicKeyRing.verifyText(context, text, signature, time, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using [PublicKeyRing].
|
||||
@@ -488,11 +509,14 @@ fun KeyHolderContext.verifyFile(
|
||||
): Boolean =
|
||||
publicKeyRing.verifyFile(context, file, signature, time)
|
||||
|
||||
|
||||
/**
|
||||
* Verify [signature] of [text] is correctly signed using [PublicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return the timestamp if at least one [PublicKey] verify [signature], null otherwise
|
||||
*
|
||||
@@ -501,8 +525,9 @@ fun KeyHolderContext.verifyFile(
|
||||
fun KeyHolderContext.getVerifiedTimestampOfText(
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Long? = publicKeyRing.getVerifiedTimestampOfText(context, text, signature, time)
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Long? = publicKeyRing.getVerifiedTimestampOfText(context, text, signature, time, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using [PublicKeyRing].
|
||||
@@ -851,7 +876,7 @@ fun KeyHolderContext.decryptAndVerifyData(
|
||||
data: DataPacket,
|
||||
keyPacket: KeyPacket,
|
||||
verifyKeyRing: PublicKeyRing = publicKeyRing,
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): DecryptedData = decryptSessionKey(keyPacket).use { sessionKey ->
|
||||
sessionKey.decryptAndVerifyData(context, data, verifyKeyRing.keys.map { it.key }, time)
|
||||
}
|
||||
@@ -871,7 +896,7 @@ fun KeyHolderContext.decryptAndVerifyData(
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
verifyKeyRing: PublicKeyRing = publicKeyRing,
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): DecryptedData =
|
||||
sessionKey.decryptAndVerifyData(context, data, verifyKeyRing.keys.map { it.key }, time)
|
||||
|
||||
|
||||
@@ -127,12 +127,17 @@ fun PrivateKey.decryptDataOrNull(context: CryptoContext, message: EncryptedMessa
|
||||
/**
|
||||
* Sign [text] using this [PrivateKey].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [PublicKey.verifyText]
|
||||
*/
|
||||
fun PrivateKey.signText(context: CryptoContext, text: String): Signature =
|
||||
unlock(context).use { it.signText(context, text) }
|
||||
fun PrivateKey.signText(context: CryptoContext, text: String, trimTrailingSpaces: Boolean = true): Signature =
|
||||
unlock(context).use { it.signText(context, text, trimTrailingSpaces) }
|
||||
|
||||
/**
|
||||
* Sign [data] using this [PrivateKey].
|
||||
@@ -262,12 +267,17 @@ fun PrivateKeyRing.decryptSessionKeyOrNull(keyPacket: KeyPacket): SessionKey? =
|
||||
/**
|
||||
* Sign [text] using primary [UnlockedPrivateKey].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [PublicKeyRing.verifyText]
|
||||
*/
|
||||
fun PrivateKeyRing.signText(text: String): Signature =
|
||||
unlockedPrimaryKey.signText(context, text)
|
||||
fun PrivateKeyRing.signText(text: String, trimTrailingSpaces: Boolean = true): Signature =
|
||||
unlockedPrimaryKey.signText(context, text, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Sign [data] using primary [UnlockedPrivateKey].
|
||||
@@ -293,16 +303,21 @@ fun PrivateKeyRing.signFile(file: File): Signature =
|
||||
* Sign [text] using this [UnlockedPrivateKey]
|
||||
* and then encrypt the signature with [encryptionKeyRing].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [PrivateKeyRing.verifyTextEncrypted]
|
||||
*/
|
||||
fun PrivateKeyRing.signTextEncrypted(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
encryptionKeyRing: PublicKeyRing
|
||||
encryptionKeyRing: PublicKeyRing,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Signature =
|
||||
unlockedPrimaryKey.signTextEncrypted(context, text, encryptionKeyRing)
|
||||
unlockedPrimaryKey.signTextEncrypted(context, text, encryptionKeyRing, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Sign [data] using this [UnlockedPrivateKey]
|
||||
@@ -339,21 +354,26 @@ fun PrivateKeyRing.signFileEncrypted(
|
||||
* and then verify it is a valid signature of [text] using [verificationKeyRing]
|
||||
*
|
||||
* @param time time for [encryptedSignature] validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [PrivateKeyRing.signTextEncrypted]
|
||||
*/
|
||||
fun PrivateKeyRing.verifyTextEncrypted(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
encryptedSignature: Armored,
|
||||
verificationKeyRing: PublicKeyRing,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = unlockedPrimaryKey.verifyTextEncrypted(
|
||||
context,
|
||||
text,
|
||||
encryptedSignature,
|
||||
verificationKeyRing,
|
||||
time
|
||||
time,
|
||||
trimTrailingSpaces
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,6 +35,10 @@ import me.proton.core.key.domain.entity.keyholder.KeyHolderContext
|
||||
* Verify [signature] of [text] is correctly signed using this [PublicAddress.publicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return true if at least one [PublicKey] verify [signature].
|
||||
*
|
||||
@@ -44,8 +48,9 @@ fun PublicAddress.verifyText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Boolean = publicKeyRing().verifyText(context, text, signature, time)
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = publicKeyRing().verifyText(context, text, signature, time, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using this [PublicAddress.publicKeyRing].
|
||||
@@ -67,6 +72,10 @@ fun PublicAddress.verifyData(
|
||||
* Verify [signature] of [text] is correctly signed using this [PublicAddress.publicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return the timestamp of the signature if at least one [PublicKey] verify [signature]. null otherwise
|
||||
*
|
||||
@@ -76,8 +85,9 @@ fun PublicAddress.getVerifiedTimestampOfText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Long? = publicKeyRing().getVerifiedTimestampOfText(context, text, signature, time)
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Long? = publicKeyRing().getVerifiedTimestampOfText(context, text, signature, time, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using this [PublicAddress.publicKeyRing].
|
||||
|
||||
@@ -34,6 +34,10 @@ import me.proton.core.key.domain.entity.key.UnlockedPrivateKey
|
||||
* Verify [signature] of [text] is correctly signed using this [PublicKey].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [PrivateKeyRing.signText]
|
||||
*/
|
||||
@@ -41,8 +45,9 @@ fun PublicKey.verifyText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Boolean = isActive && canVerify && context.pgpCrypto.verifyText(text, signature, key, time)
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = isActive && canVerify && context.pgpCrypto.verifyText(text, signature, key, time, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using this [PublicKey].
|
||||
@@ -77,6 +82,10 @@ fun PublicKey.verifyFile(
|
||||
* return the timestamp if it is, null otherwise.
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @see [PrivateKeyRing.signText]
|
||||
*/
|
||||
@@ -84,9 +93,10 @@ fun PublicKey.getVerifiedTimestampOfText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Long? = if (isActive && canVerify) {
|
||||
context.pgpCrypto.getVerifiedTimestampOfText(text, signature, key, time)
|
||||
context.pgpCrypto.getVerifiedTimestampOfText(text, signature, key, time, trimTrailingSpaces)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -64,6 +64,10 @@ fun PublicKeyRing.encryptSessionKey(context: CryptoContext, sessionKey: SessionK
|
||||
* Verify [signature] of [text] is correctly signed using this [PublicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return true if at least one [PublicKey] verify [signature].
|
||||
*
|
||||
@@ -73,8 +77,9 @@ fun PublicKeyRing.verifyText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
): Boolean = keys.any { it.verifyText(context, text, signature, time) }
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = keys.any { it.verifyText(context, text, signature, time, trimTrailingSpaces) }
|
||||
|
||||
/**
|
||||
* Verify [signature] of [data] is correctly signed using this [PublicKeyRing].
|
||||
@@ -112,19 +117,25 @@ fun PublicKeyRing.verifyFile(
|
||||
* Verify [signature] of [text] is correctly signed using this [PublicKeyRing].
|
||||
*
|
||||
* @param time time for embedded signature validation, default to [VerificationTime.Now].
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @return the timestamp of the signature if at least one [PublicKey] verify [signature]. null otherwise
|
||||
*
|
||||
* @see [PrivateKeyRing.signText]
|
||||
*/
|
||||
|
||||
fun PublicKeyRing.getVerifiedTimestampOfText(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
signature: Signature,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Long? = keys
|
||||
.asSequence()
|
||||
.mapNotNull { key -> key.getVerifiedTimestampOfText(context, text, signature, time) }
|
||||
.mapNotNull { key -> key.getVerifiedTimestampOfText(context, text, signature, time, trimTrailingSpaces) }
|
||||
.firstOrNull()
|
||||
|
||||
/**
|
||||
|
||||
@@ -173,12 +173,17 @@ fun UnlockedPrivateKey.decryptSessionKeyOrNull(context: CryptoContext, keyPacket
|
||||
/**
|
||||
* Sign [text] using this [UnlockedPrivateKey].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [PublicKey.verifyText]
|
||||
*/
|
||||
fun UnlockedPrivateKey.signText(context: CryptoContext, text: String): Signature =
|
||||
context.pgpCrypto.signText(text, unlockedKey.value)
|
||||
fun UnlockedPrivateKey.signText(context: CryptoContext, text: String, trimTrailingSpaces: Boolean = true): Signature =
|
||||
context.pgpCrypto.signText(text, unlockedKey.value, trimTrailingSpaces)
|
||||
|
||||
/**
|
||||
* Sign [data] using this [UnlockedPrivateKey].
|
||||
@@ -204,6 +209,11 @@ fun UnlockedPrivateKey.signFile(context: CryptoContext, file: File): Signature =
|
||||
* Sign [text] using this [UnlockedPrivateKey]
|
||||
* and then encrypt the signature with [encryptionKeyRing].
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @throws [CryptoException] if [text] cannot be signed.
|
||||
*
|
||||
* @see [UnlockedPrivateKey.verifyTextEncrypted]
|
||||
@@ -211,11 +221,13 @@ fun UnlockedPrivateKey.signFile(context: CryptoContext, file: File): Signature =
|
||||
fun UnlockedPrivateKey.signTextEncrypted(
|
||||
context: CryptoContext,
|
||||
text: String,
|
||||
encryptionKeyRing: PublicKeyRing
|
||||
encryptionKeyRing: PublicKeyRing,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): EncryptedSignature = context.pgpCrypto.signTextEncrypted(
|
||||
text,
|
||||
unlockedKey.value,
|
||||
encryptionKeyRing.keys.map { it.key }
|
||||
encryptionKeyRing.keys.map { it.key },
|
||||
trimTrailingSpaces
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -258,6 +270,11 @@ fun UnlockedPrivateKey.signFileEncrypted(
|
||||
* Decrypt [encryptedSignature] using this [UnlockedPrivateKey]
|
||||
* and then verify it is a valid signature of [text] using [verificationKeyRing]
|
||||
*
|
||||
* @param trimTrailingSpaces: If set to true, each line end will be trimmed of all trailing spaces and tabs,
|
||||
* before signing the message.
|
||||
* Trimming trailing spaces used to be the default behavior of the library.
|
||||
* This might be needed in some cases to respect a standard, or to maintain compatibility with old signatures.
|
||||
*
|
||||
* @param time time for [encryptedSignature] validation, default to [VerificationTime.Now].
|
||||
*
|
||||
* @see [UnlockedPrivateKey.signTextEncrypted]
|
||||
@@ -267,13 +284,15 @@ fun UnlockedPrivateKey.verifyTextEncrypted(
|
||||
text: String,
|
||||
encryptedSignature: EncryptedSignature,
|
||||
verificationKeyRing: PublicKeyRing,
|
||||
time: VerificationTime = VerificationTime.Now
|
||||
time: VerificationTime = VerificationTime.Now,
|
||||
trimTrailingSpaces: Boolean = true
|
||||
): Boolean = context.pgpCrypto.verifyTextEncrypted(
|
||||
text,
|
||||
encryptedSignature,
|
||||
unlockedKey.value,
|
||||
verificationKeyRing.keys.map { it.key },
|
||||
time
|
||||
time,
|
||||
trimTrailingSpaces
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -333,7 +352,7 @@ fun UnlockedPrivateKey.lock(
|
||||
isPrimary: Boolean = true,
|
||||
isActive: Boolean = true,
|
||||
canEncrypt: Boolean = true,
|
||||
canVerify: Boolean = true,
|
||||
canVerify: Boolean = true
|
||||
): PrivateKey = passphrase.decrypt(context.keyStoreCrypto).use { decrypted ->
|
||||
context.pgpCrypto.lock(unlockedKey.value, decrypted.array).let {
|
||||
PrivateKey(
|
||||
|
||||
@@ -101,7 +101,7 @@ open class TestCryptoContext : CryptoContext {
|
||||
}
|
||||
|
||||
// Concat text+key for testing purpose.
|
||||
override fun signText(plainText: String, unlockedKey: Unarmored): Signature =
|
||||
override fun signText(plainText: String, unlockedKey: Unarmored, trimTrailingSpaces: Boolean): Signature =
|
||||
"sign([$plainText], with=${unlockedKey.fromByteArray()})"
|
||||
.encryptMessage(unlockedKey)
|
||||
|
||||
@@ -116,9 +116,10 @@ open class TestCryptoContext : CryptoContext {
|
||||
override fun signTextEncrypted(
|
||||
plainText: String,
|
||||
unlockedKey: Unarmored,
|
||||
encryptionKeys: List<Armored>
|
||||
encryptionKeys: List<Armored>,
|
||||
trimTrailingSpaces: Boolean
|
||||
): EncryptedSignature {
|
||||
val signature = signText(plainText, unlockedKey)
|
||||
val signature = signText(plainText, unlockedKey, trimTrailingSpaces)
|
||||
return "encrypt([$signature], with=${encryptionKeys.joinToString(", ")})"
|
||||
.encryptMessage(encryptionKeys.first())
|
||||
}
|
||||
@@ -143,7 +144,8 @@ open class TestCryptoContext : CryptoContext {
|
||||
plainText: String,
|
||||
signature: Signature,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean {
|
||||
val decryptedSignature = signature.decryptMessage(publicKey)
|
||||
return plainText == decryptedSignature.extractMessage()
|
||||
@@ -174,7 +176,8 @@ open class TestCryptoContext : CryptoContext {
|
||||
plainText: String,
|
||||
signature: Armored,
|
||||
publicKey: Armored,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Long? {
|
||||
val decryptedSignature = signature.decryptMessage(publicKey)
|
||||
if (plainText == decryptedSignature.extractMessage()) {
|
||||
@@ -201,7 +204,8 @@ open class TestCryptoContext : CryptoContext {
|
||||
encryptedSignature: EncryptedSignature,
|
||||
privateKey: Unarmored,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime
|
||||
time: VerificationTime,
|
||||
trimTrailingSpaces: Boolean
|
||||
): Boolean = runCatching {
|
||||
val decryptedSignature = encryptedSignature.decryptMessage(privateKey)
|
||||
val signature = decryptedSignature.extractMessage()
|
||||
@@ -237,7 +241,7 @@ open class TestCryptoContext : CryptoContext {
|
||||
|
||||
override fun getEncryptedPackets(message: EncryptedMessage): List<EncryptedPacket> = listOf(
|
||||
EncryptedPacket("keyPacket".toByteArray(), PacketType.Key),
|
||||
EncryptedPacket("dataPacket".toByteArray(), PacketType.Data),
|
||||
EncryptedPacket("dataPacket".toByteArray(), PacketType.Data)
|
||||
)
|
||||
|
||||
override fun decryptText(message: EncryptedMessage, unlockedKey: Unarmored): String =
|
||||
@@ -278,7 +282,8 @@ open class TestCryptoContext : CryptoContext {
|
||||
|
||||
override fun encryptData(data: ByteArray, sessionKey: SessionKey): DataPacket =
|
||||
"BINARY([${data.fromByteArray()}]+${sessionKey.key})"
|
||||
.encryptMessage(sessionKey.key).toByteArray()
|
||||
.encryptMessage(sessionKey.key)
|
||||
.toByteArray()
|
||||
|
||||
override fun encryptFile(source: File, destination: File, sessionKey: SessionKey): EncryptedFile =
|
||||
destination.apply { appendBytes(source.readBytes()) }
|
||||
@@ -301,9 +306,10 @@ open class TestCryptoContext : CryptoContext {
|
||||
"BINARY([${data.fromByteArray()}]+$publicKey+${unlockedKey.fromByteArray()})"
|
||||
.encryptMessage(unlockedKey)
|
||||
|
||||
override fun encryptAndSignData(data: ByteArray, sessionKey: SessionKey, unlockedKey: Unarmored): DataPacket =
|
||||
override fun encryptAndSignData(data: ByteArray, sessionKey: SessionKey, unlockedKey: Unarmored): DataPacket =
|
||||
"BINARY([${data.fromByteArray()}]+${sessionKey.key}+${unlockedKey.fromByteArray()})"
|
||||
.encryptMessage(unlockedKey).toByteArray()
|
||||
.encryptMessage(unlockedKey)
|
||||
.toByteArray()
|
||||
|
||||
override fun encryptAndSignDataWithCompression(
|
||||
data: ByteArray,
|
||||
@@ -354,7 +360,7 @@ open class TestCryptoContext : CryptoContext {
|
||||
data: DataPacket,
|
||||
sessionKey: SessionKey,
|
||||
publicKeys: List<Armored>,
|
||||
time: VerificationTime,
|
||||
time: VerificationTime
|
||||
): DecryptedData = DecryptedData(
|
||||
data.decrypt(sessionKey.key).let { decrypted ->
|
||||
check(String(decrypted).startsWith("BINARY"))
|
||||
@@ -427,17 +433,13 @@ open class TestCryptoContext : CryptoContext {
|
||||
salt: String,
|
||||
modulus: String,
|
||||
serverEphemeral: String
|
||||
): SrpProofs {
|
||||
return SrpProofs(mockk(), mockk(), mockk())
|
||||
}
|
||||
): SrpProofs = SrpProofs(mockk(), mockk(), mockk())
|
||||
|
||||
override fun calculatePasswordVerifier(
|
||||
username: String,
|
||||
password: ByteArray,
|
||||
modulusId: String,
|
||||
modulus: String
|
||||
): Auth {
|
||||
return Auth(mockk(), mockk(), mockk(), mockk())
|
||||
}
|
||||
): Auth = Auth(mockk(), mockk(), mockk(), mockk())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user