1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 19:18:44 +00:00

temporal state (currently tools for files decryption in JS do work

This commit is contained in:
InsanusMokrassar 2021-01-23 18:49:34 +06:00
parent 2f52ad45a4
commit d34deade0d
8 changed files with 237 additions and 1 deletions

View File

@ -16,6 +16,8 @@ micro_utils_version=0.4.21
javax_activation_version=1.1.1
crypto_version=1.0.1
library_group=dev.inmo
library_version=0.32.0

View File

@ -71,12 +71,16 @@ kotlin {
api "javax.activation:activation:$javax_activation_version"
}
}
jsMain {
dependencies {
api npm("crypto", "${crypto_version}")
}
}
jvmTest {
dependencies {
implementation kotlin('test-junit')
}
}
jsTest {
dependencies {
implementation kotlin('test-junit')

View File

@ -0,0 +1,25 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.DownloadFile
import dev.inmo.tgbotapi.requests.get.GetFile
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
expect class DecryptionContext(
key: String
) {
fun ByteArray.decrypt(): ByteArray
}
suspend fun DecryptionContext.decrypt(
file: PassportFile,
bot: TelegramBot
): ByteArray {
return bot.execute(
DownloadFile(
bot.execute(
GetFile(file.fileId)
).filePath
)
).decrypt()
}

View File

@ -0,0 +1,52 @@
package dev.inmo.tgbotapi.passport
import dev.inmo.micro_utils.crypto.decodeBase64
import dev.inmo.tgbotapi.types.passport.EncryptedAndBase64EncodedData
import dev.inmo.tgbotapi.utils.passport.DecryptionContext
import kotlin.test.Test
import kotlin.test.assertEquals
//expect val privateKey: String
//expect val encryptedLoremIpsum: EncryptedAndBase64EncodedData
val privateKey: String = """-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaaMFkmMBi6Vl5
vzy7wQjoZNLN+e9e6Lb/WBekJxfbNMt+EN9Zv7xT9xzfY68DX7v0uGoLcAqS5Mh5
8ELiJbzg4O8dbxkggt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+J7npcKW7P41j1KceDaUArjph2yIELEXCr8qUZzWXZdrw96Te8gLi99c="
class DecryptionTest {
val inputText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
@Test
fun testThatDecryptionIsWorkingCorrectly() {
DecryptionContext(privateKey).apply {
val decrypted = encryptedLoremIpsum.decodeBase64().decrypt().decodeToString()
assertEquals(inputText, decrypted)
}
}
}

View File

@ -0,0 +1,50 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.common.toArrayBuffer
import dev.inmo.micro_utils.common.toByteArray
import dev.inmo.micro_utils.crypto.encodeBase64
import dev.inmo.micro_utils.crypto.encodeBase64String
import org.khronos.webgl.ArrayBuffer
import kotlin.js.Json
import kotlin.js.json
private external interface InternalBuffer {
val buffer: ArrayBuffer
}
private external object Buffer {
fun from(data: String): InternalBuffer
}
private external interface CryptoConstants {
val RSA_PKCS1_OAEP_PADDING: Any
}
private external interface Crypto {
val constants: CryptoConstants
fun privateDecrypt(
key: Json,
data: InternalBuffer
): InternalBuffer
fun privateEncrypt(
key: Json,
data: InternalBuffer
) : InternalBuffer
}
@JsModule("crypto")
@JsNonModule
private external val crypto: Crypto
actual class DecryptionContext actual constructor(key: String) {
private val key = json(
"key" to key,
"padding" to crypto.constants.RSA_PKCS1_OAEP_PADDING
)
actual fun ByteArray.decrypt(): ByteArray {
return crypto.privateDecrypt(
key,
Buffer.from(encodeBase64String())
).buffer.toByteArray()
}
}

View File

@ -0,0 +1,32 @@
package dev.inmo.tgbotapi.passport
import dev.inmo.tgbotapi.types.passport.EncryptedAndBase64EncodedData
//actual val privateKey = """-----BEGIN RSA PRIVATE KEY-----
//MIIEow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0WwK3ZECgYEAwSkdmbQZfPwg19zBF05f
//Ho6SmbMLak7O+/jkerbbBPJxZ+eOpVTrlIs5pOYifImNg7oDz1cKHWR8yikTynN9
//PkcF+R2RFTCAiR0S9d5PQFlzccDjD05Lux5/HZC53VA3cd7sQAOB2XXkr5TLD08N
//yI0I/mskPBL23Hymp3ANZKkCgYAO2fqttCnGjnz9ACcYk5ky+biNRQyMAKv65O3p
//BbwTzXDdBkxLwJb2ajikntEC7ceY56VtrNB/q78mhgKgNcuwS0p/s7wZhAOEQwee
//5LR5p8hSQPPyn2tHXbIQDzs0yKzGUP/LmvY+5oMu81Gkl03VephG3dTWUDN0wPJt
//5/dGMQKBgE++3BNLLV8siedqwjhSrRB/KmpbLPtoOUlRJYBfG+oITgl/d/W2NKOg
//C5GztdiZUKlHsvvcYoAAWrgEE3bKg+D7GI15yzAjQQZH6AOgL+CEnunIHBaxEEgG
//RS39yB/kPcn9ZtvcJd9v8myQnQjhco8Z2RUwb6IUIoOBvZlSGhGe
//-----END RSA PRIVATE KEY-----"""
//actual val encryptedLoremIpsum: EncryptedAndBase64EncodedData = "KeuPVezTbMW4MvOGJFTAt34PRaL9iepGb1g4QEi5EQYjKXeXo7RytFOCVx6pW9O/uoMWhl+cX32kNqJVWyK7a22kuhnNT2+aiXqEpuh+madx+LK1zUvGDR1A1Mrf3fFOugcnadCQKICiqvl9cFdqsBkJQOFEs9qj5wiu1F57kekuHLWI7ZnHOojkRNq7l1aCHL0DxcLCfZW5CtWAi8g/zUE5WnGd+vUZ+hqc1vnehDul8JE8YUQbAiIxetzba9XoWouTHYZRZcNCztDbrRBYnq2UCcI5adEwQ3VNcES0lIjRuwn1BBWpvk7VOjqh+4c2tSebDX5AkqrO8XwQYwo8OwvZF+hUXFRK6QLHd4B1JQIdygCaEODG0X353upiEKJXDBqv/ZMXR9KqYZBZEfu48ZM/J6heNjVSOP4TSDrXywJgIOieu8mrQ4d7Or7Twnsu/B+bqS37PvVlfU4wHLl7ehXFj4Kusat6cIrb0R4F/Y3fL4+FcfEsk4ioEAndYBFrc1S11vo/TkcuFnXtqrr181gC5JD5LtsUH6sst36vE/JdL/UwTGqhu+rDUcgvr7FiunkasWBnzCtFs58JJrRycUKPzyKINS16GkY8yYtp7xJXBGPYOFM+J7npcKW7P41j1KceDaUArjph2yIELEXCr8qUZzWXZdrw96Te8gLi99c="

View File

@ -0,0 +1,40 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.decodeBase64
import sun.security.rsa.RSAPublicKeyImpl
import java.security.KeyFactory
import java.security.interfaces.RSAPrivateKey
import java.security.spec.PKCS8EncodedKeySpec
import javax.crypto.Cipher
private val regexToRemoveFromKey = Regex("(-----(BEGIN|END) ((?:.*? KEY)|CERTIFICATE)-----|[\\s])")
private fun String.adaptKey() {
val replaced = replace(regexToRemoveFromKey, "")
}
/**
* @param key PKCS8
*/
actual class DecryptionContext actual constructor(key: String) {
private val privateKey: RSAPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(
PKCS8EncodedKeySpec(key.replace(regexToRemoveFromKey, "").decodeBase64())
) as RSAPrivateKey
private val chunkSize: Int = privateKey.modulus.bitLength() / 8
actual fun ByteArray.decrypt(): ByteArray {
return Cipher.getInstance("RSA/ECB/PKCS1Padding").run {
init(Cipher.DECRYPT_MODE, privateKey)
(0 until size step chunkSize).flatMap {
val firstIndex = it
val lastIndexExclusive = if (it + chunkSize > size) {
size
} else {
it + chunkSize
}
doFinal(copyOfRange(firstIndex, lastIndexExclusive)).toList()
}.toByteArray()
}
}
}

View File

@ -0,0 +1,31 @@
package dev.inmo.tgbotapi.passport
//actual val privateKey: String = """-----BEGIN PRIVATE KEY-----
//MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaaMFkmMBi6Vl5
//vzy7wQjoZNLN+e9e6Lb/WBekJxfbNMt+EN9Zv7xT9xzfY68DX7v0uGoLcAqS5Mh5
//8ELiJbzg4O8dbxkggt0G+kxcbT+a3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