diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..65246f6 --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,65 @@ +name: Continuous Integration + +env: + GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.configureondemand=false -Dorg.gradle.vfs.watch=false -Dkotlin.compiler.execution.strategy=in-process' + JAVA_VERSION: 11 + ORG_GRADLE_PROJECT_githubUsername: ${{ github.actor }} + ORG_GRADLE_PROJECT_githubPassword: ${{ github.token }} + +on: + push: + branches: + - master + paths-ignore: + - '*.md' + - 'LICENSE' + - '.gitignore' + +jobs: + build: + timeout-minutes: 15 + runs-on: ubuntu-latest + + steps: + - name: Checkout the repo + uses: actions/checkout@v2 + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Setup JDK ${{ env.JAVA_VERSION }} + uses: actions/setup-java@v1 + with: + java-version: ${{ env.JAVA_VERSION }} + + - name: Cache gradle + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + ~/.konan + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build project and run tests with Gradle + run: ./gradlew clean build --info + + - name: Bundle the failed build report + if: failure() + run: find ./build -type d -name 'reports' | zip -@ -r build-reports.zip + + - name: Upload the failed build report + if: failure() + uses: actions/upload-artifact@v2 + with: + name: error-report + path: build-reports.zip + retention-days: 7 + + - name: Publish library with Gradle + run: ./gradlew publish -Dversion=master-SNAPSHOT \ No newline at end of file diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml new file mode 100644 index 0000000..8f113de --- /dev/null +++ b/.github/workflows/pr-check.yml @@ -0,0 +1,60 @@ +name: Pull Request Check + +env: + GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.configureondemand=false -Dorg.gradle.vfs.watch=false -Dkotlin.compiler.execution.strategy=in-process' + JAVA_VERSION: 11 + +on: + pull_request: + branches: + - master + paths-ignore: + - '*.md' + - 'LICENSE' + - '.gitignore' + +jobs: + build: + timeout-minutes: 15 + runs-on: ubuntu-latest + + steps: + - name: Checkout the repo + uses: actions/checkout@v2 + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Setup JDK ${{ env.JAVA_VERSION }} + uses: actions/setup-java@v1 + with: + java-version: ${{ env.JAVA_VERSION }} + + - name: Cache gradle + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + ~/.konan + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build project and run tests + run: ./gradlew clean build --info + + - name: Bundle the failed build report + if: failure() + run: find ./build -type d -name 'reports' | zip -@ -r build-reports.zip + + - name: Upload the failed build report + if: failure() + uses: actions/upload-artifact@v2 + with: + name: error-report + path: build-reports.zip + retention-days: 7 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..80b2f8a --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,62 @@ +name: Release + +env: + GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.configureondemand=false -Dorg.gradle.vfs.watch=false -Dkotlin.compiler.execution.strategy=in-process' + JAVA_VERSION: 11 + ORG_GRADLE_PROJECT_githubUsername: ${{ github.actor }} + ORG_GRADLE_PROJECT_githubPassword: ${{ github.token }} + +on: + release: + types: [published] + +jobs: + build: + timeout-minutes: 15 + runs-on: ubuntu-latest + + steps: + - name: Checkout the repo + uses: actions/checkout@v2 + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Setup JDK ${{ env.JAVA_VERSION }} + uses: actions/setup-java@v1 + with: + java-version: ${{ env.JAVA_VERSION }} + + - name: Cache gradle + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + ~/.konan + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build project and run tests with Gradle + run: ./gradlew clean build --info + + - name: Bundle the failed build report + if: failure() + run: find ./build -type d -name 'reports' | zip -@ -r build-reports.zip + + - name: Upload the failed build report + if: failure() + uses: actions/upload-artifact@v2 + with: + name: error-report + path: build-reports.zip + retention-days: 7 + + - name: Publish library with Gradle + run: | + NEW_VERSION=$(echo "${GITHUB_REF}" | cut -d "/" -f3) + ./gradlew publish -Dversion=${NEW_VERSION} \ No newline at end of file diff --git a/README.md b/README.md index ea2ff69..7f37736 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -[![](https://jitpack.io/v/komputing/khash.svg)](https://jitpack.io/#komputing/khash) +![CI](https://github.com/komputing/KHash/workflows/Continuous%20Integration/badge.svg) # KHash -KHash is a Kotlin library implementing common hashing functions. +KHash is a Kotlin multiplatform library implementing common hashing functions. A part from that, it also provides some useful [extensions functions](https://kotlinlang.org/docs/reference/extensions.html). @@ -13,6 +13,57 @@ The implemented hashing functions are the following. - Heavily based on this java implementation: [@romus/sha](https://github.com/romus/sha) - Tested with [Nist test vectors](https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bittestvectors.zip) +## Get it + +### GitHub Packages + +This library is available through GitHub Packages. + +![badge][badge-js] +![badge][badge-jvm] + +In order to use it, first include the GitHub Packages maven repository inside your project `build.gradle.kts` file: + +```kotlin +repositories { + maven { + name = "komputing/KHash GitHub Packages" + url = uri("https://maven.pkg.github.com/komputing/KHash") + credentials { + username = "token" + password = "\u0039\u0032\u0037\u0034\u0031\u0064\u0038\u0033\u0064\u0036\u0039\u0061\u0063\u0061\u0066\u0031\u0062\u0034\u0061\u0030\u0034\u0035\u0033\u0061\u0063\u0032\u0036\u0038\u0036\u0062\u0036\u0032\u0035\u0065\u0034\u0061\u0065\u0034\u0032\u0062" + } + } +} +``` +When 'username' could be anything and 'password' is an [encoded access token for public access](https://github.community/t/download-from-github-package-registry-without-authentication/14407/44). + +### JitPack (JVM only!) + +This library is available on Jitpack. The current version is: + +[![](https://jitpack.io/v/komputing/khash.svg)](https://jitpack.io/#komputing/khash) +![badge][badge-jvm] + +In order to use it, first include the Jitpack maven repository inside your project `build.gradle.kts` file: + +```kotlin +repositories { + maven { url="https://jitpack.io" } +} +``` + +## Set it up + +Include the modules inside your project: + +```kotlin +dependencies { + implementation("com.github.komputing.khash::") +} +``` + +Where `` can be either a [release](https://github.com/komputing/KHash/releases) or `-SNAPSHOT` such as `master-SNAPHOT`. ## Usage ### `keccak` @@ -92,3 +143,6 @@ Also this code is not hardened against side channel attacks. Keep this in mind w # License MIT + +[badge-js]: http://img.shields.io/badge/platform-js-yellow.svg?style=flat +[badge-jvm]: http://img.shields.io/badge/platform-jvm-orange.svg?style=flat \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index d60d39a..405d805 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,50 +1,20 @@ plugins { - id("com.github.ben-manes.versions").version(Versions.versions_plugin) + base } -buildscript { - repositories { - mavenLocal() - jcenter() - maven("https://plugins.gradle.org/m2/") - } - - dependencies { - classpath("com.github.ben-manes:gradle-versions-plugin:${Versions.versions_plugin}") - classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}") - } +val testAggregateReport = tasks.register("testAggregateReport") { + group = "Reporting" + description = "Collect aggregate test reports of all sub-modules." + destinationDir = file("$buildDir/reports/tests") + reportOn(subprojects.map { + it.tasks.withType() + }) } - - subprojects { - group = "org.komputing" - - plugins.apply("kotlin") - plugins.apply("maven") - - dependencies { - "implementation"("org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}") - - "testCompile"("org.junit.jupiter:junit-jupiter-api:${Versions.jupiter}") - "testCompile"("org.junit.jupiter:junit-jupiter-params:${Versions.jupiter}") - "testRuntime"("org.junit.jupiter:junit-jupiter-engine:${Versions.jupiter}") - - "testImplementation"("org.jetbrains.kotlin:kotlin-test") - - "testImplementation"("com.github.komputing:khex:0.6") - } - - tasks.withType { - useJUnitPlatform() - testLogging { - events("passed", "skipped", "failed") + afterEvaluate { + tasks.withType { + finalizedBy(testAggregateReport) } } - - repositories { - jcenter() - maven("https://jitpack.io") - maven("https://kotlin.bintray.com/kotlinx") - } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 208d368..7ede595 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,6 +1,7 @@ plugins { idea `kotlin-dsl` + `kotlin-dsl-precompiled-script-plugins` } repositories { @@ -12,4 +13,9 @@ idea { isDownloadJavadoc = false isDownloadSources = false } +} + +dependencies { + implementation(kotlin("gradle-plugin", "${property("kgp")}")) + implementation("com.github.ben-manes", "gradle-versions-plugin", "${property("plugin.com.github.ben-manes.versions")}") } \ No newline at end of file diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties new file mode 120000 index 0000000..7677fb7 --- /dev/null +++ b/buildSrc/gradle.properties @@ -0,0 +1 @@ +../gradle.properties \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 21f6f8e..76312fa 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -1,5 +1,4 @@ object Versions { - const val kotlin = "1.3.71" - const val versions_plugin = "0.28.0" - const val jupiter = "5.6.1" + const val khex = "1.1.0" + const val bignum = "0.2.8" } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/mpp-module.gradle.kts b/buildSrc/src/main/kotlin/mpp-module.gradle.kts new file mode 100644 index 0000000..6f18dea --- /dev/null +++ b/buildSrc/src/main/kotlin/mpp-module.gradle.kts @@ -0,0 +1,111 @@ +import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask +import org.gradle.api.tasks.testing.logging.TestExceptionFormat +import org.gradle.api.tasks.testing.logging.TestLogEvent + +plugins { + kotlin("multiplatform") + id("com.github.ben-manes.versions") + id("maven-publish") +} + +kotlin { + explicitApi() + targets { + jvm { + compilations.all { + kotlinOptions.jvmTarget = "1.8" + } + } + js(BOTH) { + compilations { + this.forEach { compilation -> + compilation.compileKotlinTask.kotlinOptions.apply { + sourceMap = true + moduleKind = "umd" + metaInfo = true + sourceMapEmbedSources = "always" + + if (compilation.name == "main") main = "noCall" + } + } + } + nodejs { + testTask { + useMocha { + timeout = "300s" + } + } + } + } + } + sourceSets { + commonTest { + dependencies { + implementation(kotlin("test-common")) + implementation(kotlin("test-annotations-common")) + implementation("com.github.komputing.khex:extensions:${Versions.khex}") + } + } + + val jvmTest by getting { + dependencies { + implementation(kotlin("test-junit")) + } + } + + val jsTest by getting { + dependencies { + implementation(kotlin("test-js")) + } + } + matching { it.name.contains("test", ignoreCase = true) } + .forEach { + it.languageSettings.enableLanguageFeature("InlineClasses") + } + } +} + +tasks { + withType { + testLogging { + events = setOf(TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED) + showExceptions = true + exceptionFormat = TestExceptionFormat.FULL + } + } +} + +/** + * The Gradle Versions Plugin is another Gradle plugin to discover dependency updates + * plugins.id("com.github.ben-manes.versions") + * Run it with $ ./gradlew --scan dependencyUpdates + * https://github.com/ben-manes/gradle-versions-plugin + * **/ +tasks.named("dependencyUpdates", DependencyUpdatesTask::class).configure { + fun isNonStable(version: String): Boolean { + val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { version.toUpperCase().contains(it) } + val regex = "^[0-9,.v-]+(-r)?$".toRegex() + val isStable = stableKeyword || regex.matches(version) + return isStable.not() + } + + rejectVersionIf { + isNonStable(candidate.version) + } + checkConstraints = true +} + +System.getenv("GITHUB_REPOSITORY")?.let { githubRepo -> + val (owner, repoName) = githubRepo.split('/').map(String::toLowerCase) + group = "com.github.$owner.$repoName" + version = System.getProperty("version") + publishing { + repositories { + maven { + name = "github" + url = uri("https://maven.pkg.github.com/$githubRepo") + credentials(PasswordCredentials::class) + } + } + } +} diff --git a/gradle.properties b/gradle.properties index ea69e15..b4b552e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,12 +1,31 @@ -# sytleguide +# gradle.properties + +## Gradle Build Settings | jvmargs have been optimized for github Actions + +org.gradle.caching=true +org.gradle.configureondemand=true +org.gradle.jvmargs=-Xmx6g -XX:+UseG1GC +org.gradle.parallel=true +org.gradle.vfs.watch=true + +## Kotlin Build Settings + kotlin.code.style=official +kotlin.incremental=true +kotlin.mpp.enableGranularSourceSetsMetadata=true +kotlin.mpp.stability.nowarn=true +kotlin.native.enableDependencyPropagation=false -# config -version=1.0.0-RC2 +## Kapt Build Settings -# gradle -org.gradle.daemon=true -org.gradle.jvmargs=-Xmx2g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -org.jetbrains.kotlin.native.jvmArgs=-XX:TieredStopAtLevel=1 +kapt.include.compile.classpath=false +kapt.incremental.apt=true +kapt.use.worker.api=true -org.gradle.parallel=true +## Kotlin Gradle Plugin + +kgp=1.4.30 + +## Gradle Plugins + +plugin.com.github.ben-manes.versions=0.36.0 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 6623300..25d3265 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/keccak/build.gradle.kts b/keccak/build.gradle.kts new file mode 100644 index 0000000..076f392 --- /dev/null +++ b/keccak/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + id("mpp-module") +} + +kotlin { + sourceSets{ + commonMain { + dependencies { + implementation("com.ionspin.kotlin:bignum:${Versions.bignum}") + } + } + } +} \ No newline at end of file diff --git a/keccak/src/main/kotlin/org/komputing/khash/keccak/Keccak.kt b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/Keccak.kt similarity index 92% rename from keccak/src/main/kotlin/org/komputing/khash/keccak/Keccak.kt rename to keccak/src/commonMain/kotlin/org/komputing/khash/keccak/Keccak.kt index 42aa4ee..621ccf8 100644 --- a/keccak/src/main/kotlin/org/komputing/khash/keccak/Keccak.kt +++ b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/Keccak.kt @@ -1,15 +1,15 @@ package org.komputing.khash.keccak +import com.ionspin.kotlin.bignum.integer.BigInteger import org.komputing.khash.keccak.extensions.fillWith -import java.math.BigInteger import kotlin.math.min -object Keccak { +public object Keccak { - private val BIT_65 = BigInteger.ONE.shiftLeft(64) - private val MAX_64_BITS = BIT_65.subtract(BigInteger.ONE) + private val BIT_65 = BigInteger.ONE shl (64) + private val MAX_64_BITS = BIT_65 - BigInteger.ONE - fun digest(value: ByteArray, parameter: KeccakParameter): ByteArray { + public fun digest(value: ByteArray, parameter: KeccakParameter): ByteArray { val uState = IntArray(200) val uMessage = convertToUInt(value) @@ -140,7 +140,7 @@ object Keccak { // pow(2, i) - 1 val bitPosition = (1 shl i) - 1 if (lfsrState and 2 != 0) { - state[0][0] = state[0][0].xor(BigInteger.ONE.shiftLeft(bitPosition)) + state[0][0] = state[0][0].xor(BigInteger.ONE shl bitPosition) } } } @@ -161,7 +161,7 @@ object Keccak { .map { if (it.length == 2) it else "0$it" } .reversed() .joinToString("") - return BigInteger(value, 16) + return BigInteger.parseString(value, 16) } /** @@ -179,5 +179,5 @@ object Keccak { private fun BigInteger.leftRotate64Safely(rotate: Int) = leftRotate64(rotate % 64) - private fun BigInteger.leftRotate64(rotate: Int) = shiftRight(64 - rotate).add(shiftLeft(rotate)).mod(BIT_65) + private fun BigInteger.leftRotate64(rotate: Int) = (this shr (64 - rotate)).add(this shl rotate).mod(BIT_65) } diff --git a/keccak/src/main/kotlin/org/komputing/khash/keccak/KeccakParameter.kt b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/KeccakParameter.kt similarity index 74% rename from keccak/src/main/kotlin/org/komputing/khash/keccak/KeccakParameter.kt rename to keccak/src/commonMain/kotlin/org/komputing/khash/keccak/KeccakParameter.kt index e508e6b..2925ab4 100644 --- a/keccak/src/main/kotlin/org/komputing/khash/keccak/KeccakParameter.kt +++ b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/KeccakParameter.kt @@ -3,7 +3,7 @@ package org.komputing.khash.keccak /** * Parameters defining the FIPS 202 standard. */ -enum class KeccakParameter constructor(val rateInBytes: Int, val outputLengthInBytes: Int, val d: Int) { +public enum class KeccakParameter constructor(public val rateInBytes: Int, public val outputLengthInBytes: Int, public val d: Int) { KECCAK_224(144, 28, 0x01), KECCAK_256(136, 32, 0x01), diff --git a/keccak/src/main/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensions.kt b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensions.kt similarity index 100% rename from keccak/src/main/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensions.kt rename to keccak/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensions.kt diff --git a/keccak/src/main/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt similarity index 64% rename from keccak/src/main/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt rename to keccak/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt index e2ad582..c137414 100644 --- a/keccak/src/main/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt +++ b/keccak/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt @@ -6,13 +6,13 @@ import org.komputing.khash.keccak.KeccakParameter /** * Computes the proper Keccak digest of [this] byte array based on the given [parameter] */ -fun ByteArray.digestKeccak(parameter: KeccakParameter): ByteArray { +public fun ByteArray.digestKeccak(parameter: KeccakParameter): ByteArray { return Keccak.digest(this, parameter) } /** * Computes the proper Keccak digest of [this] string based on the given [parameter] */ -fun String.digestKeccak(parameter: KeccakParameter): ByteArray { - return Keccak.digest(toByteArray(), parameter) +public fun String.digestKeccak(parameter: KeccakParameter): ByteArray { + return Keccak.digest(encodeToByteArray(), parameter) } diff --git a/keccak/src/test/kotlin/org/komputing/khash/keccak/KeccakTests.kt b/keccak/src/commonTest/kotlin/org/komputing/khash/keccak/KeccakTests.kt similarity index 68% rename from keccak/src/test/kotlin/org/komputing/khash/keccak/KeccakTests.kt rename to keccak/src/commonTest/kotlin/org/komputing/khash/keccak/KeccakTests.kt index 364f916..8b391a4 100644 --- a/keccak/src/test/kotlin/org/komputing/khash/keccak/KeccakTests.kt +++ b/keccak/src/commonTest/kotlin/org/komputing/khash/keccak/KeccakTests.kt @@ -1,12 +1,13 @@ package org.komputing.khash.keccak -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test import org.komputing.khash.keccak.extensions.digestKeccak -import org.walleth.khex.hexToByteArray -import org.walleth.khex.toHexString -import org.walleth.khex.toNoPrefixHexString +import org.komputing.khex.extensions.hexToByteArray +import org.komputing.khex.extensions.toHexString +import org.komputing.khex.extensions.toNoPrefixHexString +import org.komputing.khex.model.HexString +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue /** * Tests the [Keccak.digest] method against the NIST test vectors. @@ -25,7 +26,7 @@ class KeccakTests { fun testNistVectors() { allNistTestVectors.forEach { (vectorList, params) -> vectorList.forEach { (input, expected) -> - assertEquals(Keccak.digest(input.hexToByteArray(), params).toNoPrefixHexString(), expected) + assertEquals(Keccak.digest(HexString(input).hexToByteArray(), params).toNoPrefixHexString(), expected) } } } @@ -35,28 +36,28 @@ class KeccakTests { assertTrue( "".digestKeccak(KeccakParameter.KECCAK_256) - .contentEquals("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".hexToByteArray()) + .contentEquals(HexString("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").hexToByteArray()) ) assertTrue( "The quick brown fox jumps over the lazy dog".digestKeccak(KeccakParameter.KECCAK_256) - .contentEquals("4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15".hexToByteArray()) + .contentEquals(HexString("4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15").hexToByteArray()) ) assertTrue( "The quick brown fox jumps over the lazy dog.".digestKeccak(KeccakParameter.KECCAK_256) - .contentEquals("578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d".hexToByteArray()) + .contentEquals(HexString("578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d").hexToByteArray()) ) assertTrue( "The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog." .digestKeccak(KeccakParameter.KECCAK_256) - .contentEquals("e35949d2ca446ea2fd99f49bed23c60e0b9849f5384661bc574a5c55fcaeb4bd".hexToByteArray()) + .contentEquals(HexString("e35949d2ca446ea2fd99f49bed23c60e0b9849f5384661bc574a5c55fcaeb4bd").hexToByteArray()) ) assertEquals( Keccak.digest( - "a1b31be4d58a7ddd24b135db0da56a90fb5382077ae26b250e1dc9cd6232ce2270f4c995428bc76aa78e522316e95d7834d725efc9ca754d043233af6ca90113".hexToByteArray(), + HexString("a1b31be4d58a7ddd24b135db0da56a90fb5382077ae26b250e1dc9cd6232ce2270f4c995428bc76aa78e522316e95d7834d725efc9ca754d043233af6ca90113").hexToByteArray(), KeccakParameter.KECCAK_256 ).toHexString(""), "e1674295e8fc8ffdfb46cadb01c52b08330e05d731e38c856c1043288f7d9744" diff --git a/keccak/src/test/kotlin/org/komputing/khash/keccak/Sha3TestVectors.kt b/keccak/src/commonTest/kotlin/org/komputing/khash/keccak/Sha3TestVectors.kt similarity index 100% rename from keccak/src/test/kotlin/org/komputing/khash/keccak/Sha3TestVectors.kt rename to keccak/src/commonTest/kotlin/org/komputing/khash/keccak/Sha3TestVectors.kt diff --git a/keccak/src/test/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt b/keccak/src/commonTest/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt similarity index 88% rename from keccak/src/test/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt rename to keccak/src/commonTest/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt index 466b760..eaee377 100644 --- a/keccak/src/test/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt +++ b/keccak/src/commonTest/kotlin/org/komputing/khash/keccak/extensions/IntArrayExtensionsTests.kt @@ -1,7 +1,7 @@ package org.komputing.khash.keccak.extensions -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test +import kotlin.test.Test +import kotlin.test.assertTrue /** * Tests for the extensions functions present inside `IntArrayExtensions.kt` diff --git a/ripemd160/build.gradle.kts b/ripemd160/build.gradle.kts new file mode 100644 index 0000000..e31a490 --- /dev/null +++ b/ripemd160/build.gradle.kts @@ -0,0 +1,3 @@ +plugins { + id("mpp-module") +} \ No newline at end of file diff --git a/ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt b/ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt similarity index 98% rename from ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt rename to ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt index 50ab062..97187be 100644 --- a/ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt +++ b/ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/Ripemd160Digest.kt @@ -4,7 +4,7 @@ package org.komputing.khash.ripemd160 * implementation of RIPEMD see, * http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html */ -class Ripemd160Digest { +public class Ripemd160Digest { private var h0: Int = 0 private var h1: Int = 0 @@ -24,7 +24,7 @@ class Ripemd160Digest { reset() } - fun update(input: Byte) { + public fun update(input: Byte) { xBuf[xBufOff++] = input if (xBufOff == xBuf.size) { @@ -35,7 +35,7 @@ class Ripemd160Digest { byteCount++ } - fun update(input: ByteArray, inputOffset: Int, len: Int) { + public fun update(input: ByteArray, inputOffset: Int, len: Int) { var pos = inputOffset var toProcess = len // @@ -136,7 +136,7 @@ class Ripemd160Digest { out[outOffset + 3] = word.ushr(24).toByte() } - fun doFinal(out: ByteArray, outOffset: Int): Int { + public fun doFinal(out: ByteArray, outOffset: Int): Int { finish() unpackWord(h0, out, outOffset) @@ -567,7 +567,7 @@ class Ripemd160Digest { } } - companion object { - const val DIGEST_LENGTH = 20 + public companion object { + public const val DIGEST_LENGTH: Int = 20 } } diff --git a/ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt b/ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt similarity index 75% rename from ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt rename to ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt index 06151a5..1900533 100644 --- a/ripemd160/src/main/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt +++ b/ripemd160/src/commonMain/kotlin/org/komputing/khash/ripemd160/extensions/PublicExtensions.kt @@ -5,7 +5,7 @@ import org.komputing.khash.ripemd160.Ripemd160Digest /** * Computes the RIPEMD160 from [this] [ByteArray]. */ -fun ByteArray.digestRipemd160(): ByteArray { +public fun ByteArray.digestRipemd160(): ByteArray { return ByteArray(Ripemd160Digest.DIGEST_LENGTH).apply { val digest = Ripemd160Digest() digest.update(this@digestRipemd160, 0, this@digestRipemd160.size) @@ -16,4 +16,4 @@ fun ByteArray.digestRipemd160(): ByteArray { /** * Computes the RIPEMD160 for [this] string. */ -fun String.digestRipemd160() = toByteArray(Charsets.UTF_8).digestRipemd160() +public fun String.digestRipemd160(): ByteArray = encodeToByteArray().digestRipemd160() diff --git a/ripemd160/src/test/java/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt b/ripemd160/src/commonTest/kotlin/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt similarity index 75% rename from ripemd160/src/test/java/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt rename to ripemd160/src/commonTest/kotlin/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt index 1e63815..0345f38 100644 --- a/ripemd160/src/test/java/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt +++ b/ripemd160/src/commonTest/kotlin/org/komputing/khash/ripemd160/RIPEMD160DigestTests.kt @@ -1,9 +1,10 @@ package org.komputing.khash.ripemd160 -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test import org.komputing.khash.ripemd160.extensions.digestRipemd160 -import org.walleth.khex.hexToByteArray +import org.komputing.khex.extensions.hexToByteArray +import org.komputing.khex.model.HexString +import kotlin.test.Test +import kotlin.test.assertTrue class RIPEMD160DigestTests { @@ -23,14 +24,14 @@ class RIPEMD160DigestTests { @Test fun testStringInput() { testVectors.forEach { (message, expectedDigest) -> - assertTrue(message.digestRipemd160().contentEquals(expectedDigest.hexToByteArray())) + assertTrue(message.digestRipemd160().contentEquals(HexString(expectedDigest).hexToByteArray())) } } @Test fun testByteArrayInput() { testVectors.forEach { (message, expectedDigest) -> - assertTrue(message.toByteArray().digestRipemd160().contentEquals(expectedDigest.hexToByteArray())) + assertTrue(message.encodeToByteArray().digestRipemd160().contentEquals(HexString(expectedDigest).hexToByteArray())) } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index ad6cbb5..02a6aac 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,20 @@ +@file:Suppress("UnstableApiUsage") + +dependencyResolutionManagement { + repositories { + maven { + name = "komputing/KHex GitHub Packages" + url = uri("https://maven.pkg.github.com/komputing/KHex") + credentials { + username = "token" + // see: https://github.community/t/download-from-github-package-registry-without-authentication/14407/44 + password = "\u0039\u0032\u0037\u0034\u0031\u0064\u0038\u0033\u0064\u0036\u0039\u0061\u0063\u0061\u0066\u0031\u0062\u0034\u0061\u0030\u0034\u0035\u0033\u0061\u0063\u0032\u0036\u0038\u0036\u0062\u0036\u0032\u0035\u0065\u0034\u0061\u0065\u0034\u0032\u0062" + } + } + mavenCentral() + } +} + include(":keccak") include(":sha256") include(":sha512") diff --git a/sha256/build.gradle.kts b/sha256/build.gradle.kts new file mode 100644 index 0000000..e31a490 --- /dev/null +++ b/sha256/build.gradle.kts @@ -0,0 +1,3 @@ +plugins { + id("mpp-module") +} \ No newline at end of file diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/Sha256.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt similarity index 98% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/Sha256.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt index 1ddba7e..15a9561 100644 --- a/sha256/src/main/kotlin/org/komputing/khash/sha256/Sha256.kt +++ b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt @@ -10,7 +10,7 @@ import org.komputing.khash.sha256.extensions.toIntArray * Digest Class for SHA-256. * Original Java version at https://github.com/meyfa/java-sha256/blob/master/src/main/java/net/meyfa/sha256/Sha256.java */ -object Sha256 { +public object Sha256 { private val K = intArrayOf( 0x428a2f98, 0x71374491, -0x4a3f0431, -0x164a245b, 0x3956c25b, 0x59f111f1, -0x6dc07d5c, -0x54e3a12b, @@ -39,7 +39,7 @@ object Sha256 { * @param message The bytes to digest. * @return The digest's bytes. */ - fun digest(message: ByteArray): ByteArray { + public fun digest(message: ByteArray): ByteArray { // Let H = H0 H0.copy(0, H, 0, H0.size) diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensions.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensions.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ByteArrayExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ByteArrayExtensions.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ByteArrayExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ByteArrayExtensions.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ByteExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ByteExtensions.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/ByteExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/ByteExtensions.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/Constants.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/Constants.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/Constants.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/Constants.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensions.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensions.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/IntExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/IntExtensions.kt similarity index 100% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/IntExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/IntExtensions.kt diff --git a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt similarity index 59% rename from sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt rename to sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt index c331bc1..247ce37 100644 --- a/sha256/src/main/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt +++ b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/extensions/PublicExtensions.kt @@ -5,9 +5,9 @@ import org.komputing.khash.sha256.Sha256 /** * Returns the SHA256 digest of this byte array. */ -fun ByteArray.sha256() = Sha256.digest(this) +public fun ByteArray.sha256(): ByteArray = Sha256.digest(this) /** * Returns the SHA256 digest of this string. */ -fun String.sha256() = this.toByteArray(Charsets.UTF_8).sha256() +public fun String.sha256(): ByteArray = this.encodeToByteArray().sha256() diff --git a/sha256/src/test/kotlin/org/komputing/khash/sha256/Sha256Tests.kt b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/Sha256Tests.kt similarity index 79% rename from sha256/src/test/kotlin/org/komputing/khash/sha256/Sha256Tests.kt rename to sha256/src/commonTest/kotlin/org/komputing/khash/sha256/Sha256Tests.kt index 45702ca..c3a82bb 100644 --- a/sha256/src/test/kotlin/org/komputing/khash/sha256/Sha256Tests.kt +++ b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/Sha256Tests.kt @@ -1,9 +1,10 @@ package org.komputing.khash.sha256 -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test -import org.walleth.khex.hexToByteArray +import org.komputing.khex.extensions.hexToByteArray +import org.komputing.khex.model.HexString +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue class Sha256Tests { @@ -18,8 +19,8 @@ class Sha256Tests { } private fun testHash(input: String, expected: String) { - val inputArray = input.toByteArray() - val expectedOutput = expected.hexToByteArray() + val inputArray = input.encodeToByteArray() + val expectedOutput = HexString(expected).hexToByteArray() assertTrue(expectedOutput.contentEquals(Sha256.digest(inputArray))) } @@ -29,7 +30,7 @@ class Sha256Tests { b[i] = i.toByte() } - val expected = "40aff2e9d2d8922e47afd4648e6967497158785fbd1da870e7110266bf944880".hexToByteArray() + val expected = HexString("40aff2e9d2d8922e47afd4648e6967497158785fbd1da870e7110266bf944880").hexToByteArray() assertTrue(expected.contentEquals(Sha256.digest(b))) } diff --git a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt similarity index 93% rename from sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt rename to sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt index 14ddfe5..12e5b23 100644 --- a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt +++ b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ArrayExtensionsTests.kt @@ -1,6 +1,6 @@ package org.komputing.khash.sha256.extensions -import org.junit.jupiter.api.Test +import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith diff --git a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt similarity index 93% rename from sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt rename to sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt index 702b21e..653680a 100644 --- a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt +++ b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/ByteExtensionsTests.kt @@ -1,6 +1,6 @@ package org.komputing.khash.sha256.extensions -import org.junit.jupiter.api.Test +import kotlin.test.Test import kotlin.test.assertEquals class ByteExtensionsTests { diff --git a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt similarity index 96% rename from sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt rename to sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt index 9be27bf..6e89b90 100644 --- a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt +++ b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntArrayExtensionsTests.kt @@ -1,6 +1,6 @@ package org.komputing.khash.sha256.extensions -import org.junit.jupiter.api.Test +import kotlin.test.Test import kotlin.test.assertFailsWith import kotlin.test.assertTrue diff --git a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt similarity index 82% rename from sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt rename to sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt index 61f6ccf..a34a8c0 100644 --- a/sha256/src/test/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt +++ b/sha256/src/commonTest/kotlin/org/komputing/khash/sha256/extensions/IntExtensionsTests.kt @@ -1,8 +1,8 @@ package org.komputing.khash.sha256.extensions -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue class IntExtensionsTests { diff --git a/sha512/build.gradle.kts b/sha512/build.gradle.kts new file mode 100644 index 0000000..e31a490 --- /dev/null +++ b/sha512/build.gradle.kts @@ -0,0 +1,3 @@ +plugins { + id("mpp-module") +} \ No newline at end of file diff --git a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha384.kt b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha384.kt similarity index 66% rename from sha512/src/main/kotlin/org/komputing/khash/sha512/Sha384.kt rename to sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha384.kt index 50a0b19..abad2e7 100644 --- a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha384.kt +++ b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha384.kt @@ -1,10 +1,11 @@ package org.komputing.khash.sha512 -object Sha384 { +public object Sha384 { private val H0 = longArrayOf( -0x344462a23efa6128L, 0x629a292a367cd507L, -0x6ea6fea5cf8f22e9L, 0x152fecd8f70e5939L, 0x67332667ffc00b31L, -0x714bb57897a7eaefL, -0x24f3d1f29b067059L, 0x47b5481dbefa4fa4L ) - fun digest(message: ByteArray) = Sha512.digest(message, H0).sliceArray(0 until 48) + public fun digest(message: ByteArray): ByteArray = + Sha512.digest(message, H0).sliceArray(0 until 48) } \ No newline at end of file diff --git a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512.kt b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512.kt similarity index 97% rename from sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512.kt rename to sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512.kt index 5f11485..dea4c32 100644 --- a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512.kt +++ b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512.kt @@ -4,7 +4,7 @@ package org.komputing.khash.sha512 * Digest Class for SHA-512. * Original Java version at https://github.com/trittimo/SHA512 */ -object Sha512 { +public object Sha512 { private val K = longArrayOf( 0x428A2F98D728AE22L, 0x7137449123EF65CDL, -0x4a3f043013b2c4d1L, -0x164a245a7e762444L, @@ -34,7 +34,7 @@ object Sha512 { 0x510E527FADE682D1L, -0x64fa9773d4c193e1L, 0x1F83D9ABFB41BD6BL, 0x5BE0CD19137E2179L ) - fun digest(input: ByteArray) = digest(input, H0) + public fun digest(input: ByteArray): ByteArray = digest(input, H0) // Does the actual hash internal fun digest(input: ByteArray, h0: LongArray): ByteArray { @@ -46,7 +46,7 @@ object Sha512 { val expandedMessageBlocks: Array = calculateMessageBlocks(blocks) // Set up the buffer which will eventually contain the final hash // Initially, it's set to the constants provided as part of the algorithm - val buffer: LongArray = h0.clone() + val buffer: LongArray = h0.copyOf() // For every block for (i in blocks.indices) { // a-h is set to the buffer initially var a = buffer[0] @@ -101,7 +101,7 @@ object Sha512 { private fun maj(x: Long, y: Long, z: Long) = x and y xor (x and z) xor (y and z) // Used in the compression function - private fun rotate(x: Long, l: Int) = x ushr l or (x shl java.lang.Long.SIZE - l) + private fun rotate(x: Long, l: Int) = x ushr l or (x shl Long.SIZE_BITS - l) // Used in the compression function // Sn = right rotate by n bits diff --git a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_224.kt b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_224.kt similarity index 65% rename from sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_224.kt rename to sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_224.kt index 79ade1c..a642f6b 100644 --- a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_224.kt +++ b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_224.kt @@ -1,10 +1,11 @@ package org.komputing.khash.sha512 -object Sha512_224 { +public object Sha512_224 { private val H0 = longArrayOf( -0x73c2c837e6abb25eL, 0x73E1996689DCD4D6L, 0x1DFAB7AE32FF9C82L, 0x679DD514582F9FCFL, 0x0F6D2B697BD44DA8L, 0x77E36F7304C48942L, 0x3F9D85A86A1D36C8L, 0x1112E6AD91D692A1L ) - fun digest(message: ByteArray) = Sha512.digest(message, H0).sliceArray(0 until 28) + public fun digest(message: ByteArray): ByteArray = + Sha512.digest(message, H0).sliceArray(0 until 28) } \ No newline at end of file diff --git a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_256.kt b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_256.kt similarity index 65% rename from sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_256.kt rename to sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_256.kt index 199b4f5..4b5f7a8 100644 --- a/sha512/src/main/kotlin/org/komputing/khash/sha512/Sha512_256.kt +++ b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/Sha512_256.kt @@ -1,10 +1,11 @@ package org.komputing.khash.sha512 -object Sha512_256 { +public object Sha512_256 { private val H0 = longArrayOf( 0x22312194fc2bf72cL, -0x60aaa05c37b39b3eL, 0x2393b86b6f53b151L, -0x69c788e6a6bf1543L, -0x69d7c11d5771001dL, -0x41a1e1daac79c66eL, 0x2b0199fc2c85b8aaL, 0x0eb72ddc81c52ca2L ) - fun digest(message: ByteArray) = Sha512.digest(message, H0).sliceArray(0 until 32) + public fun digest(message: ByteArray): ByteArray = + Sha512.digest(message, H0).sliceArray(0 until 32) } \ No newline at end of file diff --git a/sha512/src/main/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt similarity index 60% rename from sha512/src/main/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt rename to sha512/src/commonMain/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt index cb998c4..0ee4f75 100644 --- a/sha512/src/main/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt +++ b/sha512/src/commonMain/kotlin/org/komputing/khash/sha512/extensions/PublicExtensions.kt @@ -5,9 +5,9 @@ import org.komputing.khash.sha512.Sha512 /** * Returns the SHA512 digest of this byte array. */ -fun ByteArray.sha512() = Sha512.digest(this) +public fun ByteArray.sha512(): ByteArray = Sha512.digest(this) /** * Returns the SHA512 digest of this string. */ -fun String.sha512() = toByteArray(Charsets.UTF_8).sha512() \ No newline at end of file +public fun String.sha512(): ByteArray = encodeToByteArray().sha512() \ No newline at end of file diff --git a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha384Tests.kt b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha384Tests.kt similarity index 83% rename from sha512/src/test/kotlin/org/komputing/khash/sha512/Sha384Tests.kt rename to sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha384Tests.kt index de678eb..4e5827e 100644 --- a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha384Tests.kt +++ b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha384Tests.kt @@ -1,15 +1,15 @@ package org.komputing.khash.sha512 -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.walleth.khex.toNoPrefixHexString +import org.komputing.khex.extensions.toNoPrefixHexString +import kotlin.test.Test +import kotlin.test.assertEquals class Sha384Tests { @Test fun testDigest1() { testHash( - "".toByteArray(), + "".encodeToByteArray(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b" ) } @@ -17,7 +17,7 @@ class Sha384Tests { @Test fun testDigest2() { testHash( - "Hello world!".toByteArray(), + "Hello world!".encodeToByteArray(), "86255fa2c36e4b30969eae17dc34c772cbebdfc58b58403900be87614eb1a34b8780263f255eb5e65ca9bbb8641cccfe" ) } @@ -27,7 +27,7 @@ class Sha384Tests { val loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Proin pulvinar turpis purus, sit amet dapibus magna commodo quis metus." testHash( - loremIpsum.toByteArray(), + loremIpsum.encodeToByteArray(), "bd2143a9952d27a18331aa3632a01331531ffec2a4ff351a5859706ebdb39068b34553ac81f83f1bc6b1b16f31c06e2b" ) } diff --git a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512Tests.kt b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512Tests.kt similarity index 84% rename from sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512Tests.kt rename to sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512Tests.kt index 510c39b..fda3df2 100644 --- a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512Tests.kt +++ b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512Tests.kt @@ -1,15 +1,15 @@ package org.komputing.khash.sha512 -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.walleth.khex.toNoPrefixHexString +import org.komputing.khex.extensions.toNoPrefixHexString +import kotlin.test.Test +import kotlin.test.assertEquals class Sha512Tests { @Test fun testDigest1() { testHash( - "".toByteArray(), + "".encodeToByteArray(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" ) } @@ -17,7 +17,7 @@ class Sha512Tests { @Test fun testDigest2() { testHash( - "Hello world!".toByteArray(), + "Hello world!".encodeToByteArray(), "f6cde2a0f819314cdde55fc227d8d7dae3d28cc556222a0a8ad66d91ccad4aad6094f517a2182360c9aacf6a3dc323162cb6fd8cdffedb0fe038f55e85ffb5b6" ) } @@ -27,7 +27,7 @@ class Sha512Tests { val loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Proin pulvinar turpis purus, sit amet dapibus magna commodo quis metus." testHash( - loremIpsum.toByteArray(), + loremIpsum.encodeToByteArray(), "de94877cc9711605dcdc09d85bd3080f74398d5e1ad8f0dcd1726c54ac93f2b4b781c3f56de1fbc725ac261a2c09d1d5bb24d0afa7449e4ffe4b2a7e6d09f40d" ) } diff --git a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt similarity index 81% rename from sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt rename to sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt index 0aec5a4..1c235e8 100644 --- a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt +++ b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_224Tests.kt @@ -1,15 +1,15 @@ package org.komputing.khash.sha512 -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.walleth.khex.toNoPrefixHexString +import org.komputing.khex.extensions.toNoPrefixHexString +import kotlin.test.Test +import kotlin.test.assertEquals class Sha512_224Tests { @Test fun testDigest1() { testHash( - "".toByteArray(), + "".encodeToByteArray(), "6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4" ) } @@ -17,7 +17,7 @@ class Sha512_224Tests { @Test fun testDigest2() { testHash( - "Hello world!".toByteArray(), + "Hello world!".encodeToByteArray(), "b48c4994a3d2b6b48ae7fa6fcc09f33dc0c985109c0b7493fd3c74d0" ) } @@ -27,7 +27,7 @@ class Sha512_224Tests { val loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Proin pulvinar turpis purus, sit amet dapibus magna commodo quis metus." testHash( - loremIpsum.toByteArray(), + loremIpsum.encodeToByteArray(), "09c63e3a2e822c8477b192da10afa757824ba057d9996b823ad08656" ) } diff --git a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt similarity index 81% rename from sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt rename to sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt index 53cb358..049623e 100644 --- a/sha512/src/test/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt +++ b/sha512/src/commonTest/kotlin/org/komputing/khash/sha512/Sha512_256Tests.kt @@ -1,15 +1,15 @@ package org.komputing.khash.sha512 -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.walleth.khex.toNoPrefixHexString +import org.komputing.khex.extensions.toNoPrefixHexString +import kotlin.test.Test +import kotlin.test.assertEquals class Sha512_256Tests { @Test fun testDigest1() { testHash( - "".toByteArray(), + "".encodeToByteArray(), "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a" ) } @@ -17,7 +17,7 @@ class Sha512_256Tests { @Test fun testDigest2() { testHash( - "Hello world!".toByteArray(), + "Hello world!".encodeToByteArray(), "f8162ad49196c1c12bddbcff1d362ddacf03ae246b6a7864b75c244b965fe475" ) } @@ -27,7 +27,7 @@ class Sha512_256Tests { val loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Proin pulvinar turpis purus, sit amet dapibus magna commodo quis metus." testHash( - loremIpsum.toByteArray(), + loremIpsum.encodeToByteArray(), "3e4d6734eb5ea2195a818833ad47bd3be83320bc4619e076a9cc21028e0dfd7e" ) }