Class: Crate::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/crate/digest.rb

Overview

A wrapper around a given digest value and the algorithm that creates it. Use it to verify a bytestream.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex, klass) ⇒ Digest

Returns a new instance of Digest.



24
25
26
27
# File 'lib/crate/digest.rb', line 24

def initialize( hex, klass )
  @hex = hex
  @engine = klass.new
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



11
12
13
# File 'lib/crate/digest.rb', line 11

def engine
  @engine
end

#hexObject (readonly)

Returns the value of attribute hex.



12
13
14
# File 'lib/crate/digest.rb', line 12

def hex
  @hex
end

Class Method Details

.md5(hex) ⇒ Object



19
20
21
# File 'lib/crate/digest.rb', line 19

def md5( hex )
  return Crate::Digest.new( hex, ::Digest::MD5  )
end

.sha1(hex) ⇒ Object



15
16
17
# File 'lib/crate/digest.rb', line 15

def sha1( hex )
  return Crate::Digest.new( hex, ::Digest::SHA1 )
end

Instance Method Details

#valid?(filename) ⇒ Boolean

verify that the given filename has a digest value

Returns:

  • (Boolean)


32
33
34
# File 'lib/crate/digest.rb', line 32

def valid?( filename )
  engine.hexdigest( IO.read( filename ) ) == hex
end