Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active June 18, 2026 16:39
Show Gist options
  • Select an option

  • Save miguelmota/3793b160992b4ea0b616497b8e5aee2f to your computer and use it in GitHub Desktop.

Select an option

Save miguelmota/3793b160992b4ea0b616497b8e5aee2f to your computer and use it in GitHub Desktop.
Generate Ethereum Private key, Public key, and Address using Bash and OpenSSL
# Generate the private and public keys
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > key
# Extract the public key and remove the EC prefix 0x04
cat key | grep pub -A 5 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^04//' > pub
# Extract the private key and remove the leading zero byte
cat key | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//' > priv
# Generate the hash and take the address part
cat pub | keccak-256sum -x -l | tr -d ' -' | tail -c 41 > address
# (Optional) import the private key to geth
geth account import priv
@KasraAhmadi

Copy link
Copy Markdown

How to format public key to "secp256 compressed form - 66 characters" form?

@miguelmota

Copy link
Copy Markdown
Author

@Cheny-chen not sure how to do it with OpenSSL; here's an example using Node:

const KeyEncoder = require('key-encoder').default
const keyEncoder = new KeyEncoder('secp256k1')

const pubKey = Buffer.from('04997d9778d772bee04e01522c00d7d52371bd1a27cd87f6cf6f9c828d3a6d0d0da661b58f035aef01db4d7ad1c837ac28f3dec50f54bd351d090db6cb85069cc7', 'hex')

const pem = keyEncoder.encodePublic(pubKey.toString('hex'), 'raw' ,'pem').toString()
console.log(pem)
/*
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEmX2XeNdyvuBOAVIsANfVI3G9GifNh/bP
b5yCjTptDQ2mYbWPA1rvAdtNetHIN6wo897FD1S9NR0JDbbLhQacxw==
-----END PUBLIC KEY-----
*/

const raw = keyEncoder.encodePublic(pem, 'pem' ,'raw').toString('hex')
console.log(raw)
// 04997d9778d772bee04e01522c00d7d52371bd1a27cd87f6cf6f9c828d3a6d0d0da661b58f035aef01db4d7ad1c837ac28f3dec50f54bd351d090db6cb85069cc7

@avhacker

avhacker commented Nov 7, 2019

Copy link
Copy Markdown

The letters in public address in this script is all lower cased. Can you make it right? (I know lower case key still works)

@Afsha79

Afsha79 commented Jun 26, 2021

Copy link
Copy Markdown

Hi

@navdissenyo

Copy link
Copy Markdown

Hello, How to create eth private key from eth wallet address or public key?

@miguelmota

miguelmota commented Jul 6, 2021

Copy link
Copy Markdown
Author

@navdissenyo it's not possible to reconstruct a private key from the public key or address because they are generated using a one-way function.

@lau-bin

lau-bin commented Jan 24, 2022

Copy link
Copy Markdown

you can replace keccak-256sum -x -l with the commonly builtin sha256sum

@lau-bin

lau-bin commented Jan 24, 2022

Copy link
Copy Markdown

you can replace keccak-256sum -x -l with the commonly builtin sha256sum

@nicexe

nicexe commented Mar 9, 2022

Copy link
Copy Markdown

@lau-bin you cannot really substitute one for another. Because of the slight differences in how each digest algorithm works, this results in a different hash.

@hell0men

Copy link
Copy Markdown

Ubuntu:

cat pub | keccak-256sum -x -l | tr -d ' -' | tail -c 41 > address
keccak-256sum: command not found

@dhruvio

dhruvio commented Aug 9, 2023

Copy link
Copy Markdown

@miguelmota Thanks for putting this together. I implemented this for people to use in 1 line if they have NixOS or the Nix package manager installed:

nix run \
  github:realfolk/nix?dir=lib/packages/generate-ethereum-account \
  PATH_TO_STORE_PUBLIC_KEY \
  PATH_TO_STORE_PRIVATE_KEY

@bertho-zero

Copy link
Copy Markdown

Create address with checksum (EIP 55): 0xcf8e7afab9576f3bd85885065aecd479d35f4d70 -> 0xCf8e7aFAB9576F3Bd85885065aeCD479D35F4d70

address=$(cat address)
hash=$(echo -n "$address" | keccak-256sum | tr -d ' -')

checksum_address=""
for i in $(seq 0 39); do
    char="${address:$i:1}"
    hash_char="${hash:$i:1}"
    if [[ "$char" =~ [0-9] ]]; then
        checksum_address+="$char"
    elif (( 0x$hash_char >= 8 )); then
        checksum_address+="${char^^}"
    else
        checksum_address+="$char"
    fi
done

@raj4600

raj4600 commented Nov 4, 2024

Copy link
Copy Markdown

Lost my Trc20 private key how to recover it ??

@bertho-zero

Copy link
Copy Markdown

To install keccak-256sum:

wget -qO- https://codeberg.org/maandree/libkeccak/archive/1.3.1.2.tar.gz | tar xz
cd libkeccak
make
sudo make install

cd ..
wget -qO- wget https://codeberg.org/maandree/sha3sum/archive/1.2.2.tar.gz | tar xz
cd sha3sum
make
sudo make install

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