Module: Crypto::MetalHelper

Included in:
Metal
Defined in:
lib/crypto-lite/metal.rb

Instance Method Summary collapse

Instance Method Details

#base58(bin) ⇒ Object

note: all (digest) hash functions

take in a binary string and return a binary string!!!!


9
10
11
# File 'lib/crypto-lite/metal.rb', line 9

def base58( bin )
  Base58::Bitcoin.encode_bin( bin )
end

#base58check(bin) ⇒ Object



13
14
15
16
# File 'lib/crypto-lite/metal.rb', line 13

def base58check( bin )
  hash256 = hash256( bin )
  base58( bin + hash256[0,4] )
end

#hash160(bin) ⇒ 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



67
68
69
# File 'lib/crypto-lite/metal.rb', line 67

def hash160( bin )
  rmd160(sha256( bin ))
end

#hash256(bin) ⇒ Object



71
72
73
# File 'lib/crypto-lite/metal.rb', line 71

def hash256( bin )
  sha256(sha256( bin ))
end

#keccak256(bin) ⇒ Object

(secure) hash functions



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

def keccak256( bin )
  Digest::KeccakLite.digest( bin, 256 )
end

#rmd160(bin) ⇒ Object Also known as: ripemd160



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

def rmd160( bin )
  Digest::RMD160.digest( bin )
end

#sha256(bin, engine = nil) ⇒ Object

todo/check: add alias sha256b or such to - why? why not?



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/crypto-lite/metal.rb', line 33

def sha256( bin, engine=nil )   ## todo/check: add alias sha256b or such to - why? why not?
  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( bin )
    digest.digest
  else  ## use "built-in" hash function from digest module
    Digest::SHA256.digest( bin )
  end
end

#sha3_256(bin, engine = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/crypto-lite/metal.rb', line 46

def sha3_256( bin, engine=nil )
  if engine && ['openssl'].include?( engine.to_s.downcase )
    ## puts "  engine: #{engine}"    if debug?
    digest = OpenSSL::Digest.new( 'SHA3-256' )
    digest.update( bin )
    digest.digest
  else  ## use "built-in" hash function from digest module
    Digest::SHA3Lite.digest( bin, 256 )
  end
end