def hexdigest(self):
return self.digest().encode('hex')
-
-if __name__ == '__main__':
- import hashlib
- import random
- for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
- print test
- print sha256(test).hexdigest()
- print hashlib.sha256(test).hexdigest()
- print
- def random_str(l):
- return ''.join(chr(random.randrange(256)) for i in xrange(l))
- for length in xrange(1500):
- test = random_str(length)
- a = sha256(test).hexdigest()
- b = hashlib.sha256(test).hexdigest()
- print length, a, b
- if a != b:
- print 'ERROR!'
- raise ValueError()
- while True:
- test = random_str(int(random.expovariate(1/1000)))
- test2 = random_str(int(random.expovariate(1/1000)))
-
- a = sha256(test)
- a = a.copy()
- a.update(test2)
- a = a.hexdigest()
-
- b = hashlib.sha256(test)
- b = b.copy()
- b.update(test2)
- b = b.hexdigest()
- print a, b
- if a != b:
- print 'ERROR!'
- raise ValueError()
--- /dev/null
+from __future__ import division
+
+import unittest
+import hashlib
+import random
+
+from p2pool.bitcoin import sha256
+
+class Test(unittest.TestCase):
+ def test_all(self):
+ for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
+ #print test
+ #print sha256.sha256(test).hexdigest()
+ #print hashlib.sha256(test).hexdigest()
+ #print
+ assert sha256.sha256(test).hexdigest() == hashlib.sha256(test).hexdigest()
+ def random_str(l):
+ return ''.join(chr(random.randrange(256)) for i in xrange(l))
+ for length in xrange(150):
+ test = random_str(length)
+ a = sha256.sha256(test).hexdigest()
+ b = hashlib.sha256(test).hexdigest()
+ #print length, a, b
+ if a != b:
+ print 'ERROR!'
+ raise ValueError()
+ for i in xrange(100):
+ test = random_str(int(random.expovariate(1/100)))
+ test2 = random_str(int(random.expovariate(1/100)))
+
+ a = sha256.sha256(test)
+ a = a.copy()
+ a.update(test2)
+ a = a.hexdigest()
+
+ b = hashlib.sha256(test)
+ b = b.copy()
+ b.update(test2)
+ b = b.hexdigest()
+ #print a, b
+ if a != b:
+ print 'ERROR!'
+ raise ValueError()