Skip to content

Commit 375b817

Browse files
authored
kn: checksum bindings (#121)
1 parent f059811 commit 375b817

File tree

8 files changed

+128
-7
lines changed

8 files changed

+128
-7
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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
6+
7+
/**
8+
* A mixin class used to ensure CRT is initialized before the class is invoked
9+
*/
10+
public open class WithCrt {
11+
init {
12+
CRT.initRuntime { }
13+
}
14+
}

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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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+
7+
import aws.sdk.kotlin.crt.WithCrt
58
import kotlinx.cinterop.*
69
import libcrt.aws_checksums_crc32
710
import libcrt.aws_checksums_crc32c
@@ -21,6 +24,10 @@ internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
2124
private var crc = 0U
2225

2326
override fun update(input: ByteArray, offset: Int, length: Int) {
27+
if (input.isEmpty() || length == 0) {
28+
return
29+
}
30+
2431
val offsetInput = input.usePinned {
2532
it.addressOf(offset)
2633
}
@@ -42,7 +49,9 @@ internal class Crc(val checksumFn: AwsChecksumsCrcFunction) : HashFunction {
4249
/**
4350
* A CRC32 [HashFunction] implemented using bindings to CRT.
4451
*/
45-
public class Crc32 : HashFunction {
52+
public class Crc32 :
53+
WithCrt(),
54+
HashFunction {
4655
private val crc32 = Crc(::aws_checksums_crc32)
4756
override fun update(input: ByteArray, offset: Int, length: Int) {
4857
crc32.update(input, offset, length)
@@ -56,7 +65,9 @@ public class Crc32 : HashFunction {
5665
/**
5766
* A CRC32C [HashFunction] implemented using bindings to CRT.
5867
*/
59-
public class Crc32c : HashFunction {
68+
public class Crc32c :
69+
WithCrt(),
70+
HashFunction {
6071
private val crc32c = Crc(::aws_checksums_crc32c)
6172
override fun update(input: ByteArray, offset: Int, length: Int) {
6273
crc32c.update(input, offset, length)

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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.WithCrt
9+
import aws.sdk.kotlin.crt.awsAssertOpSuccess
10+
import kotlinx.cinterop.addressOf
11+
import kotlinx.cinterop.cValue
12+
import kotlinx.cinterop.convert
13+
import kotlinx.cinterop.pointed
14+
import kotlinx.cinterop.reinterpret
15+
import kotlinx.cinterop.usePinned
16+
import libcrt.aws_byte_buf
17+
import libcrt.aws_byte_cursor_from_array
18+
import libcrt.aws_hash_destroy
19+
import libcrt.aws_hash_finalize
20+
import libcrt.aws_hash_update
21+
import libcrt.aws_md5_new
22+
23+
/**
24+
* MD5 hash function implemented using bindings to CRT
25+
*/
26+
public class Md5 :
27+
WithCrt(),
28+
HashFunction {
29+
private var md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
30+
31+
override fun update(input: ByteArray, offset: Int, length: Int) {
32+
if (input.isEmpty() || length == 0) {
33+
return
34+
}
35+
36+
val inputCursor = input.usePinned {
37+
aws_byte_cursor_from_array(it.addressOf(offset), length.convert())
38+
}
39+
40+
awsAssertOpSuccess(aws_hash_update(md5, inputCursor)) {
41+
"aws_hash_update"
42+
}
43+
}
44+
45+
override fun digest(): ByteArray {
46+
val output = ByteArray(md5.pointed.digest_size.toInt())
47+
48+
val byteBuf = output.usePinned {
49+
cValue<aws_byte_buf> {
50+
capacity = output.size.convert()
51+
len = 0U
52+
buffer = it.addressOf(0).reinterpret()
53+
}
54+
}
55+
56+
awsAssertOpSuccess(aws_hash_finalize(md5, byteBuf, 0U)) { "aws_hash_finalize" }
57+
58+
return output.also { reset() }
59+
}
60+
61+
override fun reset() {
62+
aws_hash_destroy(md5)
63+
md5 = checkNotNull(aws_md5_new(Allocator.Default)) { "aws_md5_new" }
64+
}
65+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
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
8+
import aws.sdk.kotlin.crt.WithCrt
69
import aws.sdk.kotlin.crt.awsAssertOpSuccess
710
import kotlinx.cinterop.*
811
import libcrt.*
@@ -17,7 +20,9 @@ internal typealias InitializeHashFn = (
1720
/**
1821
* SHA-1 hash function implemented using bindings to CRT
1922
*/
20-
public class Sha1 : HashFunction {
23+
public class Sha1 :
24+
WithCrt(),
25+
HashFunction {
2126
private val sha1 = Sha(::aws_sha1_new)
2227
override fun update(input: ByteArray, offset: Int, length: Int) {
2328
sha1.update(input, offset, length)
@@ -31,7 +36,9 @@ public class Sha1 : HashFunction {
3136
/**
3237
* SHA-256 hash function implemented using bindings to CRT
3338
*/
34-
public class Sha256 : HashFunction {
39+
public class Sha256 :
40+
WithCrt(),
41+
HashFunction {
3542
private val sha256 = Sha(::aws_sha256_new)
3643
override fun update(input: ByteArray, offset: Int, length: Int) {
3744
sha256.update(input, offset, length)
@@ -51,6 +58,10 @@ internal class Sha(val initializeFn: InitializeHashFn) : HashFunction {
5158

5259
// aws_hash_update
5360
override fun update(input: ByteArray, offset: Int, length: Int) {
61+
if (input.isEmpty() || length == 0) {
62+
return
63+
}
64+
5465
val inputCursor = input.usePinned {
5566
aws_byte_cursor_from_array(it.addressOf(offset), length.convert())
5667
}

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

Lines changed: 19 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
@@ -48,4 +50,21 @@ class HashFunctionTest {
4850
assertEquals(expected, hash.digest().encodeToHex())
4951
}
5052
}
53+
54+
@Test
55+
fun testEmptyUpdate() {
56+
// algorithm -> hash("")
57+
val tests = listOf(
58+
(Sha1() to "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
59+
(Sha256() to "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
60+
(Crc32() to "00000000"),
61+
(Crc32c() to "00000000"),
62+
)
63+
64+
tests.forEach { (hash, expected) ->
65+
val data = "".encodeToByteArray()
66+
hash.update(data, 0, 0)
67+
assertEquals(expected, hash.digest().encodeToHex())
68+
}
69+
}
5170
}

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.16-kn"
55

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

0 commit comments

Comments
 (0)