~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/conch/ssh/common.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
5
 
 
6
 
"""Common functions for the SSH classes.
7
 
 
8
 
This module is unstable.
9
 
 
10
 
Maintainer: U{Paul Swartz<mailto:z3p@twistedmatrix.com>}
11
 
"""
12
 
 
13
 
import struct
14
 
try:
15
 
    from Crypto import Util
16
 
    from Crypto.Util import randpool
17
 
except ImportError:
18
 
    import warnings
19
 
    warnings.warn("PyCrypto not installed, but continuing anyways!", 
20
 
            RuntimeWarning)
21
 
else:
22
 
    entropy = randpool.RandomPool()
23
 
    entropy.stir()
24
 
 
25
 
 
26
 
def NS(t):
27
 
    """
28
 
    net string
29
 
    """
30
 
    return struct.pack('!L',len(t)) + t
31
 
 
32
 
def getNS(s, count=1):
33
 
    """
34
 
    get net string
35
 
    """
36
 
    ns = []
37
 
    c = 0
38
 
    for i in range(count):
39
 
        l, = struct.unpack('!L',s[c:c+4])
40
 
        ns.append(s[c+4:4+l+c])
41
 
        c += 4 + l
42
 
    return tuple(ns) + (s[c:],)
43
 
 
44
 
def MP(number):
45
 
    if number==0: return '\000'*4
46
 
    assert number>0
47
 
    bn = Util.number.long_to_bytes(number)
48
 
    if ord(bn[0])&128:
49
 
        bn = '\000' + bn
50
 
    return struct.pack('>L',len(bn)) + bn
51
 
 
52
 
def getMP(data):
53
 
    """
54
 
    get multiple precision integer
55
 
    """
56
 
    length=struct.unpack('>L',data[:4])[0]
57
 
    return Util.number.bytes_to_long(data[4:4+length]),data[4+length:]
58
 
 
59
 
def _MPpow(x, y, z):
60
 
    """return the MP version of (x**y)%z
61
 
    """
62
 
    return MP(pow(x,y,z))
63
 
 
64
 
def ffs(c, s):
65
 
    """
66
 
    first from second
67
 
    goes through the first list, looking for items in the second, returns the first one
68
 
    """
69
 
    for i in c:
70
 
        if i in s: return i
71
 
 
72
 
getMP_py = getMP
73
 
MP_py = MP
74
 
_MPpow_py = _MPpow
75
 
pyPow = pow
76
 
 
77
 
def _fastgetMP(i):
78
 
    l = struct.unpack('!L', i[:4])[0]
79
 
    n = i[4:l+4][::-1]
80
 
    return long(gmpy.mpz(n+'\x00', 256)), i[4+l:]
81
 
 
82
 
def _fastMP(i):
83
 
    i2 = gmpy.mpz(i).binary()[::-1]
84
 
    return struct.pack('!L', len(i2)) + i2
85
 
 
86
 
def _fastMPpow(x, y, z=None):
87
 
    r = pyPow(gmpy.mpz(x),y,z).binary()[::-1]
88
 
    return struct.pack('!L', len(r)) + r
89
 
 
90
 
def _fastpow(x, y, z=None):
91
 
    return pyPow(gmpy.mpz(x), y, z)
92
 
 
93
 
def install():
94
 
    global getMP, MP, _MPpow
95
 
    getMP = _fastgetMP
96
 
    MP = _fastMP
97
 
    _MPpow = _fastMPpow
98
 
    __builtins__['pow'] = _fastpow # evil evil
99
 
    
100
 
try:
101
 
    import gmpy
102
 
    install()
103
 
except ImportError:
104
 
    pass
105