commit 519cf204e57a9fd5f4e34edf81fb9fed96b7eaae Author: Infendro Date: Sat Jul 12 19:25:45 2025 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..769753d --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# IDE +/.idea/ + +# Gradle +/.gradle/ +/gradle.properties + +# Kotlin +/.kotlin/ +/build/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0a04128 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..749ffbd --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,48 @@ +group = "com.infendro" +version = "1.1.0" + +repositories { + mavenCentral() +} + +plugins { + alias(libs.plugins.multiplatform) + id("maven-publish") +} + +kotlin { + jvm() + js { + nodejs() + } + linuxX64() +} + +publishing { + repositories { + maven { + url = uri("https://git.infendro.com/api/packages/Infendro/maven") + authentication.create("header", HttpHeaderAuthentication::class) + credentials(HttpHeaderCredentials::class) { + val token = secret("git.token") ?: throw GradleException() + + name = "Authorization" + value = "token $token" + } + } + } +} + +fun secret( + key: String, +): String? { + val property = key + val environment = key + .uppercase() + .replace(".", "__") + + val prop = properties[property] as String? + val env = System.getenv(environment) + + return prop ?: env +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..bb704f3 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,5 @@ +[versions] +kotlin = "2.1.20" + +[plugins] +multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8ca0b58 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun May 18 00:11:37 CEST 2025 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..e5fc5a4 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "hash" diff --git a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt new file mode 100644 index 0000000..d7aef53 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt @@ -0,0 +1,7 @@ +package com.infendro.hash + +sealed class HashFunction { + abstract fun hash( + value: ByteArray, + ): ByteArray +} diff --git a/src/commonMain/kotlin/com/infendro/hash/MD5.kt b/src/commonMain/kotlin/com/infendro/hash/MD5.kt new file mode 100644 index 0000000..c2eea38 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/MD5.kt @@ -0,0 +1,109 @@ +package com.infendro.hash + +import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toUIntArray + +object MD5 : HashFunction() { + private val s = arrayOf( + 7, 12, 17, 22, + 7, 12, 17, 22, + 7, 12, 17, 22, + 7, 12, 17, 22, + 5, 9, 14, 20, + 5, 9, 14, 20, + 5, 9, 14, 20, + 5, 9, 14, 20, + 4, 11, 16, 23, + 4, 11, 16, 23, + 4, 11, 16, 23, + 4, 11, 16, 23, + 6, 10, 15, 21, + 6, 10, 15, 21, + 6, 10, 15, 21, + 6, 10, 15, 21, + ) + + private val k = arrayOf( + 0xd76aa478U, 0xe8c7b756U, 0x242070dbU, 0xc1bdceeeU, + 0xf57c0fafU, 0x4787c62aU, 0xa8304613U, 0xfd469501U, + 0x698098d8U, 0x8b44f7afU, 0xffff5bb1U, 0x895cd7beU, + 0x6b901122U, 0xfd987193U, 0xa679438eU, 0x49b40821U, + 0xf61e2562U, 0xc040b340U, 0x265e5a51U, 0xe9b6c7aaU, + 0xd62f105dU, 0x02441453U, 0xd8a1e681U, 0xe7d3fbc8U, + 0x21e1cde6U, 0xc33707d6U, 0xf4d50d87U, 0x455a14edU, + 0xa9e3e905U, 0xfcefa3f8U, 0x676f02d9U, 0x8d2a4c8aU, + 0xfffa3942U, 0x8771f681U, 0x6d9d6122U, 0xfde5380cU, + 0xa4beea44U, 0x4bdecfa9U, 0xf6bb4b60U, 0xbebfbc70U, + 0x289b7ec6U, 0xeaa127faU, 0xd4ef3085U, 0x04881d05U, + 0xd9d4d039U, 0xe6db99e5U, 0x1fa27cf8U, 0xc4ac5665U, + 0xf4292244U, 0x432aff97U, 0xab9423a7U, 0xfc93a039U, + 0x655b59c3U, 0x8f0ccc92U, 0xffeff47dU, 0x85845dd1U, + 0x6fa87e4fU, 0xfe2ce6e0U, 0xa3014314U, 0x4e0811a1U, + 0xf7537e82U, 0xbd3af235U, 0x2ad7d2bbU, 0xeb86d391U, + ) + + override fun hash( + value: ByteArray, + ): ByteArray { + var a0 = 0x67452301U + var b0 = 0xefcdab89U + var c0 = 0x98badcfeU + var d0 = 0x10325476U + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 64 != 56) { + add(0x00.toByte()) + } + addAll(ml.toByteArray(LITTLE_ENDIAN)) + } + + val chunks = paddedValue + .chunked(64) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toUIntArray(LITTLE_ENDIAN)) + } + + var a = a0 + var b = b0 + var c = c0 + var d = d0 + + for (i in 0..<64) { + val f = when (i) { + in 0..15 -> (b and c) or (b.inv() and d) + in 16..31 -> (d and b) or (d.inv() and c) + in 32..47 -> b xor c xor d + else -> c xor (b or d.inv()) + } + val g = when (i) { + in 0..15 -> i + in 16..31 -> (5 * i + 1) % 16 + in 32..47 -> (3 * i + 5) % 16 + else -> (7 * i) % 16 + } + a = d + d = c + c = b + b += (f + a + k[i] + w[g]).rotateLeft(s[i]) + } + + a0 += a + b0 += b + c0 += c + d0 += d + } + + return byteArrayOf( + *a0.toByteArray(LITTLE_ENDIAN), + *b0.toByteArray(LITTLE_ENDIAN), + *c0.toByteArray(LITTLE_ENDIAN), + *d0.toByteArray(LITTLE_ENDIAN), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA1.kt b/src/commonMain/kotlin/com/infendro/hash/SHA1.kt new file mode 100644 index 0000000..d2ca6cb --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/SHA1.kt @@ -0,0 +1,80 @@ +package com.infendro.hash + +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toUIntArray + +object SHA1 : HashFunction() { + override fun hash( + value: ByteArray, + ): ByteArray { + var h0 = 0x67452301U + var h1 = 0xefcdaB89U + var h2 = 0x98badcfeU + var h3 = 0x10325476U + var h4 = 0xc3d2e1f0U + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 64 != 56) { + add(0x00.toByte()) + } + addAll(ml.toByteArray()) + } + + val chunks = paddedValue + .chunked(64) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toUIntArray()) + for (i in 16..<80) { + val word = (get(i - 3) xor get(i - 8) xor get(i - 14) xor get(i - 16)).rotateLeft(1) + add(word) + } + } + + var a = h0 + var b = h1 + var c = h2 + var d = h3 + var e = h4 + + for (i in 0..<80) { + val f = when (i) { + in 0..<20 -> (b and c) or (b.inv() and d) + in 20..<40 -> b xor c xor d + in 40..<60 -> (b and c) or (b and d) or (c and d) + else -> b xor c xor d + } + val k = when (i) { + in 0..<20 -> 0x5a827999U + in 20..<40 -> 0x6ed9eba1U + in 40..<60 -> 0x8f1bbcdcU + else -> 0xca62c1d6U + } + val temp = a.rotateLeft(5) + f + e + k + w[i] + e = d + d = c + c = b.rotateLeft(30) + b = a + a = temp + } + + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + } + + return byteArrayOf( + *h0.toByteArray(), + *h1.toByteArray(), + *h2.toByteArray(), + *h3.toByteArray(), + *h4.toByteArray(), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA224.kt b/src/commonMain/kotlin/com/infendro/hash/SHA224.kt new file mode 100644 index 0000000..31b29ef --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/SHA224.kt @@ -0,0 +1,100 @@ +package com.infendro.hash + +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toUIntArray + +object SHA224 : HashFunction() { + private val k = arrayOf( + 0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U, + 0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U, + 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, + 0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U, + 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U, + 0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U, + 0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U, + 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U, + ) + + override fun hash( + value: ByteArray, + ): ByteArray { + var h0 = 0xc1059ed8U + var h1 = 0x367cd507U + var h2 = 0x3070dd17U + var h3 = 0xf70e5939U + var h4 = 0xffc00b31U + var h5 = 0x68581511U + var h6 = 0x64f98fa7U + var h7 = 0xbefa4fa4U + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 64 != 56) { + add(0x00.toByte()) + } + addAll(ml.toByteArray()) + } + + val chunks = paddedValue + .chunked(64) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toUIntArray()) + for (i in 16..<64) { + val s0 = get(i - 15).rotateRight(7) xor get(i - 15).rotateRight(18) xor (get(i - 15) shr 3) + val s1 = get(i - 2).rotateRight(17) xor get(i - 2).rotateRight(19) xor (get(i - 2) shr 10) + add(get(i - 16) + s0 + get(i - 7) + s1) + } + } + + var a = h0 + var b = h1 + var c = h2 + var d = h3 + var e = h4 + var f = h5 + var g = h6 + var h = h7 + + for (i in 0..<64) { + val s1 = e.rotateRight(6) xor e.rotateRight(11) xor e.rotateRight(25) + val ch = (e and f) xor (e.inv() and g) + val temp1 = h + s1 + ch + k[i] + w[i] + val s0 = a.rotateRight(2) xor a.rotateRight(13) xor a.rotateRight(22) + val maj = (a and b) xor (a and c) xor (b and c) + val temp2 = s0 + maj + + h = g + g = f + f = e + e = d + temp1 + d = c + c = b + b = a + a = temp1 + temp2 + } + + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + } + + return byteArrayOf( + *h0.toByteArray(), + *h1.toByteArray(), + *h2.toByteArray(), + *h3.toByteArray(), + *h4.toByteArray(), + *h5.toByteArray(), + *h6.toByteArray(), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA256.kt b/src/commonMain/kotlin/com/infendro/hash/SHA256.kt new file mode 100644 index 0000000..46f69a6 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/SHA256.kt @@ -0,0 +1,101 @@ +package com.infendro.hash + +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toUIntArray + +object SHA256 : HashFunction() { + private val k = arrayOf( + 0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U, + 0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U, + 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, + 0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U, + 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U, + 0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U, + 0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U, + 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U, + ) + + override fun hash( + value: ByteArray, + ): ByteArray { + var h0 = 0x6a09e667U + var h1 = 0xbb67ae85U + var h2 = 0x3c6ef372U + var h3 = 0xa54ff53aU + var h4 = 0x510e527fU + var h5 = 0x9b05688cU + var h6 = 0x1f83d9abU + var h7 = 0x5be0cd19U + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 64 != 56) { + add(0x00.toByte()) + } + addAll(ml.toByteArray()) + } + + val chunks = paddedValue + .chunked(64) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toUIntArray()) + for (i in 16..<64) { + val s0 = get(i - 15).rotateRight(7) xor get(i - 15).rotateRight(18) xor (get(i - 15) shr 3) + val s1 = get(i - 2).rotateRight(17) xor get(i - 2).rotateRight(19) xor (get(i - 2) shr 10) + add(get(i - 16) + s0 + get(i - 7) + s1) + } + } + + var a = h0 + var b = h1 + var c = h2 + var d = h3 + var e = h4 + var f = h5 + var g = h6 + var h = h7 + + for (i in 0..<64) { + val s0 = a.rotateRight(2) xor a.rotateRight(13) xor a.rotateRight(22) + val s1 = e.rotateRight(6) xor e.rotateRight(11) xor e.rotateRight(25) + val ch = (e and f) xor (e.inv() and g) + val temp1 = h + s1 + ch + k[i] + w[i] + val maj = (a and b) xor (a and c) xor (b and c) + val temp2 = s0 + maj + + h = g + g = f + f = e + e = d + temp1 + d = c + c = b + b = a + a = temp1 + temp2 + } + + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + } + + return byteArrayOf( + *h0.toByteArray(), + *h1.toByteArray(), + *h2.toByteArray(), + *h3.toByteArray(), + *h4.toByteArray(), + *h5.toByteArray(), + *h6.toByteArray(), + *h7.toByteArray(), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA384.kt b/src/commonMain/kotlin/com/infendro/hash/SHA384.kt new file mode 100644 index 0000000..d069bd4 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/SHA384.kt @@ -0,0 +1,107 @@ +package com.infendro.hash + +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toULongArray + +object SHA384 : HashFunction() { + private val k = arrayOf( + 0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL, + 0x59f111f1b605d019UL, 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL, 0x12835b0145706fbeUL, + 0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL, 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL, + 0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL, + 0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL, 0x983e5152ee66dfabUL, + 0xa831c66d2db43210UL, 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL, + 0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL, + 0x53380d139d95b3dfUL, 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL, 0x92722c851482353bUL, + 0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL, + 0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL, 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL, + 0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL, + 0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL, + 0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL, 0xca273eceea26619cUL, + 0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL, + 0x113f9804bef90daeUL, 0x1b710b35131c471bUL, 0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL, + 0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL, + ) + + override fun hash( + value: ByteArray, + ): ByteArray { + var h0 = 0xcbbb9d5dc1059ed8UL + var h1 = 0x629a292a367cd507UL + var h2 = 0x9159015a3070dd17UL + var h3 = 0x152fecd8f70e5939UL + var h4 = 0x67332667ffc00b31UL + var h5 = 0x8eb44a8768581511UL + var h6 = 0xdb0c2e0d64f98fa7UL + var h7 = 0x47b5481dbefa4fa4UL + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 128 != 120) { + add(0x00.toByte()) + } + addAll(ml.toByteArray()) + } + + val chunks = paddedValue + .chunked(128) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toULongArray()) + for (i in 16..<80) { + val s0 = get(i - 15).rotateRight(1) xor get(i - 15).rotateRight(8) xor (get(i - 15) shr 7) + val s1 = get(i - 2).rotateRight(19) xor get(i - 2).rotateRight(61) xor (get(i - 2) shr 6) + add(get(i - 16) + s0 + get(i - 7) + s1) + } + } + + var a = h0 + var b = h1 + var c = h2 + var d = h3 + var e = h4 + var f = h5 + var g = h6 + var h = h7 + + for (i in 0..<80) { + val s0 = a.rotateRight(28) xor a.rotateRight(34) xor a.rotateRight(39) + val s1 = e.rotateRight(14) xor e.rotateRight(18) xor e.rotateRight(41) + val ch = (e and f) xor (e.inv() and g) + val temp1 = h + s1 + ch + k[i] + w[i] + val maj = (a and b) xor (a and c) xor (b and c) + val temp2 = s0 + maj + + h = g + g = f + f = e + e = d + temp1 + d = c + c = b + b = a + a = temp1 + temp2 + } + + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + } + + return byteArrayOf( + *h0.toByteArray(), + *h1.toByteArray(), + *h2.toByteArray(), + *h3.toByteArray(), + *h4.toByteArray(), + *h5.toByteArray(), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/SHA512.kt b/src/commonMain/kotlin/com/infendro/hash/SHA512.kt new file mode 100644 index 0000000..b3f45ad --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/SHA512.kt @@ -0,0 +1,109 @@ +package com.infendro.hash + +import com.infendro.hash.util.addAll +import com.infendro.hash.util.toByteArray +import com.infendro.hash.util.toULongArray + +object SHA512 : HashFunction() { + private val k = arrayOf( + 0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL, + 0x59f111f1b605d019UL, 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL, 0x12835b0145706fbeUL, + 0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL, 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL, + 0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL, + 0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL, 0x983e5152ee66dfabUL, + 0xa831c66d2db43210UL, 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL, + 0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL, + 0x53380d139d95b3dfUL, 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL, 0x92722c851482353bUL, + 0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL, + 0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL, 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL, + 0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL, + 0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL, + 0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL, 0xca273eceea26619cUL, + 0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL, + 0x113f9804bef90daeUL, 0x1b710b35131c471bUL, 0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL, + 0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL, + ) + + override fun hash( + value: ByteArray, + ): ByteArray { + var h0 = 0x6a09e667f3bcc908UL + var h1 = 0xbb67ae8584caa73bUL + var h2 = 0x3c6ef372fe94f82bUL + var h3 = 0xa54ff53a5f1d36f1UL + var h4 = 0x510e527fade682d1UL + var h5 = 0x9b05688c2b3e6c1fUL + var h6 = 0x1f83d9abfb41bd6bUL + var h7 = 0x5be0cd19137e2179UL + val ml = value.size.toULong() * 8UL + + val paddedValue = buildList { + addAll(value) + add(0x80.toByte()) + while (size % 128 != 120) { + add(0x00.toByte()) + } + addAll(ml.toByteArray()) + } + + val chunks = paddedValue + .chunked(128) + for (chunk in chunks) { + val w = buildList { + addAll(chunk.toULongArray()) + for (i in 16..<80) { + val s0 = get(i - 15).rotateRight(1) xor get(i - 15).rotateRight(8) xor (get(i - 15) shr 7) + val s1 = get(i - 2).rotateRight(19) xor get(i - 2).rotateRight(61) xor (get(i - 2) shr 6) + add(get(i - 16) + s0 + get(i - 7) + s1) + } + } + + var a = h0 + var b = h1 + var c = h2 + var d = h3 + var e = h4 + var f = h5 + var g = h6 + var h = h7 + + for (i in 0..<80) { + val s0 = a.rotateRight(28) xor a.rotateRight(34) xor a.rotateRight(39) + val s1 = e.rotateRight(14) xor e.rotateRight(18) xor e.rotateRight(41) + val ch = (e and f) xor (e.inv() and g) + val temp1 = h + s1 + ch + k[i] + w[i] + val maj = (a and b) xor (a and c) xor (b and c) + val temp2 = s0 + maj + + h = g + g = f + f = e + e = d + temp1 + d = c + c = b + b = a + a = temp1 + temp2 + } + + h0 += a + h1 += b + h2 += c + h3 += d + h4 += e + h5 += f + h6 += g + h7 += h + } + + return byteArrayOf( + *h0.toByteArray(), + *h1.toByteArray(), + *h2.toByteArray(), + *h3.toByteArray(), + *h4.toByteArray(), + *h5.toByteArray(), + *h6.toByteArray(), + *h7.toByteArray(), + ) + } +} diff --git a/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt b/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt new file mode 100644 index 0000000..718f8d5 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/hash/util/ByteArray.kt @@ -0,0 +1,119 @@ +package com.infendro.hash.util + +import com.infendro.hash.util.ByteOrder.BIG_ENDIAN +import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN + +internal fun MutableCollection.addAll( + bytes: ByteArray, +) { + addAll(bytes.toList()) +} + +enum class ByteOrder { + BIG_ENDIAN, + LITTLE_ENDIAN, +} + +internal fun UInt.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = ByteArray(4) { i -> + val offset = (3 - i) * 8 + (this shr offset).toByte() + } + + return when (order) { + BIG_ENDIAN -> bytes + LITTLE_ENDIAN -> bytes.reversedArray() + } +} + +internal fun ULong.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = ByteArray(8) { i -> + val offset = (7 - i) * 8 + (this shr offset).toByte() + } + + return when (order) { + BIG_ENDIAN -> bytes + LITTLE_ENDIAN -> bytes.reversedArray() + } +} + +internal fun ByteArray.toUInt( + order: ByteOrder = BIG_ENDIAN, +): UInt { + if (size != 4) throw Exception() + + val bytes = when (order) { + BIG_ENDIAN -> this + LITTLE_ENDIAN -> this.reversedArray() + } + + return bytes.fold(0U) { acc, byte -> + (acc shl 8) or (byte.toUInt() and 0xFFU) + } +} + +internal fun Collection.toUInt( + order: ByteOrder = BIG_ENDIAN, +): UInt { + return toByteArray().toUInt(order) +} + +internal fun ByteArray.toULong( + order: ByteOrder = BIG_ENDIAN, +): ULong { + if (size != 8) throw Exception() + + val bytes = when (order) { + BIG_ENDIAN -> this + LITTLE_ENDIAN -> this.reversedArray() + } + + return bytes.fold(0UL) { acc, byte -> + (acc shl 8) or (byte.toULong() and 0xFFUL) + } +} + +internal fun Collection.toULong( + order: ByteOrder = BIG_ENDIAN, +): ULong { + return toByteArray().toULong(order) +} + +internal fun ByteArray.toUIntArray( + order: ByteOrder = BIG_ENDIAN, +): UIntArray { + if (size % 4 != 0) throw Exception() + + return toList() + .chunked(4) + .map { it.toUInt(order) } + .toUIntArray() +} + +internal fun Collection.toUIntArray( + order: ByteOrder = BIG_ENDIAN, +): UIntArray { + return toByteArray().toUIntArray(order) +} + +internal fun ByteArray.toULongArray( + order: ByteOrder = BIG_ENDIAN, +): ULongArray { + if (size % 8 != 0) throw Exception() + + return toList() + .chunked(8) + .map { it.toULong(order) } + .toULongArray() +} + +internal fun Collection.toULongArray( + order: ByteOrder = BIG_ENDIAN, +): ULongArray { + return toByteArray().toULongArray(order) +}