Sha256: f00b8a3dd7acefb300ad0c3304ccfc3f955f4baefd96e33640251e9395315a07

Contents?: true

Size: 823 Bytes

Versions: 3

Compression:

Stored size: 823 Bytes

Contents

require "veronica/version"
require 'digest'

module Veronica
  VALID_SHA2_BIT_LENGTHS = [256, 384, 512]
  VALID_SHA2_ENCODING_FORMATS = ["hexdigest", "base64digest", "digest"]

  # We use hexdigest by default for familiarities sake
  def self.generate_sha2(digest_bits: , string: , encoding_format: "hexdigest")
    unless VALID_SHA2_BIT_LENGTHS.include?(digest_bits)
      raise ArgumentError, "You can only use 256, 384, or 512 for bit length."
    end

    unless VALID_SHA2_ENCODING_FORMATS.include?(encoding_format)
      raise ArgumentError, "You can only use hexdigest, base64digest or just digest for an encoding format."
    end

    # This lets us dynamically pass in digest_bits as well as encoding formats to the Digest class.
    Digest::SHA2.new(digest_bits).method(encoding_format.to_sym).(string)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
veronica-0.2.1 lib/veronica.rb
veronica-0.2.0 lib/veronica.rb
veronica-0.1.0 lib/veronica.rb