From d76c09ffb2e7f67cf47c6bee3f96296aaa118e35 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 25 Jun 2020 15:45:15 +0600 Subject: [PATCH] start implement passport --- .../TelegramBotAPI/types/Common.kt | 4 ++++ .../types/passport/credentials/Credentials.kt | 5 +++++ .../credentials/common/FileCredentials.kt | 6 ++++++ .../passport/raw/EncryptedCredentials.kt | 15 ++++++++++++++ .../utils/crypto/DecryptSecret.kt | 11 ++++++++++ .../TelegramBotAPI/utils/crypto/Decryptor.kt | 20 +++++++++++++++++++ .../TelegramBotAPI/utils/crypto/SHA512.kt | 3 +++ 7 files changed, 64 insertions(+) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/Credentials.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/common/FileCredentials.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/raw/EncryptedCredentials.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/DecryptSecret.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/Decryptor.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/SHA512.kt diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 2fb043f7bd..9793a62fe6 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -264,6 +264,10 @@ const val permissionsField = "permissions" const val typeField = "type" const val valueField = "value" +const val dataField = "data" +const val hashField = "hash" +const val secretField = "secret" + const val pointField = "point" const val xShiftField = "x_shift" const val yShiftField = "y_shift" diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/Credentials.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/Credentials.kt new file mode 100644 index 0000000000..9eecfdc567 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/Credentials.kt @@ -0,0 +1,5 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.passport.credentials + +data class Credentials( + +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/common/FileCredentials.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/common/FileCredentials.kt new file mode 100644 index 0000000000..c8ad78354c --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/credentials/common/FileCredentials.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.passport.credentials.common + +data class FileCredentials( + val fileHash: String, + val secret: String +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/raw/EncryptedCredentials.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/raw/EncryptedCredentials.kt new file mode 100644 index 0000000000..bef806c970 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/passport/raw/EncryptedCredentials.kt @@ -0,0 +1,15 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.passport.raw + +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class EncryptedCredentials( + @SerialName(dataField) + val data: String, + @SerialName(hashField) + val hash: String, + @SerialName(secretField) + val secret: String +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/DecryptSecret.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/DecryptSecret.kt new file mode 100644 index 0000000000..956235fb93 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/DecryptSecret.kt @@ -0,0 +1,11 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils.crypto + +import com.soywiz.krypto.AES +import com.soywiz.krypto.Padding + +fun decryptSecret( + privateKey: ByteArray, + encryptedSecret: ByteArray +): ByteArray { + AES.decryptAes128Cbc(privateKey, Padding.PKCS7Padding) +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/Decryptor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/Decryptor.kt new file mode 100644 index 0000000000..4976a736be --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/Decryptor.kt @@ -0,0 +1,20 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils.crypto + +import com.soywiz.krypto.AES +import com.soywiz.krypto.Padding + +typealias Decryptor = (ByteArray) -> ByteArray + +fun createDecryptor( + secret: ByteArray, + hash: ByteArray +): Decryptor { + val secretHash = sha512(secret + hash) + val key = secretHash.copyOfRange(0, 32) + val iv = secretHash.copyOfRange(32, 48) + + return { + val decrypted = AES.decryptAesCbc(it, key, iv, Padding.NoPadding) + decrypted.copyOfRange(decrypted[0].toInt(), decrypted.size) + } +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/SHA512.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/SHA512.kt new file mode 100644 index 0000000000..89cf99416c --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/crypto/SHA512.kt @@ -0,0 +1,3 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils.crypto + +external fun sha512(from: ByteArray): ByteArray