hafiz
02/05/2025, 5:38 AMjava.lang.IllegalStateException: No providers registered. Please provide a dependency or register provider explicitly
Sample code :
fun encrypt(plainText: String): ByteArray =
runBlocking {
CryptographyProvider.Default
.get(SHA512)
.hasher()
.hash(plainText.encodeToByteArray())
}
library use:
dev.whyoleg.cryptography
how to solve this problemMoussa
02/05/2025, 10:03 AMhafiz
02/06/2025, 2:39 AMMoussa
02/06/2025, 3:44 AMMoussa
02/06/2025, 3:56 AMcommonMain
// SHA-224, SHA-256, SHA-384, SHA-512
// SHA-512/t, SHA-512/224, SHA-512/256
implementation("org.kotlincrypto.hash:sha2:0.6.0")
Then in method implementation would be
fun encrypt(plainText: String): ByteArray =
runBlocking {
val hasher = SHA256()
val hashedByteArrray = hasher.digest(plainText.encodeToByteArray())
}
hafiz
02/06/2025, 4:39 AMMoussa
02/06/2025, 5:12 AMBase64.Default.withPadding(Base64.PaddingOption.PRESENT).encode(hashedByteArray)
hafiz
02/06/2025, 5:49 AMMoussa
02/06/2025, 5:51 AMBase64.Default.withPadding(Base64.PaddingOption.PRESENT).decode(encodedData)
Where encodedData
is the Base64 encoded String which will return a ByteArray
which in your example would be the hashedByteArrray
hafiz
02/06/2025, 5:53 AMBase64.Default.withPadding(Base64.PaddingOption.PRESENT).decode(encoded).decodeToString()
is that correct?hafiz
02/06/2025, 5:55 AMhafiz
02/06/2025, 5:59 AMMoussa
02/06/2025, 6:17 AMval plainText: String = "Hello World!"
val encodedData = Base64.Default.withPadding(Base64.PaddingOption.PRESENT).encode(plainText)
val decodedData = Base64.Default.withPadding(Base64.PaddingOption.PRESENT).decode(encodedData).decodeToString() // returns "Hello World!" which is `plainText`
But, in your case, it is the result of a hash
which means it is the ByteArray
representation of the hash
which should not be converted to a normal string
it should be encoded with something like Base64
encoding.
example
// NOTE: hash is a one way cryptography, there is no mathematical decryption of a hash.
// NOTE: every time you give the same `plainText` to the same `hash` you will get the same value.
// This method will hash the `plainText` and return the hashed value.
fun sha256Hash(plainText: String): ByteArray =
runBlocking {
val hasher = SHA256()
val hashedByteArrray = hasher.digest(plainText.encodeToByteArray())
return hashedByteArrray
}
// This method will hash the given `plainText` then encode it in Base64 encoding and return it.
fun sha256HashEncodingBase64(plainText: String): String =
runBlocking {
val hasher = SHA256()
val hashedByteArrray = hasher.digest(plainText.encodeToByteArray())
val base64Encoded = Base64.Default.withPadding(Base64.PaddingOption.PRESENT).encode(hashedByteArray)
return base64Encoded
}
Using sha256HashEncodingBase64
method will return a Base64
encoding string of the hashed value.
If you want to verify an existing hash value, you would need 3 things:
• The hashed value
• The plain text value
• The used hash method (In you case the hash method is SHA256
)
// This method takes the plaintext and the hashed byte array which in this example the result of `sha256Hash` method.
fun verifySha256(plainText: String, hashedByteArrray: ByteArray): Boolean {
val hashed = sha256Hash(plainText)
return hashedByteArrray.contentEquals(hashed)
}
// This method takes the plaintext and the encoded hashed byte array which in this example the result of `sha256HashEncodingBase64` method.
fun verifySha256(plainText: String, encodedHashed: String): Boolean {
val encoded = sha256HashEncodingBase64(plainText)
return encoded == encodedHashed
}
hafiz
02/06/2025, 6:45 AMMoussa
02/06/2025, 6:48 AMhashkey
, what do you mean?
• Also, it would be very helpful for me in order to better help you if I understood you use case or at least have a better understanding of what you are trying to accomplish so I can better help you.hafiz
02/06/2025, 6:52 AMMoussa
02/06/2025, 7:06 AMSHA256
) that scrambles the data into a short, fixed-size string of characters, the "hash". There is no way to convert the hashed value to it's plain text. For that, you would use an Encryption like AES
encryption.hafiz
02/06/2025, 7:09 AMAES
encryption do you have example of it? thanksMoussa
02/06/2025, 7:34 AMAES
. But, you can use the expect/actual
feature in KMP to have a method for encryption and another one for decryption and you can implement the platform logic using their existing cryptography modules