Encoding

This shows all the encoding objects and usages.

All of these classes are importable from dofutils.encoding

Base64

class dofutils.encoding.Base64
static chr(value: int) str

Get the base64 character from the given value

Parameters:

value – The value to convert

Raises:

ValueError – when value is not in the charset

Returns:

Encoded value

Return type:

str

static chr_mod(value: int) str

Get the base64 character from the given value after applying a modulo on it to ensure the call will not fail

Parameters:

value – The value to convert

Returns:

Encoded value

Return type:

str

static decode(encoded: str) int

Decode pseudo base64 value to int

Parameters:

encoded – the encoded value

Returns:

the decoded value

Raises:

ValueError – when the encoded string is not in range [1-6]

Return type:

int

static encode(value: int, length: int) str

Encode a int value to pseudo base64

Parameters:
  • value – value to encode

  • length – then expected result length. Must be in range [1-6]

Raises:

ValueError – when the encoded string is not in range [1-6]

Returns:

the decoded value

Return type:

str

static ord(char: str) int

Convert the given single char value in base64

Parameters:

char – The char to convert

Raises:
  • ValueError – When parameter char isn’t a single character

  • ValueError – When parameter char is outside the charset

Returns:

The int value, between 0 and 63 included

Return type:

int

static to_bytes(encoded: str) bytearray

Decode a Base 64 string to a byte array Each byte will represents the {@link Base64#ord(char)} value of each characters

Parameters:

encoded – the encoded value

Returns:

the decoded byte array. The array size will be the same as the string size

Return type:

bytearray

CheckSum

class dofutils.encoding.CheckSum
static hexadecimal(value: str) str

Compute the checksum as an hexadecimal string The return value is a single hexadecimal char string

Parameters:

value – The value to compute

Returns:

The hexadecimal checksum of the given value

Return type:

str

static integer(value: str) int

Compute the checksum as in integer

Parameters:

value – The value to compute

Returns:

the checksum of the given value

Return type:

str

static verify_int(input: str, expected: int) bool

Verify the checksum of the input

Parameters:
  • input – The value to check

  • expected – The expected checksum as int

Returns:

true if checksum match, false otherwise

Return type:

bool

static verify_str(input: str, expected: str) bool

Verify the checksum of the input value

Parameters:
  • input – The input value to check

  • expected – The expected checksum value as an hexadecimal string

Returns:

true if checksum match, false otherwise

Return type:

bool

Key

class dofutils.encoding.Key(key: str)
property cipher: XorCipher

Get the cipher relative to this key. The cipher instance is saved into the key

Returns:

The cipher instance

Return type:

XorCipher

encode() str

Encode the key to hexadecimal string

Returns:

The encoded key

Return type:

str

static generate(size: int = 128) Key

Generate a new random key

Parameters:

size – The key size

Returns:

The generated key

Return type:

Key

property key: str

Return the current key used

Returns:

The current key used

Return type:

str

static parse(input: str) Key

Parse a hexadecimal key string

Parameters:

input – The key to parse

Raises:

ValueError – When invalid key is not even

Returns:

The key instance

Return type:

Key

PasswordEncoder

class dofutils.encoding.PasswordEncoder(key: str)
decode(encoded: str) str

decode the given encoded string using the key

Parameters:

encoded – the encoded string

Raises:

ValueError – when the encoded string is invalid or the key is too small

Returns:

the decoded string in base64 format

Return type:

str

encode(password: str) str

Encode the given password using the key

Parameters:

password – the raw password

Raises:

ValueError – when the password is too long

Returns:

the encoded password

Return type:

str

property key: str

Get the encoding key

Returns:

the key

Return type:

str

XorCipher

class dofutils.encoding.XorCipher(key: str)
decrypt(value: str, key_offset: int) str

Decrypt the given value by using the key

Parameters:
  • value – The value to decrypt. Must be an hexadecimal string

  • key_offset – Offset to use on the key. Must be the same used for encryption

Raises:

ValueError – When encrypted value is not odd

Returns:

The decrypted value

Return type:

str

encrypt(value: str, key_offset: int) str

Encrypt the given value with the current xor cipher key The encrypted value is an upper case hexadecimal string

Parameters:
  • value – Value to encrypt

  • key_offset – Offset to use for the key

Returns:

Encrypted value

Return type:

str

property key: str

Return the key

Returns:

the key used by the xor cipher

Return type:

str