Class: Libis::Tools::Checksum
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
-
.base64digest(file_path_or_string, type) ⇒ Object
Calculate the base64 digest of a file.
-
.digest(file_path_or_string, type) ⇒ Object
Calculate the binary digest of a file.
-
.get_hasher(type) ⇒ Object
Instatiate a Digest instance for access to low-level functionality.
-
.hexdigest(file_path_or_string, type) ⇒ Object
Calculate the hexadecimal digest of a file.
Instance Method Summary collapse
-
#base64digest(file_path_or_string) ⇒ Object
Calculate the base64 digest of a file.
-
#digest(file_path_or_string) ⇒ Object
Calculate binary digest of a file.
-
#hexdigest(file_path_or_string) ⇒ Object
Calculate the hexadecimal digest of a file.
-
#initialize(type) ⇒ Checksum
constructor
Create instance for a given checksum algorithm.
Constructor Details
#initialize(type) ⇒ Checksum
Create instance for a given checksum algorithm.
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.
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.
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
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.
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.
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.
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.
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 |