Module: SecureString::DigestMethods::InstanceMethods

Defined in:
lib/secure_string/digest_methods.rb

Overview

Adds instance methods for OpenSSL::Digest support via inclusion of SecureString::DigestMethods to a class.

Instance Method Summary collapse

Instance Method Details

#to_digest(digest_obj) ⇒ Object

Returns the digest of the byte string as a SecureString, using the passed OpenSSL object.



17
18
19
# File 'lib/secure_string/digest_methods.rb', line 17

def to_digest(digest_obj)
  return self.class.new( digest_obj.digest(self) )
end

#to_md5Object

Returns the MD5 of the byte string as a SecureString.



22
23
24
# File 'lib/secure_string/digest_methods.rb', line 22

def to_md5
  return to_digest( OpenSSL::Digest::MD5.new )
end

#to_sha1Object

Returns the SHA1 of the byte string as SecureString



27
28
29
# File 'lib/secure_string/digest_methods.rb', line 27

def to_sha1
  return to_digest( OpenSSL::Digest::SHA1.new )
end

#to_sha2(length = 256) ⇒ Object

Returns the SHA2 of the byte string as a SecureString.

By default, this uses the 256 bit SHA2, but the optional arugment allows specification of which bit length to use.



35
36
37
38
39
40
41
42
# File 'lib/secure_string/digest_methods.rb', line 35

def to_sha2(length=256)
  if [224,256,384,512].include?(length)
    digest_klass = OpenSSL::Digest.const_get("SHA#{length}", false)
    return to_digest( digest_klass )
  else
    raise ArgumentError, "Invalid SHA2 length: #{length}"
  end
end

#to_sha256Object

Returns the SHA2 256 of the data string. See to_sha2.



45
46
47
# File 'lib/secure_string/digest_methods.rb', line 45

def to_sha256
  return to_sha2(256)
end

#to_sha512Object

Returns the SHA2 512 of the data string. See to_sha2.



50
51
52
# File 'lib/secure_string/digest_methods.rb', line 50

def to_sha512
  return to_sha2(512)
end