Merge lp:~habnabit/pyskein/getrandbits into lp:pyskein

Proposed by Aaron Gallagher on 2013-07-08
Status: Needs review
Proposed branch: lp:~habnabit/pyskein/getrandbits
Merge into: lp:pyskein
Diff against target: 38 lines (+17/-0)
2 files modified
src/skein.py (+9/-0)
test/test_skein.py (+8/-0)
To merge this branch: bzr merge lp:~habnabit/pyskein/getrandbits
Reviewer Review Type Date Requested Status
PySkein Maintainers 2013-07-08 Pending
Review via email: mp+173392@code.launchpad.net

Description of the change

Submitting a branch for fixing #1198768.

To post a comment you must log in.

Unmerged revisions

419. By Aaron Gallagher on 2013-07-08

Add skein.Random.getrandbits and a test.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/skein.py'
2--- src/skein.py 2011-06-04 14:14:40 +0000
3+++ src/skein.py 2013-07-08 01:35:29 +0000
4@@ -98,6 +98,15 @@
5 return r * self._RECIP_BPF
6
7
8+ def getrandbits(self, n_bits):
9+ """Get some number of random bits as an integer."""
10+ if n_bits <= 0:
11+ raise ValueError('number of bits must be greater than zero')
12+ n_bytes = (n_bits + 7) // 8
13+ val = int.from_bytes(self.read(n_bytes), 'little')
14+ return val >> (n_bytes * 8 - n_bits)
15+
16+
17 def getstate(self):
18 """Return internal state; can be passed to setstate() later."""
19 return (self._state,
20
21=== modified file 'test/test_skein.py'
22--- test/test_skein.py 2010-12-22 16:49:46 +0000
23+++ test/test_skein.py 2013-07-08 01:35:29 +0000
24@@ -202,6 +202,14 @@
25 self.assertEqual(r.read(i)+r.read(5000-i),
26 skein.Random(b"abc").read(5000))
27
28+ def testPRNGgetrandbitsAdvancesState(self):
29+ r1 = skein.Random(b"123", hasher=self.HASHER)
30+ s1 = r1.read(2)
31+ r1.getrandbits(32)
32+ s2 = r1.read(2)
33+ r2 = skein.Random(b"123", hasher=self.HASHER)
34+ self.assertNotEqual(s1 + s2, r2.read(4))
35+
36 def testStreamCipher(self):
37 c = skein.StreamCipher(key=b"secret", hasher=self.HASHER)
38 x = c.encrypt(b"foobar")

Subscribers

People subscribed via source and target branches