YubiOTP Utilities

yubiotp.modhex

Implementation of modhex encoding, which uses keyboard-independent characters.

hex digit:    0123456789abcdef
modhex digit: cbdefghijklnrtuv
yubiotp.modhex.modhex(data)[source]

Encode a string of bytes as modhex.

>>> modhex(b'abcdefghijklmnop') == b'hbhdhehfhghhhihjhkhlhnhrhthuhvic'
True
yubiotp.modhex.unmodhex(encoded)[source]

Decode a modhex string to its binary form.

>>> unmodhex(b'hbhdhehfhghhhihjhkhlhnhrhthuhvic') == b'abcdefghijklmnop'
True
yubiotp.modhex.is_modhex(encoded)[source]

Returns True iff the given string is valid modhex.

>>> is_modhex(b'cbdefghijklnrtuv')
True
>>> is_modhex(b'cbdefghijklnrtuvv')
False
>>> is_modhex(b'cbdefghijklnrtuvyy')
False
yubiotp.modhex.hex_to_modhex(hex_str)[source]

Convert a string of hex digits to a string of modhex digits.

>>> hex_to_modhex(b'69b6481c8baba2b60e8f22179b58cd56') == b'hknhfjbrjnlnldnhcujvddbikngjrtgh'
True
>>> hex_to_modhex(b'6j')
Traceback (most recent call last):
    ...
ValueError: Illegal hex character in input
yubiotp.modhex.modhex_to_hex(modhex_str)[source]

Convert a string of modhex digits to a string of hex digits.

>>> modhex_to_hex(b'hknhfjbrjnlnldnhcujvddbikngjrtgh') == b'69b6481c8baba2b60e8f22179b58cd56'
True
>>> modhex_to_hex(b'hbhdxx')
Traceback (most recent call last):
    ...
ValueError: Illegal modhex character in input

yubiotp.crc

CRC16 implementation for Yubico OTP.

yubiotp.crc.crc16(data)[source]

Generate the crc-16 value for a byte string.

>>> from binascii import unhexlify
>>> c = crc16(unhexlify(b'8792ebfe26cc130030c20011c89f'))
>>> hex(~c & 0xffff)
'0xc823'
>>> v = crc16(unhexlify(b'8792ebfe26cc130030c20011c89f23c8'))
>>> hex(v)
'0xf0b8'
yubiotp.crc.verify_crc16(data)[source]

Return true if this given byte string has a valid crc-16 residual.

>>> from binascii import unhexlify
>>> verify_crc16(unhexlify(b'8792ebfe26cc130030c20011c89f23c8'))
True
>>> verify_crc16(unhexlify(b'0792ebfe26cc130030c20011c89f23c8'))
False