util.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. Copyright (c) 2014 Philippe Teuwen <phil@teuwen.org>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. this software and associated documentation files (the "Software"), to deal in
  5. the Software without restriction, including without limitation the rights to
  6. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is furnished to do
  8. so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. This code is taken from https://github.com/doegox/python-cryptoplus/
  19. """
  20. def number2string(i):
  21. """Convert a number to a string
  22. Input: long or integer
  23. Output: string (big-endian)
  24. """
  25. s=hex(i)[2:].rstrip('L')
  26. if len(s) % 2:
  27. s = '0' + s
  28. return s.decode('hex')
  29. def number2string_N(i, N):
  30. """Convert a number to a string of fixed size
  31. i: long or integer
  32. N: length of string
  33. Output: string (big-endian)
  34. """
  35. s = '%0*x' % (N*2, i)
  36. return s.decode('hex')
  37. def string2number(i):
  38. """ Convert a string to a number
  39. Input: string (big-endian)
  40. Output: long or integer
  41. """
  42. return int(i.encode('hex'),16)
  43. def xorstring(a,b):
  44. """XOR two strings of same length
  45. For more complex cases, see CryptoPlus.Cipher.XOR"""
  46. assert len(a) == len(b)
  47. return number2string_N(string2number(a)^string2number(b), len(a))
  48. class Counter(str):
  49. #found here: http://www.lag.net/pipermail/paramiko/2008-February.txt
  50. """Necessary for CTR chaining mode
  51. Initializing a counter object (ctr = Counter('xxx'), gives a value to the counter object.
  52. Everytime the object is called ( ctr() ) it returns the current value and increments it by 1.
  53. Input/output is a raw string.
  54. Counter value is big endian"""
  55. def __init__(self, initial_ctr):
  56. if not isinstance(initial_ctr, str):
  57. raise TypeError("nonce must be str")
  58. self.c = int(initial_ctr.encode('hex'), 16)
  59. def __call__(self):
  60. # This might be slow, but it works as a demonstration
  61. ctr = ("%032x" % (self.c,)).decode('hex')
  62. self.c += 1
  63. return ctr