Sha256: cd6a4babe5650e47d937879c3b4a0b0dc968fec9aabe7a7fe652a9f9d11e6de7

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

require 'digest'

# Namespace and main entry point for the H library.
module H
  # Class that generates cryptographic hash
  class Generator
    # Initialize a H instance.
    #
    # @param static_key [String] a random data
    #
    # @return [H::Generator] instance
    #
    def initialize(static_key)
      @static_key = static_key
    end

    # Return a hash of +l+ length.
    #
    # @param m [String] a message
    # @param l [Fixnum] the length of the returned string
    #
    # @return [String] salted cryptographic hash.
    #
    # @api public
    def input(m, l)
      m += @static_key
      d = cryptographic_hash(m)
      truncate d, l
    end

    protected

    # Build a hash from +m+ message.
    #
    # @param m [String] a message
    #
    # @return [String] cryptographic hash.
    #
    # @api private
    def cryptographic_hash(m)
      ::Digest::SHA256.base64digest(m).tr('+/', '-_')
    end

    # Truncate the given string from +lng+ param.
    #
    # @param str [String] a string to truncate
    # @param lng [Fixnum] the length of the truncated string
    #
    # @return [String] truncated string.
    #
    # @api private
    def truncate(str, lng)
      str[0, lng]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
h-2.2.2 lib/h.rb
h-2.2.1 lib/h.rb
h-2.2.0 lib/h.rb