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.

There are two ways this can be used: using a class instance or using class methods. When a class instance is used, the desired checksum type has to be supplied when the instance is created. Each call to a checksum method will calculate the checksum and reset the digest to prevent future calls to be affected by the current result. When a class method is used besides the file name, the checksum type has to be supplied.

Examples:

require 'libis/tools/checksum'
checksum = ::Libis::Tools::Checksum.new(:MD5)
puts "Checksum: #{checksum.hexdigest(file_name)} (MD5, hex)"

require 'libis/tools/checksum'
puts "Checksum: #{::Libis::Tools::Checksum.base64digest(file_name, :SHA384)} (SHA-2, 384 bit, base64)"

Constant Summary collapse

CHECKSUM_TYPES =

All supported checksum types

[:MD5, :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:



41
42
43
# File 'lib/libis/tools/checksum.rb', line 41

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



81
82
83
# File 'lib/libis/tools/checksum.rb', line 81

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



67
68
69
# File 'lib/libis/tools/checksum.rb', line 67

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:

Raises:

  • (RuntimeError)


87
88
89
90
# File 'lib/libis/tools/checksum.rb', line 87

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



74
75
76
# File 'lib/libis/tools/checksum.rb', line 74

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



60
61
62
# File 'lib/libis/tools/checksum.rb', line 60

def base64digest(file_path_or_string)
  hashit(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



48
49
50
# File 'lib/libis/tools/checksum.rb', line 48

def digest(file_path_or_string)
  hashit(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



54
55
56
# File 'lib/libis/tools/checksum.rb', line 54

def hexdigest(file_path_or_string)
  hashit(file_path_or_string).hexdigest!
end