module Crypto

Constants

HEX_RE

more helpers

check if it is a hex (string)
 - allow optiona 0x or 0X  and allow abcdef and ABCDEF

Public Class Methods

args_to_input( args, kwargs ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/crypto-lite.rb, line 33
def self.base58check( *args, **kwargs )
  input = args_to_input( args, kwargs )
  Metal.base58bin_check( input )
end
configuration() click to toggle source

lets you use

Crypto.configure do |config|
   config.debug  =  true
end
# File lib/crypto-lite/config.rb, line 18
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/crypto-lite/config.rb, line 22
def self.configure
  yield( configuration )
end
debug=(value) click to toggle source
# File lib/crypto-lite/config.rb, line 28
def self.debug=(value) self.configuration.debug = value; end
debug?() click to toggle source

add convenience helper for format

# File lib/crypto-lite/config.rb, line 27
def self.debug?() configuration.debug?; end
hash160( *args, **kwargs ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source

(secure) hash functions

# 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
ripemd160( *args, **kwargs )
Alias for: rmd160
rmd160( *args, **kwargs ) click to toggle source
# 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
Also aliased as: ripemd160
sha256( *args, **kwargs ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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