Skip to content

Instantly share code, notes, and snippets.

@gadiener
Last active September 20, 2023 22:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gadiener/ebec8b39b15293fbc438b2d21b211dfe to your computer and use it in GitHub Desktop.
Save gadiener/ebec8b39b15293fbc438b2d21b211dfe to your computer and use it in GitHub Desktop.
OpenSSL commands to create a Bitcoin private/public keys from a ECDSA keypair
#!/bin/sh
PRIVATE_KEY="ECDSA"
PUBLIC_KEY="ECDSA.pub"
BITCOIN_PRIVATE_KEY="bitcoin"
BITCOIN_PUBLIC_KEY="bitcoin.pub"
echo "Generating private key"
openssl ecparam -genkey -name secp256k1 -rand /dev/random -out $PRIVATE_KEY
echo "Generating public key"
openssl ec -in $PRIVATE_KEY -pubout -out $PUBLIC_KEY
echo "Generating Bitcoin private key"
openssl ec -in $PRIVATE_KEY -outform DER|tail -c +8|head -c 32|xxd -p -c 32 > $BITCOIN_PRIVATE_KEY
echo "Generating Bitcoin public key"
openssl ec -in $PRIVATE_KEY -pubout -outform DER|tail -c 65|xxd -p -c 65 > $BITCOIN_PUBLIC_KEY
@GregoryTrzonkowski
Copy link

  1. BITCOIN_PRIVATE_KEY name may be misleading. There is the hex representation of ECDSA key in the script above. In contrast Bitcoin private keys are typically represented in WIF format (Base58Check encoding) like exported from Bitcoin Core and imported by many wallets.
  2. I would find it useful to add bitcoin address (preferably P2WPKH by default and P2TR, P2SH, P2PKH as options) in the script. Can I ask to develop it, please?

@gadiener
Copy link
Author

Hey @GregoryTrzonkowski, thanks for the feedback. Well appreciated.

Feel free to implement the changes you mention if you like, I'll be happy to update the script accordingly.

@GregoryTrzonkowski
Copy link

Random State Options
Prior to OpenSSL 1.1.1, it was common for applications to store information about the state of the random-number generator in a file that was loaded at startup and rewritten upon exit. On modern operating systems, this is generally no longer necessary as OpenSSL will seed itself from a trusted entropy source provided by the operating system. These flags are still supported for special platforms or circumstances that might require them.

Source: https://www.openssl.org/docs/man3.0/man1/openssl.html

Is there a reason to include the part of the command: -rand /dev/random?

@gadiener
Copy link
Author

@GregoryTrzonkowski unfortunately I don't really remember as the gist is 2 years old. But looking at the documentation you quoted, seems that it's not needed

@GregTonoski
Copy link

  1. BITCOIN_PRIVATE_KEY name may be misleading. There is the hex representation of ECDSA key in the script above. In contrast Bitcoin private keys are typically represented in WIF format (Base58Check encoding) like exported from Bitcoin Core and imported by many wallets.

There is the script that converts keys genereted by OpenSSL to Bitcoin Wallet Import Format (WIF): https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a

@JungWoo23
Copy link

how to hack bitcoin

@larrydemirx
Copy link

Hi,I want to generate a password with bitcoin hash string.Is there any openssl script or command that I can use in the terminal for this.Thank you in advance for your help.

@JungWoo23
Copy link

JungWoo23 commented Dec 29, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment