How to generate private key using Kotlin

SANDEEP DUTTA
1 min readApr 30, 2021

Q. What is Bitcoin Address ?

Ans: A Bitcoin address is a random string of hexadecimal characters generated based on certain algorithm that are used within the Bitcoin network to send and receive bitcoins.

Technically speaking private key is a 256 random bits generated by using a hashing algorithm based on SHA256 . The bitcoin address is generated from the public part of the ECDSA ,hashed using SHA-256 and RIPEMD-160 and then finally encoding the key using Base58 checked encoding.

Generating the private key:

For a particular given string value we need to generate a private key. The algorithm to do so can be implemented as below.

fun getSHA(input: String): String? {
try {
val md = MessageDigest.getInstance(“SHA-256”)
val messageDigest = md.digest(input.toByteArray())
val num = BigInteger(1, messageDigest)
var hashText = num.toString(16)
while (hashText.length < 32) {
hashText = “0$hashText
}
return hashText
} catch (ex: NoSuchAlgorithmException) {
println(“Exception Occured: ${ex.message}”)
return null
}
}

The output generated by the code:

Input : hello

Private Key: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

--

--