Class: Gibberish::Digest

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

Overview

Allows for the simple digest of data, supports MD5, SHA1, and SHA256

Examples

Gibberish::MD5("data") #=> 8d777f385d3dfec8815d20f7496026dc
Gibberish::SHA1("data") #=> a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd
Gibberish::SHA256("data") #=> 3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7

OpenSSL CLI Interop

echo -n 'data' | openssl dgst -sha1
echo -n 'data' | openssl dgst -sha256
echo -n 'data' | openssl dgst -md5

is the same as

Gibberish::SHA1("data")
Gibberish::SHA256("data")
Gibberish::MD5("data")

Class Method Summary (collapse)

Class Method Details

+ (Object) md5(data, opts = {})

Returns the MD5 digest for the data

Shorcut alias: Gibberish::MD5(data)

Parameters:

  • (String) key
  • (#to_s) data
  • (Hash) options
  • (Hash) opts (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • (Boolean) :binary — default: false

    encode the data in binary, not Base64



66
67
68
69
70
71
72
73
# File 'lib/gibberish/digest.rb', line 66

def self.md5(data, opts={})
  data = data.to_s
  if opts[:binary]
    OpenSSL::Digest::MD5.digest(data)
  else
    OpenSSL::Digest::MD5.hexdigest(data)
  end
end

+ (Object) sha1(data, opts = {})

Returns the SHA1 digest for the data

Shorcut alias: Gibberish::SHA1(data)

Parameters:

  • (String) key
  • (#to_s) data
  • (Hash) options
  • (Hash) opts (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • (Boolean) :binary — default: false

    encode the data in binary, not Base64



32
33
34
35
36
37
38
39
# File 'lib/gibberish/digest.rb', line 32

def self.sha1(data, opts={})
  data = data.to_s
  if opts[:binary]
    OpenSSL::Digest::SHA1.digest(data)
  else
    OpenSSL::Digest::SHA1.hexdigest(data)
  end
end

+ (Object) sha256(data, opts = {})

Returns the SHA256 digest for the data

Shorcut alias: Gibberish::SHA256(data)

Parameters:

  • (String) key
  • (#to_s) data
  • (Hash) options
  • (Hash) opts (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • (Boolean) :binary — default: false

    encode the data in binary, not Base64



49
50
51
52
53
54
55
56
# File 'lib/gibberish/digest.rb', line 49

def self.sha256(data, opts={})
  data = data.to_s
  if opts[:binary]
    OpenSSL::Digest::SHA256.digest(data)
  else
    OpenSSL::Digest::SHA256.hexdigest(data)
  end
end