create crypto project

This commit is contained in:
InsanusMokrassar 2021-01-23 11:43:40 +06:00
parent d074f29b82
commit a4020cb484
13 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package dev.inmo.micro_utils.common
import org.khronos.webgl.*
fun DataView.toByteArray() = ByteArray(this.byteLength) {
getInt8(it)
}
fun ArrayBuffer.toByteArray() = Int8Array(this) as ByteArray
fun ByteArray.toDataView() = DataView(ArrayBuffer(size)).also {
forEachIndexed { i, byte -> it.setInt8(i, byte) }
}
fun ByteArray.toArrayBuffer() = toDataView().buffer

View File

@ -0,0 +1,10 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.await
import org.khronos.webgl.Int8Array
import org.w3c.fetch.Response
import org.w3c.files.Blob
suspend fun Blob.toByteArray() = Int8Array(
Response(this).arrayBuffer().await()
) as ByteArray

30
crypto/build.gradle Normal file
View File

@ -0,0 +1,30 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":micro_utils.common")
}
}
jsMain {
dependencies {
api npm("crypto-js", "$crypto_js_version")
}
}
}
}
android {
sourceSets {
main {
java.srcDirs += [ "src/jvmMain/kotlin" ]
}
}
}

View File

@ -0,0 +1,77 @@
package dev.inmo.micro_utils.crypto
import kotlin.experimental.and
private const val BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
private const val BASE64_MASK: Byte = 0x3f
private const val BASE64_PAD = '='
private val BASE64_INVERSE_ALPHABET = IntArray(256) {
BASE64_ALPHABET.indexOf(it.toChar())
}
internal fun Int.toBase64(): Char = BASE64_ALPHABET[this]
internal fun Byte.fromBase64(): Byte = BASE64_INVERSE_ALPHABET[toInt() and 0xff].toByte() and BASE64_MASK
typealias EncodedBase64String = String
typealias EncodedByteArray = ByteArray
fun SourceString.encodeBase64String(): EncodedBase64String = encodeToByteArray().encodeBase64String()
fun SourceString.encodeBase64(): EncodedByteArray = encodeToByteArray().encodeBase64()
fun SourceBytes.encodeBase64String(): EncodedBase64String = buildString {
var i = 0
while (this@encodeBase64String.size > i) {
val read = kotlin.math.min(3, this@encodeBase64String.size - i)
val data = ByteArray(3) {
if (it < read) {
this@encodeBase64String[it + i]
} else {
0
}
}
val padSize = (data.size - read) * 8 / 6
val chunk = ((data[0].toInt() and 0xFF) shl 16) or
((data[1].toInt() and 0xFF) shl 8) or
(data[2].toInt() and 0xFF)
for (index in data.size downTo padSize) {
val char = (chunk shr (6 * index)) and BASE64_MASK.toInt()
append(char.toBase64())
}
repeat(padSize) { append(BASE64_PAD) }
i += read
}
}
fun SourceBytes.encodeBase64(): EncodedByteArray = encodeBase64String().encodeToByteArray()
fun EncodedBase64String.decodeBase64(): SourceBytes = dropLastWhile { it == BASE64_PAD }.encodeToByteArray().decodeBase64()
fun EncodedBase64String.decodeBase64String(): SourceString = decodeBase64().decodeToString()
fun EncodedByteArray.decodeBase64(): SourceBytes {
val result = mutableListOf<Byte>()
val data = ByteArray(4)
(0 until size step 4).forEach { i ->
var read = 0
for (j in 0 until 4) {
if (j + i < size) {
data[j] = get(j + i)
read++
} else {
break
}
}
val chunk = data.foldIndexed(0) { index, result, current ->
result or (current.fromBase64().toInt() shl ((3 - index) * 6))
}
for (index in data.size - 2 downTo (data.size - read)) {
val origin = (chunk shr (8 * index)) and 0xff
result.add(origin.toByte())
}
}
return result.toByteArray()
}
fun EncodedByteArray.decodeBase64String(): SourceString = decodeBase64().decodeToString()

View File

@ -0,0 +1,11 @@
package dev.inmo.micro_utils.crypto
/**
* This typealias shows that corresponding [ByteArray] is an raw data
*/
typealias SourceBytes = ByteArray
/**
* This typealias shows that corresponding [String] is an raw data
*/
typealias SourceString = String

View File

@ -0,0 +1,6 @@
package dev.inmo.micro_utils.crypto
typealias MD5 = String
expect fun SourceBytes.md5(): MD5
fun SourceString.md5(): MD5 = encodeToByteArray().md5()

View File

@ -0,0 +1,17 @@
package dev.inmo.micro_utils.crypto
import kotlin.test.Test
import kotlin.test.assertEquals
class MD5Test {
val source = "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."
val expectedMD5 = "db89bb5ceab87f9c0fcc2ab36c189c2c"
@Test
fun testMD5() {
var i = 0
source.md5().forEach {
assertEquals(expectedMD5[i], it)
i++
}
}
}

View File

@ -0,0 +1,9 @@
package dev.inmo.micro_utils.crypto
external interface CryptoJs {
fun MD5(data: String): String
}
@JsModule("crypto-js")
@JsNonModule
external val CryptoJS: CryptoJs

View File

@ -0,0 +1,3 @@
package dev.inmo.micro_utils.crypto
actual fun SourceBytes.md5(): MD5 = CryptoJS.MD5(decodeToString())

View File

@ -0,0 +1,9 @@
package dev.inmo.micro_utils.crypto
import java.math.BigInteger
import java.security.MessageDigest
actual fun SourceBytes.md5(): MD5 = BigInteger(
1,
MessageDigest.getInstance("MD5").digest(this)
).toString(16)

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.crypto"/>

View File

@ -33,6 +33,10 @@ junit_version=4.12
test_ext_junit_version=1.1.2
espresso_core=3.3.0
# JS NPM
crypto_js_version=4.0.0
# Dokka
dokka_version=1.4.20

View File

@ -2,6 +2,7 @@ rootProject.name='micro_utils'
String[] includes = [
":common",
":crypto",
":selector:common",
":pagination:common",
":pagination:exposed",