Module: Crypto

Defined in:
lib/crypto-lite.rb,
lib/crypto-lite/metal.rb,
lib/crypto-lite/config.rb,
lib/crypto-lite/sign_rsa.rb

Defined Under Namespace

Modules: Metal, RSA Classes: Configuration

Constant Summary collapse

HEX_RE =

more helpers check if it is a hex (string)

- allow optiona 0x or 0X  and allow abcdef and ABCDEF
/\A(?:0x)?[0-9a-f]+\z/i

Class Method Summary collapse

Class Method Details

.args_to_input(args, kwargs) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/crypto-lite.rb', line 87

def self.args_to_input( args, kwargs )
  if kwargs[:hex]
    hex = kwargs[:hex]
    raise ArgumentError, "expected hex string (0-9a-f) - got >#{hex}< - can't pack string; sorry"   unless hex =~ HEX_RE

    hex = strip0x( hex )  ##  check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
    [hex].pack( 'H*' )
  else   ## assume single input arg for now
    input = args[0]
    input = hex_to_bin_automagic( input )  ## add automagic hex (string) to bin (string) check - why? why not?
    input
  end
end

.base58(*args, **kwargs) ⇒ Object



25
26
27
28
# File 'lib/crypto-lite.rb', line 25

def self.base58( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.base58bin( input )
end

.base58check(*args, **kwargs) ⇒ Object



30
31
32
33
# File 'lib/crypto-lite.rb', line 30

def self.base58check( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.base58bin_check( input )
end

.configurationObject

lets you use

Crypto.configure do |config|
   config.debug  =  true
end


18
19
20
# File 'lib/crypto-lite/config.rb', line 18

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



22
23
24
# File 'lib/crypto-lite/config.rb', line 22

def self.configure
  yield( configuration )
end

.debug=(value) ⇒ Object



28
# File 'lib/crypto-lite/config.rb', line 28

def self.debug=(value) self.configuration.debug = value; end

.debug?Boolean

add convenience helper for format



27
# File 'lib/crypto-lite/config.rb', line 27

def self.debug?() configuration.debug?; end

.hash160(*args, **kwargs) ⇒ Object



69
70
71
72
# File 'lib/crypto-lite.rb', line 69

def self.hash160( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.hash160bin( input ).unpack( 'H*' )[0]
end

.hash256(*args, **kwargs) ⇒ Object



74
75
76
77
# File 'lib/crypto-lite.rb', line 74

def self.hash256( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.hash256bin( input ).unpack( 'H*' )[0]
end

.hex_to_bin_automagic(input) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/crypto-lite.rb', line 101

def self.hex_to_bin_automagic( input )
  ## todo/check/fix: add configure setting to turn off automagic - why? why not?
   if input.is_a?( String ) && input =~ HEX_RE
      if input[0,2] == '0x' || input[0,2] == '0X'
        ## starting with 0x or 0X always assume hex string for now - why? why not?
        input = input[2..-1]
        [input].pack( 'H*' )
      elsif input.size >= 10
        ## note: hex heuristic!!
        ##   for now assumes string MUST have more than 10 digits to qualify!!!
        [input].pack( 'H*' )
      else
        input ## pass through as is!!! (e.g.   a, abc, etc.)
      end
   else
        input  ## pass through as is
   end
end

.keccak256(*args, **kwargs) ⇒ Object

(secure) hash functions



39
40
41
42
# File 'lib/crypto-lite.rb', line 39

def self.keccak256( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.keccak256bin( input ).unpack( 'H*' )[0]
end

.rmd160(*args, **kwargs) ⇒ Object Also known as: ripemd160



45
46
47
48
# File 'lib/crypto-lite.rb', line 45

def self.rmd160( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.rmd160bin( input ).unpack( 'H*' )[0]
end

.sha256(*args, **kwargs) ⇒ Object



56
57
58
59
60
# File 'lib/crypto-lite.rb', line 56

def self.sha256( *args, **kwargs )
  input = args_to_input( args, kwargs )
  engine = kwargs[:engine]
  Metal.sha256bin( input, engine ).unpack( 'H*' )[0]
end

.sha3_256(*args, **kwargs) ⇒ Object



62
63
64
65
# File 'lib/crypto-lite.rb', line 62

def self.sha3_256( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.sha3_256bin( input ).unpack( 'H*' )[0]
end

.strip0x(str) ⇒ Object

todo/check: add alias e.g. strip_hex_prefix or such - why? why not?



121
122
123
# File 'lib/crypto-lite.rb', line 121

def self.strip0x( str )    ## todo/check: add alias e.g. strip_hex_prefix or such - why? why not?
  (str[0,2] == '0x' || str[0,2] == '0X') ?  str[2..-1] : str
end