Module: Yubikey::ModHex
- Defined in:
- lib/yubikey/modhex.rb
Constant Summary collapse
- TRANS =
'cbdefghijklnrtuv'.split(//)
Class Method Summary collapse
-
.decode(modhex_string) ⇒ Object
Decode a ModHex string into binary data.
-
.encode(string) ⇒ Object
Encode a binary string into ModHex.
Class Method Details
.decode(modhex_string) ⇒ Object
Decode a ModHex string into binary data
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/yubikey/modhex.rb', line 6 def self.decode(modhex_string) out = '' flag = true # to switch between first and last nibble byte = 0 raise ArgumentError, "ModHex string length is not even" unless modhex_string.length % 2 == 0 modhex_string.each_byte do |b| x = TRANS.index(b.chr) # lookup occurrence in table if flag byte = x else byte = (byte << 4) | x out <<= byte.chr end flag = !flag end out end |
.encode(string) ⇒ Object
Encode a binary string into ModHex
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/yubikey/modhex.rb', line 28 def self.encode(string) out = '' string.each_byte do |b| out <<= TRANS[(b >> 4) & 0xF] out <<= TRANS[b & 0xF] end out end |