Module: FunWith::Files::DigestMethods
- Defined in:
- lib/fun_with/files/digest_methods.rb
Instance Method Summary collapse
- #digest(digest_class = Digest::MD5) ⇒ Object
- #md5 ⇒ Object
- #sha1 ⇒ Object
- #sha2 ⇒ Object
- #sha224 ⇒ Object
- #sha256 ⇒ Object
- #sha384 ⇒ Object
- #sha512 ⇒ Object
-
#valid_digest?(opts) ⇒ Boolean
Takes any of the above-named digest functions, determines whether the file matches a given digest string.
Instance Method Details
#digest(digest_class = Digest::MD5) ⇒ Object
34 35 36 |
# File 'lib/fun_with/files/digest_methods.rb', line 34 def digest( digest_class = Digest::MD5 ) self.file? ? digest_class.hexdigest( self.read ) : "" end |
#md5 ⇒ Object
6 7 8 |
# File 'lib/fun_with/files/digest_methods.rb', line 6 def md5 digest( Digest::MD5 ) end |
#sha1 ⇒ Object
10 11 12 |
# File 'lib/fun_with/files/digest_methods.rb', line 10 def sha1 digest( Digest::SHA1 ) end |
#sha2 ⇒ Object
14 15 16 |
# File 'lib/fun_with/files/digest_methods.rb', line 14 def sha2 digest( Digest::SHA2 ) end |
#sha224 ⇒ Object
18 19 20 |
# File 'lib/fun_with/files/digest_methods.rb', line 18 def sha224 digest( Digest::SHA224 ) end |
#sha256 ⇒ Object
22 23 24 |
# File 'lib/fun_with/files/digest_methods.rb', line 22 def sha256 digest( Digest::SHA256 ) end |
#sha384 ⇒ Object
26 27 28 |
# File 'lib/fun_with/files/digest_methods.rb', line 26 def sha384 digest( Digest::SHA384 ) end |
#sha512 ⇒ Object
30 31 32 |
# File 'lib/fun_with/files/digest_methods.rb', line 30 def sha512 digest( Digest::SHA512 ) end |
#valid_digest?(opts) ⇒ Boolean
Takes any of the above-named digest functions, determines whether the file matches a given digest string.
Multiple digests can be given simultaneously. All must pass.
TODO: how to get around the :md6 problem? That is, where the user is sending the wrong key, and hence not getting false back
45 46 47 48 49 50 51 52 53 |
# File 'lib/fun_with/files/digest_methods.rb', line 45 def valid_digest?( opts ) for method, digest in opts if DIGEST_METHODS.include?( method ) return false unless self.send( method ) == digest end end return true end |