Module: FunWith::Files::DigestMethods

Defined in:
lib/fun_with/files/digest_methods.rb

Instance Method Summary collapse

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

#md5Object



6
7
8
# File 'lib/fun_with/files/digest_methods.rb', line 6

def md5
  digest( Digest::MD5 )
end

#sha1Object



10
11
12
# File 'lib/fun_with/files/digest_methods.rb', line 10

def sha1
  digest( Digest::SHA1 )
end

#sha2Object



14
15
16
# File 'lib/fun_with/files/digest_methods.rb', line 14

def sha2
  digest( Digest::SHA2 )
end

#sha224Object



18
19
20
# File 'lib/fun_with/files/digest_methods.rb', line 18

def sha224
  digest( Digest::SHA224 )
end

#sha256Object



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

def sha256
  digest( Digest::SHA256 )
end

#sha384Object



26
27
28
# File 'lib/fun_with/files/digest_methods.rb', line 26

def sha384
  digest( Digest::SHA384 )
end

#sha512Object



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

Returns:

  • (Boolean)


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