Class: Libis::Tools::Checksum

Inherits:
Object
  • Object
show all
Defined in:
lib/libis/tools/checksum.rb

Overview

Common interface for checksum calculations.

Supported checksum algortihms are MD5, RMD160 (not on JRuby), SHA-1, SHA-2 (256, 384 and 512-bit versions). All methods are available on the class and on the instance. The instance has to be initialized with a checksum algorithm and therefore the instance methods do not have to specify the checksum type.

Constant Summary collapse

CHECKSUM_TYPES =
[:MD5, :RMD160, :SHA1, :SHA256, :SHA384, :SHA512]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Checksum

Create instance for a given checksum algorithm.

Parameters:

  • type (Symbol)

    checksum algorithm; one of #CHECKSUM_TYPES



24
25
26
# File 'lib/libis/tools/checksum.rb', line 24

def initialize(type)
  @hasher = self.class.get_hasher(type)
end

Class Method Details

.base64digest(file_path_or_string, type) ⇒ Object

Calculate the base64 digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for

  • type (Symbol)

    checksum algorithm; one of #CHECKSUM_TYPES



64
65
66
# File 'lib/libis/tools/checksum.rb', line 64

def self.base64digest(file_path_or_string, type)
  new(type).base64digest(file_path_or_string)
end

.digest(file_path_or_string, type) ⇒ Object

Calculate the binary digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for

  • type (Symbol)

    checksum algorithm; one of #CHECKSUM_TYPES



50
51
52
# File 'lib/libis/tools/checksum.rb', line 50

def self.digest(file_path_or_string, type)
  new(type).digest(file_path_or_string)
end

.get_hasher(type) ⇒ Object

Instatiate a Digest instance for access to low-level functionality

Parameters:

  • type (Symbol)

    checksum algorithm; one of #CHECKSUM_TYPES

Raises:

  • (RuntimeError)


70
71
72
73
# File 'lib/libis/tools/checksum.rb', line 70

def self.get_hasher(type)
  raise RuntimeError, "Checksum type '#{type}' not supported." unless CHECKSUM_TYPES.include? type
  Digest(type).new
end

.hexdigest(file_path_or_string, type) ⇒ Object

Calculate the hexadecimal digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for

  • type (Symbol)

    checksum algorithm; one of #CHECKSUM_TYPES



57
58
59
# File 'lib/libis/tools/checksum.rb', line 57

def self.hexdigest(file_path_or_string, type)
  new(type).hexdigest(file_path_or_string)
end

Instance Method Details

#base64digest(file_path_or_string) ⇒ Object

Calculate the base64 digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for



43
44
45
# File 'lib/libis/tools/checksum.rb', line 43

def base64digest(file_path_or_string)
  @hasher.file(file_path_or_string).base64digest!
end

#digest(file_path_or_string) ⇒ Object

Calculate binary digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for



31
32
33
# File 'lib/libis/tools/checksum.rb', line 31

def digest(file_path_or_string)
  @hasher.file(file_path_or_string).digest!
end

#hexdigest(file_path_or_string) ⇒ Object

Calculate the hexadecimal digest of a file.

Parameters:

  • file_path_or_string (String)

    path of the file to calculate the digest for



37
38
39
# File 'lib/libis/tools/checksum.rb', line 37

def hexdigest(file_path_or_string)
  @hasher.file(file_path_or_string).hexdigest!
end