Regular expression for base 58 private key?

cilphex picture cilphex · May 24, 2013 · Viewed 8.4k times · Source

I'm looking for a regex that will validate a base 58 bitcoin private key. I found this one for public addresses:

/^[13n][1-9A-Za-z][^OIl]{20,40}/

But I don't know what the requirements are for a private key.

Answer

user3074620 picture user3074620 · Oct 11, 2015
var regex = /^[5KL][1-9A-HJ-NP-Za-km-z]{50,51}$/

In javascript.

All valid WIF private keys will match this, and the only extra stuff this will match is "WIF private key"-ish strings with invalid checksums.

Shortest possible WIF key (all 00 bytes, missing the compressed 01 byte) 51 length 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAbuatmU

Longest possible WIF key (all ff bytes with the extra 01 compression byte) 52 length L5oLkpV3aqBjhki6LmvChTCq73v9gyymzzMpBbhDLjDpKCuAXpsi