Skip to content

Commit 19dc965

Browse files
authored
Add HmacSHA3-* and HmacKeccak* Implementations (#21)
1 parent c567ed6 commit 19dc965

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1269
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
Message Authentication Code algorithms for Kotlin Multiplatform
2727

28-
If you are looking for hashing algorithms (e.g. `SHA-256`, `SHA-512`, etc), see the [hash repo][url-hash].
28+
If you are looking for hashing algorithms (e.g. `SHA-256`, `SHA3-256`, etc), see the [hash repo][url-hash].
2929

3030
If you are looking for `Encoding` (`Base16` a.k.a. `hex`, `Base32`, `Base64`, etc), see the [encoding repo][url-encoding].
3131

gradle/libs.versions.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[versions]
22
binaryCompat = "0.13.0"
3+
bouncyCastle = "1.72"
34
configuration = "0.1.0-beta02"
4-
cryptoCore = "0.2.0"
5-
cryptoHash = "0.2.1"
5+
cryptoCore = "0.2.2"
6+
cryptoHash = "0.2.2"
67
encoding = "1.2.1"
78
gradleVersions = "0.46.0"
89
kotlin = "1.8.10"
@@ -11,14 +12,17 @@ publish = "0.24.0"
1112
[libraries]
1213
kotlincrypto-core-digest = { module = "org.kotlincrypto.core:digest", version.ref = "cryptoCore" }
1314
kotlincrypto-core-mac = { module = "org.kotlincrypto.core:mac", version.ref = "cryptoCore" }
15+
kotlincrypto-core-xof = { module = "org.kotlincrypto.core:xof", version.ref = "cryptoCore" }
1416
kotlincrypto-hash-md5 = { module = "org.kotlincrypto.hash:md5", version.ref = "cryptoHash" }
1517
kotlincrypto-hash-sha1 = { module = "org.kotlincrypto.hash:sha1", version.ref = "cryptoHash" }
1618
kotlincrypto-hash-sha2 = { module = "org.kotlincrypto.hash:sha2", version.ref = "cryptoHash" }
19+
kotlincrypto-hash-sha3 = { module = "org.kotlincrypto.hash:sha3", version.ref = "cryptoHash" }
1720
gradle-kmp-configuration = { module = "io.matthewnelson:gradle-kmp-configuration-plugin", version.ref = "configuration" }
1821
gradle-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1922
gradle-maven-publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "publish" }
2023

2124
# Tests
25+
bouncyCastle = { module = "org.bouncycastle:bcprov-ext-jdk15to18", version.ref = "bouncyCastle" }
2226
encoding-base16 = { module = "io.matthewnelson.kotlin-components:encoding-base16", version.ref = "encoding" }
2327
encoding-base64 = { module = "io.matthewnelson.kotlin-components:encoding-base64", version.ref = "encoding" }
2428

library/hmac/hmac-md5/src/commonMain/kotlin/org/kotlincrypto/macs/HmacMD5.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacMD5: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacMD5", MD5())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacMD5(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacMD5(engineCopy)
4242
}

library/hmac/hmac-sha1/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA1.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacSHA1: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacSHA1", SHA1())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA1(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA1(engineCopy)
4242
}

library/hmac/hmac-sha2/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA224.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacSHA224: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacSHA224", SHA224())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA224(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA224(engineCopy)
4242
}

library/hmac/hmac-sha2/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA256.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacSHA256: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacSHA256", SHA256())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA256(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA256(engineCopy)
4242
}

library/hmac/hmac-sha2/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA384.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacSHA384: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacSHA384", SHA384())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA384(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA384(engineCopy)
4242
}

library/hmac/hmac-sha2/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA512.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HmacSHA512: Hmac {
3636
public constructor(key: ByteArray): super(key, "HmacSHA512", SHA512())
3737

3838
@OptIn(InternalKotlinCryptoApi::class)
39-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
39+
private constructor(engine: Mac.Engine): super(engine)
4040

41-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA512(algorithm(), engineCopy as Engine)
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA512(engineCopy)
4242
}

library/hmac/hmac-sha2/src/commonMain/kotlin/org/kotlincrypto/macs/HmacSHA512t.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class HmacSHA512t: Hmac {
5959
public constructor(key: ByteArray, t: Int): super(key, "HmacSHA512/$t", SHA512t(t))
6060

6161
@OptIn(InternalKotlinCryptoApi::class)
62-
private constructor(algorithm: String, engine: Engine): super(algorithm, engine)
62+
private constructor(engine: Mac.Engine): super(engine)
6363

64-
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA512t(algorithm(), engineCopy as Engine)
64+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacSHA512t(engineCopy)
6565
}

