Module: HumanDigest
- Defined in:
- lib/human_digest.rb,
lib/human_digest/version.rb
Overview
HumanDigest - Human-readable representations of digests
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.compress(bytes, target) ⇒ Object
Compress a byte array into a shorter length.
-
.humanize(hexdigest, words = 4, separator = '-') ⇒ Object
Make a digest human-readable.
Class Method Details
.compress(bytes, target) ⇒ Object
Compress a byte array into a shorter length
Example:
>> HumanDigest.compress([96, 173, 141, 13, 135, 27, 96, 149, 128, 130, 151])
=> [205, 128, 156, 96]
Arguments:
bytes: (Array)
target: (Integer)
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/human_digest.rb', line 74 def self.compress(bytes, target) bytes_copy = bytes.clone length = bytes_copy.length seg_size = (length / target).floor segments = [] target.times { segments.push(bytes_copy.slice!(0, seg_size)) } segments[-1].concat(bytes_copy) checksums = segments.collect { |a| a.reduce(:^) } return checksums end |
.humanize(hexdigest, words = 4, separator = '-') ⇒ Object
Make a digest human-readable
Example:
>> HumanDigest.humanize('60ad8d0d871b6095808297)
=> sodium-magnesium-nineteen-hydrogen
Arguments:
hexdigest: (String)
words: (Integer)
separator: (String)
55 56 57 58 59 60 61 62 |
# File 'lib/human_digest.rb', line 55 def self.humanize(hexdigest, words=4, separator='-') hexdigest = hexdigest.to_s unless hexdigest.respond_to?(:length) and hexdigest.respond_to?(:unpack) limit = hexdigest.length / 2 unpack_format = 'A2' * limit bytes = hexdigest.unpack(unpack_format).collect { |x| x.hex } compressed = compress(bytes, words) return (compressed.collect { |x| @word_list[x] }).join(separator) end |