terl / lazysodium-java

A Java implementation of the Libsodium crypto library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
135 stars 47 forks source link

Null aware library #79

Closed emartynov closed 3 years ago

emartynov commented 4 years ago

Hi people,

we are android devs and we use kotlin mostly. As for now we have code like this:

return Encryption.decrypt(it) ?: default

It returns default value if decrypt returns null. But I believe you don't return nulls and will throw exception. Also I see that a lot of methods are not accepting null params.

Could you annotate your function params and return values with Null aware annotations? If you follow non null defaults then you can do it for the whole package like described here https://stancalau.ro/java-package-nullability-contract/. This is supported by Kotlin compiler as well.

emartynov commented 4 years ago

Any thoughts people?

gurpreet- commented 3 years ago

Sorry was caught up in some covid stuff.

Lazysodium definitely needs a Kotlin version. Android API 29 introduces a lot of nullability checks doesn't it? I think the recommended way is to use annotations.

1) I need to get this library up-to-date with API 29 using annotations (though this mixes Android with Java which is something I'm hesitant to do). 2) Get Kotlin working with Lazysodium.

gurpreet- commented 3 years ago

Hi, I just tried using Lazysodium Java with Kotlin and all the methods return either a concrete non-nullable type or an exception. That is to say, the following works:

val lazySodium = LazySodiumAndroid(SodiumAndroid())

val message = "This is a super secret message."
val key = lazySodium.cryptoSecretBoxKeygen()
val nonce = lazySodium.nonce(SecretBox.NONCEBYTES)
val cipher = lazySodium.cryptoSecretBoxEasy(message, nonce, key)
val decrypted = lazySodium.cryptoSecretBoxOpenEasy(cipher, nonce, key)
Log.e("TESTAPP", decrypted) // Returns "This is a super secret message"

So by virtue of that, this is compatible with Android API 29+ and does not need extra work.

If you're still stuck, can you give me some sample problematic code?

gurpreet- commented 3 years ago

Closing, cleaning up.