Created
August 11, 2011 13:08
-
-
Save davout/1139599 to your computer and use it in GitHub Desktop.
Extract bitcoin address and private key from EC PEM certificate to nice and sweet QR codes
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
#!/usr/bin/ruby | |
require 'erb' | |
require 'digest' | |
require 'date' | |
B58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | |
B58Digits = B58Chars.split('') | |
B58Base = B58Chars.length | |
def hex2bin(str) | |
(0..((str.length/2)-1)).inject([]) do |r, i| | |
r << [str[i*2, 2]].pack("H2") | |
end.join | |
end | |
def bin2hex(str) | |
str.unpack("H*")[0] | |
end | |
class Numeric | |
def to_b58 | |
if self < B58Base | |
B58Digits[self] | |
else | |
(self / B58Base).to_b58 + (self % B58Base).to_b58 | |
end | |
end | |
end | |
class PrivateKey | |
attr_accessor :key | |
def initialize(hex_key) | |
self.key = hex_key | |
end | |
def checksum | |
bin2hex(Digest::SHA2.digest(Digest::SHA2.digest(hex2bin("80" + key)))[0, 4]) | |
end | |
def to_b58 | |
("80" + key + checksum).to_i(16).to_b58 | |
end | |
end | |
pem = ARGV[0] | |
pkey = PrivateKey.new(bin2hex(`cat #{pem} | openssl ec -outform der`[7, 32])) | |
@private_key = pkey.to_b58 | |
addy = `cat #{pem} | openssl ec -pubout -outform der | tail -c 65` | |
addy = "00" + Digest::RMD160.hexdigest(Digest::SHA2.digest(addy)) | |
@address = "1" + (addy + bin2hex(Digest::SHA2.digest(Digest::SHA2.digest(hex2bin(addy)))[0,4])).to_i(16).to_b58 | |
@now = DateTime.now.to_s | |
`qrencode -o #{@address}.png -s 4 -l H #{@address}` | |
`qrencode -o #{@private_key}.png -s 4 -l H #{@private_key}` | |
`qrencode -o bbe_#{@address}.png -s 4 -l H https://blockexplorer.com/address/#{@address}` | |
text = File.read("./template.erb") | |
template = ERB.new(text, 0, "%<>") | |
result = template.result(binding) | |
File.open("#{@address}.html", 'w') { |f| f.write(result) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment