/usr/lib/python2.7/dist-packages/fte/bit_ops.py is in python-fte 0.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 41 42 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import binascii
import Crypto.Random
def random_bytes(N):
"""Given an input integer ``N``, ``random_bytes`` returns a string of exactly ``N`` uniformly-random bytes.
"""
return Crypto.Random.get_random_bytes(N)
def long_to_bytes(N, blocksize=1):
"""Given an input integer ``N``, ``long_to_bytes`` returns the representation of ``N`` in bytes.
If ``blocksize`` is greater than ``1`` then the output string will be right justified and then padded with zero-bytes,
such that the return values length is a multiple of ``blocksize``.
"""
bytestring = hex(N)
bytestring = bytestring[2:] if bytestring.startswith('0x') else bytestring
bytestring = bytestring[:-1] if bytestring.endswith('L') else bytestring
bytestring = '0' + bytestring if (len(bytestring) % 2) != 0 else bytestring
bytestring = binascii.unhexlify(bytestring)
if blocksize > 0 and len(bytestring) % blocksize != 0:
bytestring = '\x00' * \
(blocksize - (len(bytestring) % blocksize)) + bytestring
return bytestring
def bytes_to_long(bytestring):
"""Given a ``bytestring`` returns its integer representation ``N``.
"""
bytestring = '\x00' + bytestring
N = int(bytestring.encode('hex'), 16)
return N
|