library/hmac/hmac-sha3/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public final class org/kotlincrypto/macs/HmacKeccak224 : org/kotlincrypto/macs/Hmac {
2+
public fun <init> ([B)V
3+
}
4+
5+
public final class org/kotlincrypto/macs/HmacKeccak256 : org/kotlincrypto/macs/Hmac {
6+
public fun <init> ([B)V
7+
}
8+
9+
public final class org/kotlincrypto/macs/HmacKeccak384 : org/kotlincrypto/macs/Hmac {
10+
public fun <init> ([B)V
11+
}
12+
13+
public final class org/kotlincrypto/macs/HmacKeccak512 : org/kotlincrypto/macs/Hmac {
14+
public fun <init> ([B)V
15+
}
16+
17+
public final class org/kotlincrypto/macs/HmacSHA3_224 : org/kotlincrypto/macs/Hmac {
18+
public fun <init> ([B)V
19+
}
20+
21+
public final class org/kotlincrypto/macs/HmacSHA3_256 : org/kotlincrypto/macs/Hmac {
22+
public fun <init> ([B)V
23+
}
24+
25+
public final class org/kotlincrypto/macs/HmacSHA3_384 : org/kotlincrypto/macs/Hmac {
26+
public fun <init> ([B)V
27+
}
28+
29+
public final class org/kotlincrypto/macs/HmacSHA3_512 : org/kotlincrypto/macs/Hmac {
30+
public fun <init> ([B)V
31+
}
32+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
plugins {
17+
id("configuration")
18+
id("bom-include")
19+
}
20+
21+
kmpConfiguration {
22+
configureShared(publish = true) {
23+
common {
24+
sourceSetMain {
25+
dependencies {
26+
implementation(libs.kotlincrypto.hash.sha3)
27+
api(project(":library:hmac:hmac"))
28+
}
29+
}
30+
sourceSetTest {
31+
dependencies {
32+
implementation(project(":tools:testing"))
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2023 Matthew Nelson
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
POM_ARTIFACT_ID=hmac-sha3
15+
POM_NAME=KotlinCrypto HmacSHA3/HmacKeccak
16+
POM_DESCRIPTION=HmacSHA3-224, HmacSHA3-256, HmacSHA3-384, HmacSHA3-512, HmacKeccak224, HmacKeccak256, HmacKeccak384, HmacKeccak512 Message Authentication
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("ClassName", "UnnecessaryOptInAnnotation")
17+
18+
package org.kotlincrypto.macs
19+
20+
import org.kotlincrypto.core.InternalKotlinCryptoApi
21+
import org.kotlincrypto.core.Mac
22+
import org.kotlincrypto.hash.sha3.Keccak224
23+
24+
/**
25+
* HmacKeccak224 implementation
26+
* */
27+
public class HmacKeccak224: Hmac {
28+
29+
/**
30+
* Instantiates a new instance of [HmacKeccak224].
31+
*
32+
* @throws [IllegalArgumentException] if [key] is empty.
33+
* */
34+
@OptIn(InternalKotlinCryptoApi::class)
35+
@Throws(IllegalArgumentException::class)
36+
public constructor(key: ByteArray): super(key, "HmacKeccak224", Keccak224())
37+
38+
@OptIn(InternalKotlinCryptoApi::class)
39+
private constructor(engine: Mac.Engine): super(engine)
40+
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacKeccak224(engineCopy)
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("ClassName", "UnnecessaryOptInAnnotation")
17+
18+
package org.kotlincrypto.macs
19+
20+
import org.kotlincrypto.core.InternalKotlinCryptoApi
21+
import org.kotlincrypto.core.Mac
22+
import org.kotlincrypto.hash.sha3.Keccak256
23+
24+
/**
25+
* HmacKeccak256 implementation
26+
* */
27+
public class HmacKeccak256: Hmac {
28+
29+
/**
30+
* Instantiates a new instance of [HmacKeccak256].
31+
*
32+
* @throws [IllegalArgumentException] if [key] is empty.
33+
* */
34+
@OptIn(InternalKotlinCryptoApi::class)
35+
@Throws(IllegalArgumentException::class)
36+
public constructor(key: ByteArray): super(key, "HmacKeccak256", Keccak256())
37+
38+
@OptIn(InternalKotlinCryptoApi::class)
39+
private constructor(engine: Mac.Engine): super(engine)
40+
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacKeccak256(engineCopy)
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("ClassName", "UnnecessaryOptInAnnotation")
17+
18+
package org.kotlincrypto.macs
19+
20+
import org.kotlincrypto.core.InternalKotlinCryptoApi
21+
import org.kotlincrypto.core.Mac
22+
import org.kotlincrypto.hash.sha3.Keccak384
23+
24+
/**
25+
* HmacKeccak384 implementation
26+
* */
27+
public class HmacKeccak384: Hmac {
28+
29+
/**
30+
* Instantiates a new instance of [HmacKeccak384].
31+
*
32+
* @throws [IllegalArgumentException] if [key] is empty.
33+
* */
34+
@OptIn(InternalKotlinCryptoApi::class)
35+
@Throws(IllegalArgumentException::class)
36+
public constructor(key: ByteArray): super(key, "HmacKeccak384", Keccak384())
37+
38+
@OptIn(InternalKotlinCryptoApi::class)
39+
private constructor(engine: Mac.Engine): super(engine)
40+
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacKeccak384(engineCopy)
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("ClassName", "UnnecessaryOptInAnnotation")
17+
18+
package org.kotlincrypto.macs
19+
20+
import org.kotlincrypto.core.InternalKotlinCryptoApi
21+
import org.kotlincrypto.core.Mac
22+
import org.kotlincrypto.hash.sha3.Keccak512
23+
24+
/**
25+
* HmacKeccak512 implementation
26+
* */
27+
public class HmacKeccak512: Hmac {
28+
29+
/**
30+
* Instantiates a new instance of [HmacKeccak512].
31+
*
32+
* @throws [IllegalArgumentException] if [key] is empty.
33+
* */
34+
@OptIn(InternalKotlinCryptoApi::class)
35+
@Throws(IllegalArgumentException::class)
36+
public constructor(key: ByteArray): super(key, "HmacKeccak512", Keccak512())
37+
38+
@OptIn(InternalKotlinCryptoApi::class)
39+
private constructor(engine: Mac.Engine): super(engine)
40+
41+
protected override fun copy(engineCopy: Mac.Engine): Mac = HmacKeccak512(engineCopy)
42+
}

0 commit comments

Comments
 (0)