Sha256: 2f9211e91972ed8aa89b016ca98e8fe81b803bf7088805f3d1110bff227fc3fe

Contents?: true

Size: 1.26 KB

Versions: 26

Compression:

Stored size: 1.26 KB

Contents

require 'digest/sha1'
require 'digest/sha2'
require 'digest/rmd160'
require 'openssl'

module BTC
  
  # This allows doing `BTC.sha256(...)`
  module HashFunctions; end
  include HashFunctions
  extend self

  module HashFunctions

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

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

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

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

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

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

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

    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

    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

  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
btcruby-1.8 lib/btcruby/hash_functions.rb
btcruby-1.7 lib/btcruby/hash_functions.rb
btcruby-1.6 lib/btcruby/hash_functions.rb
btcruby-1.5.1 lib/btcruby/hash_functions.rb
btcruby-1.5 lib/btcruby/hash_functions.rb
btcruby-1.4 lib/btcruby/hash_functions.rb
btcruby-1.3 lib/btcruby/hash_functions.rb
btcruby-1.2.2 lib/btcruby/hash_functions.rb
btcruby-1.2.1 lib/btcruby/hash_functions.rb
btcruby-1.2 lib/btcruby/hash_functions.rb
btcruby-1.1.6 lib/btcruby/hash_functions.rb
btcruby-1.1.5 lib/btcruby/hash_functions.rb
btcruby-1.1.4 lib/btcruby/hash_functions.rb
btcruby-1.1.3 lib/btcruby/hash_functions.rb
btcruby-1.1.2 lib/btcruby/hash_functions.rb
btcruby-1.1.1 lib/btcruby/hash_functions.rb
btcruby-1.1 lib/btcruby/hash_functions.rb
btcruby-1.0.9 lib/btcruby/hash_functions.rb
btcruby-1.0.8 lib/btcruby/hash_functions.rb
btcruby-1.0.7 lib/btcruby/hash_functions.rb