-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2914613
commit feaba2a
Showing
7 changed files
with
108 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/src/jvmMain/kotlin/dev/schlaubi/stdx/core/HashUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@file:JvmName("HashUtilJvm") | ||
|
||
package dev.schlaubi.stdx.core | ||
|
||
import java.security.MessageDigest | ||
|
||
/** | ||
* Hashes this [String] using `SHA-256`. | ||
* | ||
* @see hash | ||
*/ | ||
public fun String.sha256(): String = hash("SHA-256") | ||
|
||
/** | ||
* Hashes this [ByteArray] using `SHA-256`. | ||
* | ||
* @see hash | ||
*/ | ||
public fun ByteArray.sha256(): String = hash("SHA-256") | ||
|
||
/** | ||
* Hashes this [String] using [algorithm]. | ||
* | ||
* @param algorithm the algorithm according to | ||
* [the Java spec](https://docs.oracle.com/en/java/javase/18/docs/specs/security/standard-names.html#messagedigest-algorithms) | ||
* | ||
* @see MessageDigest.digest | ||
*/ | ||
public fun String.hash(algorithm: String): String = toByteArray().hash(algorithm) | ||
|
||
/** | ||
* Hashes this [ByteArray] using [algorithm]. | ||
* | ||
* @param algorithm the algorithm according to | ||
* [the Java spec](https://docs.oracle.com/en/java/javase/18/docs/specs/security/standard-names.html#messagedigest-algorithms) | ||
* | ||
* @see MessageDigest.digest | ||
*/ | ||
public fun ByteArray.hash(algorithm: String): String { | ||
return MessageDigest | ||
.getInstance(algorithm) | ||
.digest(this) | ||
.fold("") { str, it -> str + "%02x".format(it) } | ||
} |
27 changes: 0 additions & 27 deletions
27
core/src/jvmMain/kotlin/dev/schlaubi/stdx/core/StringUtil.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters