~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/conch/common.py

  • Committer: z3p
  • Date: 2002-07-17 14:44:36 UTC
  • Revision ID: vcs-imports@canonical.com-20020717144436-6dce525e73d836e2
moving secsh to conch.
Conch: The Twisted Shell

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import struct
 
2
from Crypto import Util
 
3
 
 
4
def NS(t):
 
5
    """
 
6
    net string
 
7
    """
 
8
    return struct.pack('!L',len(t)) + t
 
9
 
 
10
def getNS(s, count=1):
 
11
    """
 
12
    get net string
 
13
    """
 
14
    ns = []
 
15
    for i in range(count):
 
16
        l = struct.unpack('!L',s[:4])[0]
 
17
        ns.append(s[4:4+l])
 
18
        s = s[4+l:]
 
19
    return tuple(ns) + (s,)
 
20
 
 
21
def MP(number):
 
22
    if number==0: return '\000'*4
 
23
    assert number>0
 
24
    bn = Util.number.long_to_bytes(number)
 
25
    if ord(bn[0])&128:
 
26
        bn = '\000' + bn
 
27
    return struct.pack('>L',len(bn)) + bn
 
28
 
 
29
def getMP(data):
 
30
    """
 
31
    get multiple precision integer
 
32
    """
 
33
    length=struct.unpack('>L',data[:4])[0]
 
34
    return Util.number.bytes_to_long(data[4:4+length]),data[4+length:]
 
35
 
 
36
def ffs(c, s):
 
37
    """
 
38
    first from second
 
39
    goes through the first list, looking for items in the second, returns the first one
 
40
    """
 
41
    for i in c:
 
42
        if i in s: return i
 
43
 
 
44
 
 
45