OLD | NEW |
1 """cryptomath module | 1 """cryptomath module |
2 | 2 |
3 This module has basic math/crypto code.""" | 3 This module has basic math/crypto code.""" |
4 | 4 |
5 import os | 5 import os |
6 import math | 6 import math |
7 import base64 | 7 import base64 |
8 import binascii | 8 import binascii |
9 | 9 |
10 # The sha module is deprecated in Python 2.6 | 10 # The sha module is deprecated in Python 2.6 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 122 |
123 def bytesToNumber(bytes): | 123 def bytesToNumber(bytes): |
124 total = 0L | 124 total = 0L |
125 multiplier = 1L | 125 multiplier = 1L |
126 for count in range(len(bytes)-1, -1, -1): | 126 for count in range(len(bytes)-1, -1, -1): |
127 byte = bytes[count] | 127 byte = bytes[count] |
128 total += multiplier * byte | 128 total += multiplier * byte |
129 multiplier *= 256 | 129 multiplier *= 256 |
130 return total | 130 return total |
131 | 131 |
132 def numberToBytes(n): | 132 def numberToBytes(n, howManyBytes=None): |
133 howManyBytes = numBytes(n) | 133 if howManyBytes == None: |
| 134 howManyBytes = numBytes(n) |
134 bytes = createByteArrayZeros(howManyBytes) | 135 bytes = createByteArrayZeros(howManyBytes) |
135 for count in range(howManyBytes-1, -1, -1): | 136 for count in range(howManyBytes-1, -1, -1): |
136 bytes[count] = int(n % 256) | 137 bytes[count] = int(n % 256) |
137 n >>= 8 | 138 n >>= 8 |
138 return bytes | 139 return bytes |
139 | 140 |
140 def bytesToBase64(bytes): | 141 def bytesToBase64(bytes): |
141 s = bytesToString(bytes) | 142 s = bytesToString(bytes) |
142 return stringToBase64(s) | 143 return stringToBase64(s) |
143 | 144 |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 if (q >= high): | 403 if (q >= high): |
403 q = getRandomNumber(low, high) | 404 q = getRandomNumber(low, high) |
404 q += 29 - (q % 30) | 405 q += 29 - (q % 30) |
405 #Ideas from Tom Wu's SRP code | 406 #Ideas from Tom Wu's SRP code |
406 #Do trial division on p and q before Rabin-Miller | 407 #Do trial division on p and q before Rabin-Miller |
407 if isPrime(q, 0, display=display): | 408 if isPrime(q, 0, display=display): |
408 p = (2 * q) + 1 | 409 p = (2 * q) + 1 |
409 if isPrime(p, display=display): | 410 if isPrime(p, display=display): |
410 if isPrime(q, display=display): | 411 if isPrime(q, display=display): |
411 return p | 412 return p |
OLD | NEW |