summaryrefslogtreecommitdiff
path: root/paramiko/primes.py
diff options
context:
space:
mode:
authorRobey Pointer <robey@lag.net>2004-01-04 09:29:13 +0000
committerRobey Pointer <robey@lag.net>2004-01-04 09:29:13 +0000
commit988c6abda08dd7380da37cfc74b9642437afe1ae (patch)
tree64ef726eb8fdf17e01f832a8487c7a2776939067 /paramiko/primes.py
parent3a8887a42083dda796f50e1e9b32f625abcb5d5a (diff)
downloadparamiko-988c6abda08dd7380da37cfc74b9642437afe1ae.tar.gz
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-20]
more docs, and password-protected key files can now be read lots more documentation, some of it moved out of the README file, which is now much smaller and less rambling. repr(Transport) now reports the number of bits used in the cipher. cleaned up BER to use util functions, and throw a proper exception (the new BERException) on error. it doesn't ever have to be a full BER decoder, but it can at least comb its hair and tuck in its shirt. lots of stuff added to PKey.read_private_key_file so it can try to decode password-protected key files. right now it only understands "DES-EDE3-CBC" format, but this is the only format i've seen openssh make so far. if the key is password-protected, but no password was given, a new exception (PasswordRequiredException) is raised so an outer layer can ask for a password and try again.
Diffstat (limited to 'paramiko/primes.py')
-rw-r--r--paramiko/primes.py24
1 files changed, 14 insertions, 10 deletions
diff --git a/paramiko/primes.py b/paramiko/primes.py
index 68e7fc19..85569ceb 100644
--- a/paramiko/primes.py
+++ b/paramiko/primes.py
@@ -1,28 +1,32 @@
+#!/usr/bin/python
-# utility functions for dealing with primes
+"""
+Utility functions for dealing with primes.
+"""
from Crypto.Util import number
-from util import bit_length, inflate_long
+import util
-def generate_prime(bits, randpool):
+def _generate_prime(bits, randpool):
+ "primtive attempt at prime generation"
hbyte_mask = pow(2, bits % 8) - 1
while 1:
# loop catches the case where we increment n into a higher bit-range
x = randpool.get_bytes((bits+7) // 8)
if hbyte_mask > 0:
x = chr(ord(x[0]) & hbyte_mask) + x[1:]
- n = inflate_long(x, 1)
+ n = util.inflate_long(x, 1)
n |= 1
n |= (1 << (bits - 1))
while not number.isPrime(n):
n += 2
- if bit_length(n) == bits:
+ if util.bit_length(n) == bits:
return n
-def roll_random(randpool, n):
+def _roll_random(randpool, n):
"returns a random # from 0 to N-1"
- bits = bit_length(n-1)
+ bits = util.bit_length(n-1)
bytes = (bits + 7) // 8
hbyte_mask = pow(2, bits % 8) - 1
@@ -36,7 +40,7 @@ def roll_random(randpool, n):
x = randpool.get_bytes(bytes)
if hbyte_mask > 0:
x = chr(ord(x[0]) & hbyte_mask) + x[1:]
- num = inflate_long(x, 1)
+ num = util.inflate_long(x, 1)
if num < n:
return num
@@ -75,7 +79,7 @@ class ModulusPack (object):
# there's a bug in the ssh "moduli" file (yeah, i know: shock! dismay!
# call cnn!) where it understates the bit lengths of these primes by 1.
# this is okay.
- bl = bit_length(modulus)
+ bl = util.bit_length(modulus)
if (bl != size) and (bl != size + 1):
self.discarded.append((modulus, 'incorrectly reported bit length %d' % size))
return
@@ -123,6 +127,6 @@ class ModulusPack (object):
if min > good:
good = bitsizes[-1]
# now pick a random modulus of this bitsize
- n = roll_random(self.randpool, len(self.pack[good]))
+ n = _roll_random(self.randpool, len(self.pack[good]))
return self.pack[good][n]