Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun main(args: Array<String>) {
val digest = MessageDigest.getInstance("SHA-256")
args.forEach { digest.update(it.toLowerCase().toByteArray(Charset.forName("UTF-8"))) }
println(digest.digest().toHexString())
}
/**
* Set of chars for a half-byte.
*/
private val CHARS = arrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
/**
* Returns the string of two characters representing the HEX value of the byte.
*/
internal fun Byte.toHexString(): String {
val i = this.toInt()
val char2 = CHARS[i and 0x0f]
val char1 = CHARS[i shr 4 and 0x0f]
return "$char1$char2"
}
/**
* Returns the HEX representation of ByteArray data.
*/
internal fun ByteArray.toHexString(): String {
val builder = StringBuilder()
for (b in this) {
builder.append(b.toHexString())
}
return builder.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment