Sha256: 3f0228f102f236df38e775fd1b42a315d75882c3115808d30f43d1137b518625

Contents?: true

Size: 667 Bytes

Versions: 6

Compression:

Stored size: 667 Bytes

Contents

module Yubikey::ModHex
  
  TRANS = 'cbdefghijklnrtuv'.split(//)
  
  # Decode a ModHex string into binary data
  def self.decode(modhex_string)
    raise ArgumentError, "ModHex string length is not even" unless modhex_string.length % 2 == 0
    
    chars = 'cbdefghijklnrtuv'
    result = ""
    modhex_string.scan(/../).each do |c|
      result += (chars.index(c[0]) * 16 + chars.index(c[1])).chr
    end
    result
  end
  
  # Encode a binary string into ModHex
  def self.encode(string)
    result = ''
    
    string.each_byte do |b|
      result <<= TRANS[(b >> 4) & 0xF]
      result <<= TRANS[b & 0xF]
    end
    
    result
  end
  
end # Yubikey::ModHex

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
yubikey-1.4.1 lib/yubikey/modhex.rb
yubikey-1.4.0 lib/yubikey/modhex.rb
yubikey-1.3.1 lib/yubikey/modhex.rb
yubikey-1.3.0 lib/yubikey/modhex.rb
yubikey-1.2.1 lib/yubikey/modhex.rb
yubikey-1.2.0 lib/yubikey/modhex.rb