Skip to content

Commit 63ac5a4

Browse files
committed
Add package to checksum class files and implement MD5
1 parent f059811 commit 63ac5a4

File tree

8 files changed

+69
-4
lines changed

8 files changed

+69
-4
lines changed

aws-crt-kotlin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ kotlin {
100100
// disable "standalone" mode in simulator tests since it causes TLS issues. this means we need to manage the simulator
101101
// ourselves (booting / shutting down). FIXME: https://youtrack.jetbrains.com/issue/KT-38317
102102
kotlin {
103-
val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15"
103+
val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 16"
104104

105105
val xcrun = "/usr/bin/xcrun"
106106

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/DigestNative.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
65
package aws.sdk.kotlin.crt.util
76

8-
import Sha256
7+
import aws.sdk.kotlin.crt.util.hashing.Sha256
98

109
/**
1110
* Utility object for various hash functions

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/CrcNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
package aws.sdk.kotlin.crt.util.hashing
6+
57
import kotlinx.cinterop.*
68
import libcrt.aws_checksums_crc32
79
import libcrt.aws_checksums_crc32c

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/HashFunctionNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
package aws.sdk.kotlin.crt.util.hashing
6+
57
/**
68
* A function which calculates the hash of given data
79
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.crt.util.hashing
6+
7+
import aws.sdk.kotlin.crt.Allocator
8+
import aws.sdk.kotlin.crt.awsAssertOpSuccess
9+
import kotlinx.cinterop.addressOf
10+
import kotlinx.cinterop.cValue
11+
import kotlinx.cinterop.convert
12+
import kotlinx.cinterop.pointed
13+
import kotlinx.cinterop.reinterpret
14+
import kotlinx.cinterop.usePinned
15+
import libcrt.aws_byte_buf
16+
import libcrt.aws_byte_cursor_from_array
17+
import libcrt.aws_hash_destroy
18+
import libcrt.aws_hash_finalize
19+
import libcrt.aws_hash_update
20+
import libcrt.aws_md5_new
21+
22+
/**
23+
* MD5 hash function implemented using bindings to CRT
24+
*/
25+
public class Md5 : HashFunction {
26+
private var md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
27+
28+
override fun update(input: ByteArray, offset: Int, length: Int) {
29+
val inputCursor = input.usePinned {
30+
aws_byte_cursor_from_array(it.addressOf(offset), length.convert())
31+
}
32+
33+
awsAssertOpSuccess(aws_hash_update(md5, inputCursor)) {
34+
"aws_hash_update"
35+
}
36+
}
37+
38+
override fun digest(): ByteArray {
39+
val output = ByteArray(md5.pointed.digest_size.toInt())
40+
41+
val byteBuf = output.usePinned {
42+
cValue<aws_byte_buf> {
43+
capacity = output.size.convert()
44+
len = 0U
45+
buffer = it.addressOf(0).reinterpret()
46+
}
47+
}
48+
49+
awsAssertOpSuccess(aws_hash_finalize(md5, byteBuf, 0U)) { "aws_hash_finalize" }
50+
51+
return output.also { reset() }
52+
}
53+
54+
override fun reset() {
55+
aws_hash_destroy(md5)
56+
md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
57+
}
58+
}

aws-crt-kotlin/native/src/aws/sdk/kotlin/crt/util/hashing/ShaNative.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
package aws.sdk.kotlin.crt.util.hashing
6+
57
import aws.sdk.kotlin.crt.Allocator
68
import aws.sdk.kotlin.crt.awsAssertOpSuccess
79
import kotlinx.cinterop.*

aws-crt-kotlin/native/test/aws/sdk/kotlin/crt/util/hashing/HashFunctionNativeTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
package aws.sdk.kotlin.crt.util.hashing
6+
57
import aws.sdk.kotlin.crt.util.encodeToHex
68
import kotlin.test.Test
79
import kotlin.test.assertEquals

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
kotlin-version = "2.0.21"
33

4-
aws-kotlin-repo-tools-version = "0.4.14-kn"
4+
aws-kotlin-repo-tools-version = "0.4.15-kn"
55

66
# libs
77
crt-java-version = "0.31.3"

0 commit comments

Comments
 (0)