Forked from colindean/generate_bitcoin_address.sh
Last active
January 24, 2024 02:29
-
-
Save thiagosouza/83cf75e7d21639fcc0fe8a797d78c596 to your computer and use it in GitHub Desktop.
Bitcoin address generator in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Requires bc, dc, openssl, xxd | |
# sudo apt-get install bc dc openssl xxd | |
# | |
# by grondilu from https://bitcointalk.org/index.php?topic=10970.msg156708#msg156708 | |
base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}) | |
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$" | |
if [ `uname -s` = 'Darwin' ]; then | |
TAC="tail -r " | |
else | |
TAC="tac" | |
fi | |
decodeBase58() { | |
local s=$1 | |
for i in {0..57} | |
do s="${s//${base58[i]}/ $i}" | |
done | |
dc <<< "16o0d${s// /+58*}+f" | |
} | |
encodeBase58() { | |
# 58 = 0x3A | |
echo -n "$1" | sed -e's/^\(\(00\)*\).*/\1/' -e's/00/1/g' | tr -d '\n' | |
dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" | | |
while read -r n; do echo -n "${base58[n]}"; done | |
} | |
checksum() { | |
xxd -p -r <<<"$1" | | |
openssl dgst -sha256 -binary | | |
openssl dgst -sha256 -binary | | |
xxd -p -c 80 | | |
head -c 8 | |
} | |
checkBitcoinAddress() { | |
if [[ "$1" =~ $bitcoinregex ]] | |
then | |
h=$(decodeBase58 "$1") | |
checksum "00${h::${#h}-8}" | | |
grep -qi "^${h: -8}$" | |
else return 2 | |
fi | |
} | |
hash160() { | |
openssl dgst -sha256 -binary | | |
openssl dgst -rmd160 -provider legacy -binary | #-provider legacy if rmd is broken | |
xxd -p -c 80 | |
} | |
hash160ToAddress() { | |
printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" | | |
sed "y/ /1/" | |
} | |
publicKeyToAddress() { | |
hash160ToAddress $( | |
openssl ec -pubin -pubout -outform DER | # read the public key and convert it to DER format | |
tail -c 65 | | |
hash160 | |
) | |
} | |
echo -n "Public key: " | |
openssl ecparam -name secp256k1 -genkey | tee priv.pem | openssl ec -pubout | tee pub.pem | publicKeyToAddress | tee address.txt | |
echo "" | |
echo -n "Private key: " | |
cat priv.pem | |
echo "" | |
cat pub.pem | |
echo "" | |
echo "Adddress" | |
cat address.txt | |
# openssl ecparam -genkey -name secp256k1 -out private_key.pem | |
# openssl ec -pubout -in private_key.pem -out public_key.pem | |
# openssl genrsa -out rsa.private 1024 | |
# openssl genrsa -out rsa.private 2048 | |
# openssl genrsa -out rsa.private 4096 | |
# openssl genrsa -out rsa.private 8192 | |
# openssl genrsa -out rsa.private 16384 | |
# openssl genrsa -out rsa.private 16385 | |
# openssl rsa -in rsa.private -out rsa.public -pubout -outform PEM | |
# Warning: It is not recommended to use more than 16384 bit for RSA keys. | |
# Your key size is 100000! Larger key size may behave not as expected. | |
# https://river.com/learn/terms/s/secp256k1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Requires bc, dc, openssl, xxd | |
# | |
# by grondilu from https://bitcointalk.org/index.php?topic=10970.msg156708#msg156708 | |
base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}) | |
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$" | |
if [ `uname -s` = 'Darwin' ]; then | |
TAC="tail -r " | |
else | |
TAC="tac" | |
fi | |
decodeBase58() { | |
local s=$1 | |
for i in {0..57} | |
do s="${s//${base58[i]}/ $i}" | |
done | |
dc <<< "16o0d${s// /+58*}+f" | |
} | |
encodeBase58() { | |
# 58 = 0x3A | |
echo -n "$1" | sed -e's/^\(\(00\)*\).*/\1/' -e's/00/1/g' | tr -d '\n' | |
dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" | | |
while read -r n; do echo -n "${base58[n]}"; done | |
} | |
checksum() { | |
xxd -p -r <<<"$1" | | |
openssl dgst -sha256 -binary | | |
openssl dgst -sha256 -binary | | |
xxd -p -c 80 | | |
head -c 8 | |
} | |
checkBitcoinAddress() { | |
if [[ "$1" =~ $bitcoinregex ]] | |
then | |
h=$(decodeBase58 "$1") | |
checksum "00${h::${#h}-8}" | | |
grep -qi "^${h: -8}$" | |
else return 2 | |
fi | |
} | |
hash160() { | |
openssl dgst -sha256 -binary | | |
openssl dgst -rmd160 -binary | | |
xxd -p -c 80 | |
} | |
hash160ToAddress() { | |
printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" | | |
sed "y/ /1/" | |
} | |
publicKeyToAddress() { | |
hash160ToAddress $( | |
openssl ec -pubin -pubout -outform DER | | |
tail -c 65 | | |
hash160 | |
) | |
} | |
echo -n "Public key: " | |
openssl ecparam -name secp256k1 -genkey | tee priv.pem | openssl ec -pubout | publicKeyToAddress | |
echo "" | |
echo -n "Private key: " | |
cat priv.pem | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment