Module: SecurizeString::DigestMethods::InstanceMethods

Defined in:
lib/securize_string/digest_methods.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#to_digest(digest) ⇒ Object

Returns the digest of the byte string as a SecureString using the passed digest from the list of digests in supported_digests.



30
31
32
33
# File 'lib/securize_string/digest_methods.rb', line 30

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

#to_md5Object

Returns the MD5 of the byte string as a SecureString.



36
37
38
# File 'lib/securize_string/digest_methods.rb', line 36

def to_md5
  return to_digest('MD5')
end

#to_sha1Object

Returns the SHA1 of the byte string as SecureString



41
42
43
# File 'lib/securize_string/digest_methods.rb', line 41

def to_sha1
  return to_digest('SHA-1')
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.



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

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

#to_sha256Object

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



59
60
61
# File 'lib/securize_string/digest_methods.rb', line 59

def to_sha256
  return to_sha2(256)
end

#to_sha512Object

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



64
65
66
# File 'lib/securize_string/digest_methods.rb', line 64

def to_sha512
  return to_sha2(512)
end