Module: Crypto
- Defined in:
- lib/crypto-lite.rb
Defined Under Namespace
Modules: RSA
Class Method Summary collapse
-
.hash160hex(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.
- .hash256hex(input) ⇒ Object
- .keccak256(input) ⇒ Object
- .keccak256bin(input) ⇒ Object
-
.message(input) ⇒ Object
convert input to (binary) string.
- .pluralize(count, noun) ⇒ Object
- .rmd160(input) ⇒ Object
- .rmd160bin(input) ⇒ Object
- .sha256(input, engine = nil) ⇒ Object
-
.sha256bin(input, engine = nil) ⇒ Object
todo/check: add alias sha256b or such to - why? why not?.
- .sha256hex(input, engine = nil) ⇒ Object
Class Method Details
.hash160hex(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
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/crypto-lite.rb', line 116 def self.hash160hex( input ) ## convenience helper - lets you pass in hex string ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!! if input.start_with?( '0x') || input.start_with?( '0X' ) input = input[2..-1] end raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/ sha256bin = sha256bin( [input].pack( 'H*' ) ) rmd160 = rmd160bin( sha256bin ).unpack( "H*" )[0] rmd160 end |
.hash256hex(input) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/crypto-lite.rb', line 130 def self.hash256hex( input ) ## convenience helper - lets you pass in hex string ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!! if input.start_with?( '0x') || input.start_with?( '0X' ) input = input[2..-1] end raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/ sha256bin = sha256bin( [input].pack( 'H*' ) ) sha256 = sha256bin( sha256bin ).unpack( "H*" )[0] sha256 end |
.keccak256(input) ⇒ Object
58 59 60 |
# File 'lib/crypto-lite.rb', line 58 def self.keccak256( input ) keccak256bin( input ).unpack( 'H*' )[0] end |
.keccak256bin(input) ⇒ Object
53 54 55 56 |
# File 'lib/crypto-lite.rb', line 53 def self.keccak256bin( input ) = ( input ) ## "normalize" / convert to (binary) string Digest::SHA3.digest( , 256 ) end |
.message(input) ⇒ Object
convert input to (binary) string
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/crypto-lite.rb', line 24 def self.( input ) ## convert input to (binary) string input_type = if input.is_a?( String ) "#{input.class.name}/#{input.encoding}" else input.class.name end puts " input: #{input} (#{input_type})" = 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 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 |
.pluralize(count, noun) ⇒ Object
145 146 147 |
# File 'lib/crypto-lite.rb', line 145 def self.pluralize( count, noun ) count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s" end |
.rmd160(input) ⇒ Object
68 69 70 |
# File 'lib/crypto-lite.rb', line 68 def self.rmd160( input ) rmd160bin( input ).unpack( 'H*' )[0] end |
.rmd160bin(input) ⇒ Object
63 64 65 66 |
# File 'lib/crypto-lite.rb', line 63 def self.rmd160bin( input ) = ( input ) ## "normalize" / convert to (binary) string Digest::RMD160.digest( ) end |
.sha256(input, engine = nil) ⇒ Object
88 89 90 |
# File 'lib/crypto-lite.rb', line 88 def self.sha256( input, engine=nil ) sha256bin( input, engine ).unpack( 'H*' )[0] end |
.sha256bin(input, engine = nil) ⇒ Object
todo/check: add alias sha256b or such to - why? why not?
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/crypto-lite.rb', line 75 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}" digest = OpenSSL::Digest::SHA256.new digest.update( ) digest.digest else ## use "built-in" hash function from digest module Digest::SHA256.digest( ) end end |
.sha256hex(input, engine = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/crypto-lite.rb', line 93 def self.sha256hex( input, engine=nil ) ## convenience helper - lets you pass in hex string ## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!! if input.start_with?( '0x') || input.start_with?( '0X' ) input = input[2..-1] end raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/ sha256( [input].pack( 'H*' ), engine ) end |