Module: Voltronic::Digest
- Defined in:
- lib/voltronic/digest.rb
Overview
:nodoc:
Defined Under Namespace
Classes: DigestMismatchError, MalformedInputError
Class Method Summary collapse
-
.encode(input) ⇒ Object
:nodoc:.
-
.eos?(input) ⇒ Boolean
:nodoc:.
-
.parse(input) ⇒ Object
:nodoc:.
Class Method Details
.encode(input) ⇒ Object
:nodoc:
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/voltronic/digest.rb', line 39 def self.encode(input) #:nodoc: input = input.to_s crc = 0 input.bytes.each do |byte| crc = ((@@crc_table[(((crc >> 8) ^ byte) & 0xff)] ^ (crc << 8)) & 0xffff) end first_byte = ((crc & 0xff00) >> 8) second_byte = (crc & 0xff) if ((0x28 == first_byte) || (0x0d == first_byte) || (0x0a == first_byte)) first_byte += 1 end if ((0x28 == second_byte) || (0x0d == second_byte) || (0x0a == second_byte)) second_byte += 1 end [input, first_byte.chr, second_byte.chr, @@termination_character.chr].join end |
.eos?(input) ⇒ Boolean
:nodoc:
59 60 61 |
# File 'lib/voltronic/digest.rb', line 59 def self.eos?(input) #:nodoc: input.to_s.bytes.any? { |byte| (@@termination_character == byte) } end |
.parse(input) ⇒ Object
:nodoc:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/voltronic/digest.rb', line 3 def self.parse(input) #:nodoc: input = begin input_bytes = input.to_s.bytes end_index = input_bytes.length while(0 != end_index) if (@@termination_character == input_bytes[(end_index -= 1)]) break end end input_bytes[0..end_index] end if (3 > input.length) raise MalformedInputError.new 'Input must be at at least 3 bytes in size' end input_crc = input[-3..-2] input = input[0..-4].map do |byte| if (128 > byte) byte.chr else raise MalformedInputError.new 'Input contained an unparsable character' end end.join calculated_crc = encode(input).to_s.bytes[-3..-2] if (calculated_crc == input_crc) return input end raise DigestMismatchError.new ['Device digest [', input_crc.first, ',', input_crc.last, '] != [', calculated_crc.first, ',', calculated_crc.last, '] calculated digest'].join end |