<p>The base58 private key format includes an SHA256-based checksum.  See <a href="https://bitcoin.stackexchange.com/a/8249/5757">this answer</a> for details (disregard the part of the question where the private key itself is the SHA256 hash of some other data).  </p>

CC BY-SA 3.0
2014-08-16T16:53:42.287Z
2014-08-16T16:53:42.287Z
2017-04-13T12:47:33.743Z
Nate Eldredge
<p>I have a <code>BigInteger</code> that represents a private key. How can I get that private key as a base58 encoded string? The following pseudo code does not work properly:</p>

<pre><code>base58.encode(bigInt.toByteArray) 
// yields a string that differs from given bigInt's private key
</code></pre>

<p>UPDATE  </p>

<pre><code>def fullDoubleSha(bytes: ArrayOfByte) = {
  val digestSha = MessageDigest getInstance "SHA-256"
  digestSha digest digestSha.digest(bytes)
}

def getPrivBytes(key: BigInteger) = key.toByteArray match { case keyBytes =&gt;
  val res = if (keyBytes.head == 0) keyBytes drop 1 else keyBytes
  new Array(32 - res.length) ++ res
}

def privBytesToWif(vs: ArrayOfByte) = {
  val rawPrivKey = Array(0x80.toByte) ++ vs
  rawPrivKey ++ fullDoubleSha(rawPrivKey).take(4)
}
</code></pre>

<development><private-key><base58>
Get private key from BigInteger
src091
# Get private key from BigInteger

(Asked by: username_0 on 2014-08-16)

I have a `BigInteger` that represents a private key. How can I get that private key as a base58 encoded string? The following pseudo code does not work properly:

```
base58.encode(bigInt.toByteArray) 
// yields a string that differs from given bigInt's private key

```

UPDATE

```
def fullDoubleSha(bytes: ArrayOfByte) = {
  val digestSha = MessageDigest getInstance "SHA-256"
  digestSha digest digestSha.digest(bytes)
}

def getPrivBytes(key: BigInteger) = key.toByteArray match { case keyBytes =>
  val res = if (keyBytes.head == 0) keyBytes drop 1 else keyBytes
  new Array(32 - res.length) ++ res
}

def privBytesToWif(vs: ArrayOfByte) = {
  val rawPrivKey = Array(0x80.toByte) ++ vs
  rawPrivKey ++ fullDoubleSha(rawPrivKey).take(4)
}

```

**Tags:** development, private-key, base58


---

## 1 answers

### Answer by username_1 on 2014-08-16. Score: 1 | Accepted answer

The base58 private key format includes an SHA256-based checksum. See [this answer](https://bitcoin.stackexchange.com/a/8249/5757) for details (disregard the part of the question where the private key itself is the SHA256 hash of some other data).


---

