1 import random
2
4 '''
5 Creates a tcpdump-style hex dump string output.
6 @type src: String
7 @param src: Input string to convert to hexdump output.
8 @type length: Int
9 @param length: Optional length of data for a single row of output, def=16
10 @rtype: String
11 '''
12 FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
13 result = []
14 for i in xrange(0, len(src), length):
15 chars = src[i:i+length]
16 hex = ' '.join(["%02x" % ord(x) for x in chars])
17 printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
18 result.append("%04x: %-*s %s\n" % (i, length*3, hex, printable))
19 return ''.join(result)
20
22 '''
23 Returns a random string of size bytes. Not cryptographically safe.
24 @type size: Int
25 @param size: Length of random data to return.
26 @rtype: String
27 '''
28 return ''.join(chr(random.randrange(0,256)) for i in xrange(size))
29
31 '''
32 Returns a random MAC address using a list valid OUI's from ZigBee device manufacturers. Data is returned in air-format byte order (LSB first).
33 @type length: String
34 @param length: Optional length of MAC address, def=8. Minimum address return length is 3 bytes for the valid OUI.
35 @rtype: String
36 '''
37
38 prefixes = [ "\x00\x0d\x6f",
39 "\x00\x12\x4b",
40 "\x00\x04\xa3",
41 "\x00\x04\x25",
42 "\x00\x11\x7d",
43 "\x00\x13\xa2",
44 "\x00\x30\x66",
45 "\x00\x0b\x57",
46 "\x00\x04\x9f",
47 "\x00\x21\xed",
48 "\x00\xa0\x50"
49 ]
50
51 prefix = prefixes[random.randrange(0, len(prefixes))]
52 suffix = randbytes(length-3)
53
54 return ''.join([prefix, suffix])[::-1]
55