File: entropy.py

package info (click to toggle)
codeville 0.8.0-2.1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,140 kB
  • sloc: python: 10,335; ansic: 89; sh: 62; makefile: 25
file content (40 lines) | stat: -rw-r--r-- 1,014 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Written by Ross Cohen
# See LICENSE.txt for license information.

import sys

try:
    from os import urandom as random_string
except ImportError:
    if sys.platform == 'win32':
        from winrandom import winrandom as random_string

    else:
        dev = None

        def random_string(bytes):
            """Generate a random string with the given length."""
            global dev
            if dev is None:
                dev = open('/dev/urandom', 'r')
            return dev.read(bytes)

def random_long(bits):
    """Generate a random long integer with the given number of bits."""
    return string_to_long(random_string((bits+7)/8)) % (1L<<bits)

def string_to_long(s):
    """Convert a string of bytes into a long integer."""
    r = 0L
    for c in s:
	r = (r << 8) + ord(c)
    return r

def long_to_string(i, length=0):
    """Convert a long integer into a string of bytes."""
    s = ''
    while i > 0:
	s = chr(i & 255) + s
	i = i >> 8
    s = '\x00' * (length - len(s)) + s
    return s