Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Krypto buffered stream hashing extension functions #1710

Open
solonovamax opened this issue Jun 16, 2023 · 4 comments
Open

Krypto buffered stream hashing extension functions #1710

solonovamax opened this issue Jun 16, 2023 · 4 comments

Comments

@solonovamax
Copy link

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.

@soywiz
Copy link
Member

soywiz commented Jun 16, 2023

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

Also note that there are extensions for synchronous and asynchronous KorIO streams:

suspend fun AsyncInputOpenable.hash(algo: HasherFactory) = openRead().use { hash(algo) }
suspend fun AsyncInputStream.hash(algo: HasherFactory): Hash = bytesTempPool.alloc { temp -> algo.digest(temp) { read(it) } }
fun SyncInputStream.hash(algo: HasherFactory): Hash = bytesTempPool.alloc { temp -> algo.digest(temp) { read(it) } }
suspend fun AsyncInputOpenable.md5() = hash(MD5)
suspend fun AsyncInputStream.md5() = hash(MD5)
fun SyncInputStream.md5() = hash(MD5)
suspend fun AsyncInputOpenable.sha1() = hash(SHA1)
suspend fun AsyncInputStream.sha1() = hash(SHA1)
fun SyncInputStream.sha1() = hash(SHA1)

@solonovamax
Copy link
Author

solonovamax commented Aug 25, 2023

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
Copy link
Author

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

@ygdrasil-io
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Pending
Development

No branches or pull requests

3 participants