Private Key Security – How to Validate a Private Key

accountsprivate-keySecurity

I could not find any library that, given an hex value, returns true if the given string is a valid ether private key.

Any hints?

Best Answer

As mentioned by Peter, a private key is a random 256 bit blob. It is a common oversight that there're no restrictions.

It has to be valid for the secp256k1 curve, which means two conditions:

  • cannot be zero
  • must be less than the order of the curve (called n and has a value of ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141)

In Javascript you can use ethereumjs-wallet to do this verification: Wallet.fromPrivateKey(<yourkey>) will throw an exception on an invalid input.

Alternatively you can also use the privateKeyVerify() method from the underlying library secp256k1 or do it manually with the big number library of your choice.