Example #1
0
    def add_event(self, s=''):
        """add_event(s:string)
        Add an event to the random pool.  The current time is stored
        between calls and used to estimate the entropy.  The optional
        's' parameter is a string that will also be XORed into the pool.
        Returns the estimated number of additional bits of entropy gain.
        """
        event = time.time() * 1000
        delta = self._noise()
        s = (s + long_to_bytes(event) +
             4 * chr(0xaa) + long_to_bytes(delta))
        self._addBytes(s)
        if event == self._event1 and event == self._event2:
            # If events are coming too closely together, assume there's
            # no effective entropy being added.
            bits = 0
        else:
            # Count the number of bits in delta, and assume that's the entropy.
            bits = 0
            while delta:
                delta, bits = delta >> 1, bits + 1
            if bits > 8:
                bits = 8

        self._event1, self._event2 = event, self._event1

        self._updateEntropyEstimate(bits)
        return bits
Example #2
0
    def add_event(self, s=''):
        """add_event(s:string)
        Add an event to the random pool.  The current time is stored
        between calls and used to estimate the entropy.  The optional
        's' parameter is a string that will also be XORed into the pool.
        Returns the estimated number of additional bits of entropy gain.
        """
        event = time.time()*1000
        delta = self._noise()
        s = (s + long_to_bytes(event) +
             4*chr(0xaa) + long_to_bytes(delta) )
        self._addBytes(s)
        if event==self._event1 and event==self._event2:
            # If events are coming too closely together, assume there's
            # no effective entropy being added.
            bits=0
        else:
            # Count the number of bits in delta, and assume that's the entropy.
            bits=0
            while delta:
                delta, bits = delta>>1, bits+1
            if bits>8: bits=8

        self._event1, self._event2 = event, self._event1

        self._updateEntropyEstimate(bits)
        return bits
Example #3
0
    def _noise(self):
        # Adds a bit of noise to the random pool, by adding in the
        # current time and CPU usage of this process.
        # The difference from the previous call to _noise() is taken
        # in an effort to estimate the entropy.
        t = time.time()
        delta = (t - self._lastcounter) / self._ticksize * 1e6
        self._lastcounter = t
        self._addBytes(long_to_bytes(long(1000 * time.time())))
        self._addBytes(long_to_bytes(long(1000 * time.clock())))
        self._addBytes(long_to_bytes(long(1000 * time.time())))
        self._addBytes(long_to_bytes(long(delta)))

        # Reduce delta to a maximum of 8 bits so we don't add too much
        # entropy as a result of this call.
        delta = delta % 0xff
        return int(delta)
Example #4
0
    def _noise(self):
        # Adds a bit of noise to the random pool, by adding in the
        # current time and CPU usage of this process.
        # The difference from the previous call to _noise() is taken
        # in an effort to estimate the entropy.
        t=time.time()
        delta = (t - self._lastcounter)/self._ticksize*1e6
        self._lastcounter = t
        self._addBytes(long_to_bytes(long(1000*time.time())))
        self._addBytes(long_to_bytes(long(1000*time.clock())))
        self._addBytes(long_to_bytes(long(1000*time.time())))
        self._addBytes(long_to_bytes(long(delta)))

        # Reduce delta to a maximum of 8 bits so we don't add too much
        # entropy as a result of this call.
        delta=delta % 0xff
        return int(delta)