summaryrefslogtreecommitdiff
path: root/tests/utils.py
blob: ad39f759090549d96dc9a1b8f2c5000c874bc4c9 (plain)
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
import os
import struct
from calendar import timegm
from datetime import datetime


def utc_timestamp():
    return timegm(datetime.utcnow().utctimetuple())


def key_path(key_name):
    return os.path.join(
        os.path.dirname(os.path.realpath(__file__)), "keys", key_name
    )


# Borrowed from `cryptography`
if hasattr(int, "from_bytes"):
    int_from_bytes = int.from_bytes
else:

    def int_from_bytes(data, byteorder, signed=False):
        assert byteorder == "big"
        assert not signed

        if len(data) % 4 != 0:
            data = (b"\x00" * (4 - (len(data) % 4))) + data

        result = 0

        while len(data) > 0:
            digit, = struct.unpack(">I", data[:4])
            result = (result << 32) + digit
            data = data[4:]

        return result