Module: FunWith::Files::DigestMethods
- Defined in:
- lib/fun_with/files/digest_methods.rb
Instance Method Summary collapse
- #digest(digest_class = Digest::MD5, bytes = :all, offset = 0) ⇒ Object
- #md5(bytes = :all, offset = 0) ⇒ Object
- #sha1(bytes = :all, offset = 0) ⇒ Object
- #sha2(bytes = :all, offset = 0) ⇒ Object
- #sha224(bytes = :all, offset = 0) ⇒ Object
- #sha256(bytes = :all, offset = 0) ⇒ Object
- #sha384(bytes = :all, offset = 0) ⇒ Object
- #sha512(bytes = :all, offset = 0) ⇒ 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, bytes = :all, offset = 0) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fun_with/files/digest_methods.rb', line 34 def digest( digest_class = Digest::MD5, bytes = :all, offset = 0 ) if self.file? && self.readable? if bytes == :all digest_class.hexdigest( self.read ) elsif bytes.is_a?( Integer ) digest_class.hexdigest( self.read( bytes, offset ) ) else raise ArgumentError.new( "FunWith::Files::DigestMethods.digest() error: bytes argument must be an integer or :all") end else raise IOError.new( "Not a file: #{self.path}" ) unless self.file? raise IOError.new( "Not readable: #{self.path}" ) unless self.readable? end end |
#md5(bytes = :all, offset = 0) ⇒ Object
6 7 8 |
# File 'lib/fun_with/files/digest_methods.rb', line 6 def md5( bytes = :all, offset = 0 ) digest( Digest::MD5, bytes, offset ) end |
#sha1(bytes = :all, offset = 0) ⇒ Object
10 11 12 |
# File 'lib/fun_with/files/digest_methods.rb', line 10 def sha1( bytes = :all, offset = 0 ) digest( Digest::SHA1, bytes, offset ) end |
#sha2(bytes = :all, offset = 0) ⇒ Object
14 15 16 |
# File 'lib/fun_with/files/digest_methods.rb', line 14 def sha2( bytes = :all, offset = 0 ) digest( Digest::SHA2, bytes, offset ) end |
#sha224(bytes = :all, offset = 0) ⇒ Object
18 19 20 |
# File 'lib/fun_with/files/digest_methods.rb', line 18 def sha224( bytes = :all, offset = 0 ) digest( Digest::SHA224, bytes, offset ) end |
#sha256(bytes = :all, offset = 0) ⇒ Object
22 23 24 |
# File 'lib/fun_with/files/digest_methods.rb', line 22 def sha256( bytes = :all, offset = 0 ) digest( Digest::SHA256, bytes, offset ) end |
#sha384(bytes = :all, offset = 0) ⇒ Object
26 27 28 |
# File 'lib/fun_with/files/digest_methods.rb', line 26 def sha384( bytes = :all, offset = 0 ) digest( Digest::SHA384, bytes, offset ) end |
#sha512(bytes = :all, offset = 0) ⇒ Object
30 31 32 |
# File 'lib/fun_with/files/digest_methods.rb', line 30 def sha512( bytes = :all, offset = 0 ) digest( Digest::SHA512, bytes, offset ) 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
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fun_with/files/digest_methods.rb', line 56 def valid_digest?( opts ) digest_opts = Utils::Opts.( opts, DIGEST_METHODS ) for method, digest in opts if DIGEST_METHODS.include?( method ) return false unless self.send( method ) == digest end end return true end |