Module: Crypto::Metal
- Defined in:
- lib/crypto-lite/metal.rb
Class Method Summary collapse
- .base58bin(input) ⇒ Object
- .base58bin_check(input) ⇒ Object
- .debug? ⇒ Boolean
-
.hash160bin(input) ⇒ Object
helper def hash160( pubkey ) binary = [pubkey].pack( “H*” ) # Convert to binary first before hashing sha256 = Digest::SHA256.digest( binary ) ripemd160 = Digest::RMD160.digest( sha256 ) ripemd160.unpack( “H*” )[0] # Convert back to hex end.
- .hash256bin(input) ⇒ Object
-
.keccak256bin(input) ⇒ Object
(secure) hash functions.
-
.message(input) ⇒ Object
helpers.
- .pluralize(count, noun) ⇒ Object
- .rmd160bin(input) ⇒ Object (also: ripemd160bin)
-
.sha256bin(input, engine = nil) ⇒ Object
todo/check: add alias sha256b or such to - why? why not?.
- .sha3_256bin(input, engine = nil) ⇒ Object
Class Method Details
.base58bin(input) ⇒ Object
14 15 16 17 |
# File 'lib/crypto-lite/metal.rb', line 14 def self.base58bin( input ) ## todo/check: input must be a (binary) string - why? why not? Base58::Bitcoin.encode_bin( input ) end |
.base58bin_check(input) ⇒ Object
19 20 21 22 23 |
# File 'lib/crypto-lite/metal.rb', line 19 def self.base58bin_check( input ) ## todo/check: input must be a (binary) string - why? why not? hash256 = hash256bin( input ) base58bin( input + hash256[0,4] ) end |
.hash160bin(input) ⇒ Object
83 84 85 86 87 |
# File 'lib/crypto-lite/metal.rb', line 83 def self.hash160bin( input ) = ( input ) ## "normalize" / convert to (binary) string rmd160bin(sha256bin( )) end |
.hash256bin(input) ⇒ Object
90 91 92 93 94 |
# File 'lib/crypto-lite/metal.rb', line 90 def self.hash256bin( input ) = ( input ) ## "normalize" / convert to (binary) string sha256bin(sha256bin( )) end |
.keccak256bin(input) ⇒ Object
(secure) hash functions
29 30 31 32 |
# File 'lib/crypto-lite/metal.rb', line 29 def self.keccak256bin( input ) = ( input ) ## "normalize" / convert to (binary) string Digest::KeccakLite.digest( , 256 ) end |
.message(input) ⇒ Object
helpers
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/crypto-lite/metal.rb', line 99 def self.( input ) ## convert input to (binary) string if debug? input_type = if input.is_a?( String ) "#{input.class.name}/#{input.encoding}" else input.class.name end puts " input: #{input} (#{input_type})" end = if input.is_a?( Integer ) ## assume byte if single (unsigned) integer raise ArgumentError, "expected unsigned byte (0-255) - got #{input} (0x#{input.to_s(16)}) - can't pack negative number; sorry" if input < 0 ## note: pack - H (String) => hex string (high nibble first) ## todo/check: is there a better way to convert integer number to (binary) string!!! [input.to_s(16)].pack('H*') else ## assume (binary) string input end if debug? bytes = .bytes bin = bytes.map {|byte| byte.to_s(2).rjust(8, "0")}.join( ' ' ) hex = bytes.map {|byte| byte.to_s(16).rjust(2, "0")}.join( ' ' ) puts " #{pluralize( bytes.size, 'byte')}: #{bytes.inspect}" puts " binary: #{bin}" puts " hex: #{hex}" end end |
.pluralize(count, noun) ⇒ Object
130 131 132 |
# File 'lib/crypto-lite/metal.rb', line 130 def self.pluralize( count, noun ) count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s" end |
.rmd160bin(input) ⇒ Object Also known as: ripemd160bin
34 35 36 37 |
# File 'lib/crypto-lite/metal.rb', line 34 def self.rmd160bin( input ) = ( input ) ## "normalize" / convert to (binary) string Digest::RMD160.digest( ) end |
.sha256bin(input, engine = nil) ⇒ Object
todo/check: add alias sha256b or such to - why? why not?
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/crypto-lite/metal.rb', line 45 def self.sha256bin( input, engine=nil ) ## todo/check: add alias sha256b or such to - why? why not? = ( input ) ## "normalize" / convert to (binary) string if engine && ['openssl'].include?( engine.to_s.downcase ) puts " engine: #{engine}" if debug? digest = OpenSSL::Digest::SHA256.new ## or use OpenSSL::Digest.new( 'SHA256' ) digest.update( ) digest.digest else ## use "built-in" hash function from digest module Digest::SHA256.digest( ) end end |
.sha3_256bin(input, engine = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/crypto-lite/metal.rb', line 60 def self.sha3_256bin( input, engine=nil ) = ( input ) ## "normalize" / convert to (binary) string if engine && ['openssl'].include?( engine.to_s.downcase ) puts " engine: #{engine}" if debug? digest = OpenSSL::Digest.new( 'SHA3-256' ) digest.update( ) digest.digest else ## use "built-in" hash function from digest module Digest::SHA3Lite.digest( , 256 ) end end |