Skip to content

Instantly share code, notes, and snippets.

@labibmuhajir
Created October 13, 2022 08:12
Show Gist options
  • Save labibmuhajir/99aaeeab28c6b2fa613d09300d576707 to your computer and use it in GitHub Desktop.
Save labibmuhajir/99aaeeab28c6b2fa613d09300d576707 to your computer and use it in GitHub Desktop.
sha-256
fun String.getSha256(): String {
val digest = MessageDigest.getInstance("SHA-256").apply { reset() }
val byteData: ByteArray = digest.digest(toByteArray())
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Base64.getEncoder().encodeToString(byteData)
} else {
String(
android.util.Base64.encode(byteData, android.util.Base64.DEFAULT),
StandardCharsets.UTF_8
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment