Sha256: a07d36f3ad1ddce0e693a122146af0d8a2206e61dc2b0423e606c029fc673adc

Contents?: true

Size: 816 Bytes

Versions: 6

Compression:

Stored size: 816 Bytes

Contents

module Yubikey::ModHex
  
  TRANS = 'cbdefghijklnrtuv'.split(//)
  
  # Decode a ModHex string into binary data
  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 a binary string into ModHex
  def self.encode(string)
    out = ''
    
    string.each_byte do |b|
      out <<= TRANS[(b >> 4) & 0xF]
      out <<= TRANS[b & 0xF]
    end
    
    out
  end
  
end # Yubikey::ModHex

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
titanous-yubikey-1.0.2 lib/yubikey/modhex.rb
titanous-yubikey-1.1.0 lib/yubikey/modhex.rb
titanous-yubikey-1.1.1 lib/yubikey/modhex.rb
yubikey-1.1.1 lib/yubikey/modhex.rb
yubikey-1.0.2 lib/yubikey/modhex.rb
yubikey-1.1.0 lib/yubikey/modhex.rb