2 """Convert a number to a string
5 Output: string (big-endian)
7 s
=hex(i
)[2:].rstrip('L')
10 return s
.decode('hex')
12 def number2string_N(i
, N
):
13 """Convert a number to a string of fixed size
17 Output: string (big-endian)
20 return s
.decode('hex')
23 """ Convert a string to a number
25 Input: string (big-endian)
26 Output: long or integer
28 return int(i
.encode('hex'),16)
31 """XOR two strings of same length
33 For more complex cases, see CryptoPlus.Cipher.XOR"""
34 assert len(a
) == len(b
)
35 return number2string_N(string2number(a
)^
string2number(b
), len(a
))
38 #found here: http://www.lag.net/pipermail/paramiko/2008-February.txt
39 """Necessary for CTR chaining mode
41 Initializing a counter object (ctr = Counter('xxx'), gives a value to the counter object.
42 Everytime the object is called ( ctr() ) it returns the current value and increments it by 1.
43 Input/output is a raw string.
45 Counter value is big endian"""
46 def __init__(self
, initial_ctr
):
47 if not isinstance(initial_ctr
, str):
48 raise TypeError("nonce must be str")
49 self
.c
= int(initial_ctr
.encode('hex'), 16)
51 # This might be slow, but it works as a demonstration
52 ctr
= ("%032x" % (self
.c
,)).decode('hex')