File: utils.py

package info (click to toggle)
python-srptools 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 184 kB
  • sloc: python: 658; makefile: 6; sh: 5
file content (64 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import unicode_literals
from binascii import unhexlify, hexlify
from base64 import b64encode, b64decode

from six import integer_types


def value_encode(val, base64=False):
    """Encodes int into hex or base64."""
    return b64_from(val) if base64 else hex_from(val)


def hex_from_b64(val):
    """Returns hex string representation for base64 encoded value.

    :param str val:
    :rtype: bytes|str
    """
    return hex_from(b64decode(val))


def hex_from(val):
    """Returns hex string representation for a given value.

    :param bytes|str|unicode|int|long val:
    :rtype: bytes|str
    """
    if isinstance(val, integer_types):
        hex_str = '%x' % val
        if len(hex_str) % 2:
            hex_str = '0' + hex_str
        return hex_str

    return hexlify(val)


def int_from_hex(hexstr):
    """Returns int/long representation for a given hex string.

    :param bytes|str|unicode hexstr:
    :rtype: int|long
    """
    return int(hexstr, 16)


def int_to_bytes(val):
    """Returns bytes representation for a given int/long.

    :param int|long val:
    :rtype: bytes|str
    """
    hex_str = hex_from(val)
    return unhexlify(hex_str)


def b64_from(val):
    """Returns base64 encoded bytes for a given int/long/bytes value.

    :param int|long|bytes val:
    :rtype: bytes|str
    """
    if isinstance(val, integer_types):
        val = int_to_bytes(val)
    return b64encode(val).decode('ascii')