SHARE
TWEET

Cryptoshocker - generatekey

benkow_ Jun 16th, 2016 (edited) 422 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # this was derived from here: https://bitcointalk.org/index.php?topic=23081.20
  4. # I simply slimmed it down so it could be used by a script -RobKohr
  5.  
  6. base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
  7. bitcoinregex="^[$(printf "%s" "${base58}")]{34}$"
  8.  
  9. decodeBase58() {
  10.     local s=$1
  11.     for i in {0..57}
  12.     do s="${s//${base58}/ $i}"
  13.     done
  14.     dc <<< "16o0d${s// /+58*}+f"
  15. }
  16.  
  17. encodeBase58() {
  18.     # 58 = 0x3A
  19.     bc <<<"ibase=16; n=${1^^}; while(n>0) { n%3A ; n/=3A }" |
  20.     tac |
  21.     while read n
  22.     do echo -n ${base58[n]}
  23.     done
  24. }
  25.  
  26. checksum() {
  27.     xxd -p -r <<<"$1" |
  28.     openssl dgst -sha256 -binary |
  29.     openssl dgst -sha256 -binary |
  30.     xxd -p -c 80 |
  31.     head -c 8
  32. }
  33.  
  34. checkBitcoinAddress() {
  35.     if [[ "$1" =~ $bitcoinregex ]]
  36.     then
  37.         h=$(decodeBase58 "$1")
  38.         checksum "00${h::${#h}-8}" |
  39.         grep -qi "^${h: -8}$"
  40.     else return 2
  41.     fi
  42. }
  43.  
  44. hash160() {
  45.     openssl dgst -sha256 -binary |
  46.     openssl dgst -rmd160 -binary |
  47.     xxd -p -c 80
  48. }
  49.  
  50. hash160ToAddress() {
  51.     printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" |
  52.    sed "y/ /1/"
  53. }
  54.  
  55. hash256ToAddress() {   
  56.     #printf "80$1$(checksum "80$1")"
  57.    printf "%34s\n" "$(encodeBase58 "80$1$(checksum "80$1")")" |
  58.    sed "y/ /1/"
  59. }
  60.  
  61. publicKeyToAddress() {
  62.    hash160ToAddress $(
  63.    openssl ec -pubin -pubout -outform DER |
  64.    tail -c 65 |
  65.    hash160
  66.    )
  67. }
  68.  
  69. privateKeyToWIF() {
  70.    hash256ToAddress $(openssl ec -text -noout -in data.pem | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g')
  71. }
  72.  
  73. openssl  ecparam -genkey -name secp256k1 | tee data.pem &>/dev/null
  74. openssl ec -text -noout -in data.pem | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g'
  75. echo "PrivateKey"
  76. privateKeyToWIF
  77. echo "PublicKey"
  78. openssl ec -pubout < data.pem | publicKeyToAddress
  79. rm data.pem
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top