OTR plugin for Gajim 1.0+
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright 2012 Kjell Braden <afflux@pentabarf.de>
  2. #
  3. # This file is part of the python-potr library.
  4. #
  5. # python-potr is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # any later version.
  9. #
  10. # python-potr is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this library. If not, see <http://www.gnu.org/licenses/>.
  17. # some python3 compatibilty
  18. from __future__ import unicode_literals
  19. import struct
  20. def pack_mpi(n):
  21. return pack_data(long_to_bytes(n))
  22. def read_mpi(data):
  23. n, data = read_data(data)
  24. return bytes_to_long(n), data
  25. def pack_data(data):
  26. return struct.pack(b'!I', len(data)) + data
  27. def read_data(data):
  28. datalen, data = unpack(b'!I', data)
  29. return data[:datalen], data[datalen:]
  30. def unpack(fmt, buf):
  31. s = struct.Struct(fmt)
  32. return s.unpack(buf[:s.size]) + (buf[s.size:],)
  33. def bytes_to_long(b):
  34. l = len(b)
  35. s = 0
  36. for i in range(l):
  37. s += byte_to_long(b[i:i+1]) << 8*(l-i-1)
  38. return s
  39. def long_to_bytes(l, n=0):
  40. b = b''
  41. while l != 0 or n > 0:
  42. b = long_to_byte(l & 0xff) + b
  43. l >>= 8
  44. n -= 1
  45. return b
  46. def byte_to_long(b):
  47. return struct.unpack(b'B', b)[0]
  48. def long_to_byte(l):
  49. return struct.pack(b'B', l)
  50. def human_hash(fp):
  51. fp = fp.upper()
  52. fplen = len(fp)
  53. wordsize = fplen//5
  54. buf = ''
  55. for w in range(0, fplen, wordsize):
  56. buf += '{0} '.format(fp[w:w+wordsize])
  57. return buf.rstrip()