Module: BTC::HashFunctions

Included in:
BTC
Defined in:
lib/btcruby/hash_functions.rb,
lib/btcruby/hash_functions.rb

Overview

This allows doing ‘BTC.sha256(…)`

Constant Summary collapse

OPENSSL_DIGEST_NAME_SHA256 =
'sha256'.freeze
OPENSSL_DIGEST_NAME_SHA512 =
'sha512'.freeze

Instance Method Summary collapse

Instance Method Details

#hash160(data) ⇒ Object



39
40
41
# File 'lib/btcruby/hash_functions.rb', line 39

def hash160(data)
  ripemd160(sha256(data))
end

#hash256(data) ⇒ Object



35
36
37
# File 'lib/btcruby/hash_functions.rb', line 35

def hash256(data)
  sha256(sha256(data))
end

#hmac_sha256(data: nil, key: nil) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
# File 'lib/btcruby/hash_functions.rb', line 46

def hmac_sha256(data: nil, key: nil)
  raise ArgumentError, "Data is missing" if !data || !key
  ::OpenSSL::HMAC.digest(OPENSSL_DIGEST_NAME_SHA256, key, data)
end

#hmac_sha512(data: nil, key: nil) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/btcruby/hash_functions.rb', line 51

def hmac_sha512(data: nil, key: nil)
  raise ArgumentError, "Data is missing" if !data || !key
  ::OpenSSL::HMAC.digest(OPENSSL_DIGEST_NAME_SHA512, key, data)
end

#ripemd160(data) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/btcruby/hash_functions.rb', line 30

def ripemd160(data)
  raise ArgumentError, "Data is missing" if !data
  Digest::RMD160.digest(data)
end

#sha1(data) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/btcruby/hash_functions.rb', line 15

def sha1(data)
  raise ArgumentError, "Data is missing" if !data
  Digest::SHA1.digest(data)
end

#sha256(data) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
# File 'lib/btcruby/hash_functions.rb', line 20

def sha256(data)
  raise ArgumentError, "Data is missing" if !data
  Digest::SHA256.digest(data)
end

#sha512(data) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
# File 'lib/btcruby/hash_functions.rb', line 25

def sha512(data)
  raise ArgumentError, "Data is missing" if !data
  Digest::SHA512.digest(data)
end