korge icon indicating copy to clipboard operation
korge copied to clipboard

Krypto buffered stream hashing extension functions

Open solonovamax opened this issue 2 years ago • 4 comments

I noticed that currently, krypto only has extension functions to compute the hash of a ByteArray. However, it is not too difficult to implement said hashing extension functions, and would be a good quality of life inclusion.

Using the JVM-only InputStream:

fun InputStream.hash(algo: HasherFactory): Hash = algo.digest { read(it) }

fun InputStream.sha1() = hash(SHA1)

fun InputStream.md4() = hash(MD4)

fun InputStream.md5() = hash(MD5)

fun InputStream.sha256() = hash(SHA256)

fun InputStream.sha512() = hash(SHA512)

There is also okio, which provides Source and BufferedSource:

fun BufferedSource.hash(algo: HasherFactory): Hash = algo.digest { read(it) }

fun BufferedSource.sha1() = hash(SHA1)

fun BufferedSource.md4() = hash(MD4)

fun BufferedSource.md5() = hash(MD5)

fun BufferedSource.sha256() = hash(SHA256)

fun BufferedSource.sha512() = hash(SHA512)

If these could both be included in the jvm only source set, that would be great. Additionally, extensions for korio can also be included in the common source set.

solonovamax avatar Jun 16 '23 19:06 solonovamax

It is possible to add for InputStream to the JVM yes.

Also note that there are extensions for synchronous and asynchronous KorIO streams: https://github.com/korlibs/korge/blob/e2081e0e2af6f3432ac961ca636abd3c6ed7ddbf/korio/src/commonMain/kotlin/korlibs/io/hash/HashExt.kt#L14-L24

soywiz avatar Jun 16 '23 20:06 soywiz

I'm not using korio in my project, as I found the lack of documentation on everything to make it significantly more difficult to use.

Additionally, I find korio less intuitive to use in comparison to other methods of IO. If it were easier to use and had the documentation significantly improved, I'd be much more inclined to use it over the alternatives.

solonovamax avatar Aug 25 '23 11:08 solonovamax

I can submit a PR with those additions, if you'd like, however due to how short they were, I felt it'd be easiest to leave it as an issue and let a maintainer add them

solonovamax avatar Aug 25 '23 12:08 solonovamax

We are shorthanded, so feel free to create a PR.

We currently do not have contribution guidelines in place. Therefore, please remember to thoroughly document all public functions and create tests with clear and explicit names.

ygdrasil-io avatar Aug 26 '23 11:08 ygdrasil-io