utils.py 803 B

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. import struct
  3. from calendar import timegm
  4. from datetime import datetime
  5. def utc_timestamp():
  6. return timegm(datetime.utcnow().utctimetuple())
  7. def key_path(key_name):
  8. return os.path.join(os.path.dirname(os.path.realpath(__file__)),
  9. 'keys', key_name)
  10. # Borrowed from `cryptography`
  11. if hasattr(int, "from_bytes"):
  12. int_from_bytes = int.from_bytes
  13. else:
  14. def int_from_bytes(data, byteorder, signed=False):
  15. assert byteorder == 'big'
  16. assert not signed
  17. if len(data) % 4 != 0:
  18. data = (b'\x00' * (4 - (len(data) % 4))) + data
  19. result = 0
  20. while len(data) > 0:
  21. digit, = struct.unpack('>I', data[:4])
  22. result = (result << 32) + digit
  23. data = data[4:]
  24. return result