Class: H256::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/h256.rb

Overview

Builder handles the creation of salted password hashes using SHA-256

Examples:

builder = H256::Builder.new("my-secret")
hash = builder.call("p@ssw0rd") # => "a1b2c3d4..."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Builder

Initialize a new Builder with a secret salt

Parameters:

  • secret (Object)

    the salt to use for hashing (will be converted to String)



20
21
22
23
24
# File 'lib/h256.rb', line 20

def initialize(secret)
  @secret = String(secret)

  freeze
end

Instance Attribute Details

#secretString (readonly)

Returns the secret salt used for hashing.

Returns:

  • (String)

    the secret salt used for hashing



16
17
18
# File 'lib/h256.rb', line 16

def secret
  @secret
end

Instance Method Details

#call(value) ⇒ String

Generate a hex-encoded salted hash of the provided value

Parameters:

  • value (String)

    the value to hash

Returns:

  • (String)

    hex-encoded SHA-256 hash (64 characters)



29
30
31
# File 'lib/h256.rb', line 29

def call(value)
  ::Digest::SHA256.hexdigest("#{value}++#{secret}")
end