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



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/crypto-lite.rb', line 90

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



28
29
30
31
# File 'lib/crypto-lite.rb', line 28

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

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



33
34
35
36
# File 'lib/crypto-lite.rb', line 33

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

Returns:

  • (Boolean)


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

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

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



72
73
74
75
# File 'lib/crypto-lite.rb', line 72

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

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



77
78
79
80
# File 'lib/crypto-lite.rb', line 77

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

.hex_to_bin_automagic(input) ⇒ Object



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

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



42
43
44
45
# File 'lib/crypto-lite.rb', line 42

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



48
49
50
51
# File 'lib/crypto-lite.rb', line 48

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

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



59
60
61
62
63
# File 'lib/crypto-lite.rb', line 59

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



65
66
67
68
# File 'lib/crypto-lite.rb', line 65

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?



124
125
126
# File 'lib/crypto-lite.rb', line 124

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