def check_file_digest(self, name, data, hexdigest): hexdigest = hexdigest.lower() try: hashlib.new(name) except ValueError: # skip, algorithm is blocked by security policy. return digests = [name] digests.extend(self.constructors_to_test[name]) with open(os_helper.TESTFN, "wb") as f: f.write(data) try: for digest in digests: buf = io.BytesIO(data) buf.seek(0) self.assertEqual( hashlib.file_digest(buf, digest).hexdigest(), hexdigest ) with open(os_helper.TESTFN, "rb") as f: digestobj = hashlib.file_digest(f, digest) self.assertEqual(digestobj.hexdigest(), hexdigest) finally: os.unlink(os_helper.TESTFN)
def test_file_digest(self): data = b'a' * 65536 d1 = hashlib.sha256() self.addCleanup(os.unlink, os_helper.TESTFN) with open(os_helper.TESTFN, "wb") as f: for _ in range(10): d1.update(data) f.write(data) with open(os_helper.TESTFN, "rb") as f: d2 = hashlib.file_digest(f, hashlib.sha256) self.assertEqual(d1.hexdigest(), d2.hexdigest()) self.assertEqual(d1.name, d2.name) self.assertIs(type(d1), type(d2)) with self.assertRaises(ValueError): hashlib.file_digest(None, "sha256") with self.assertRaises(ValueError): with open(os_helper.TESTFN, "r") as f: hashlib.file_digest(f, "sha256") with self.assertRaises(ValueError): with open(os_helper.TESTFN, "wb") as f: hashlib.file_digest(f, "sha256")
def check_file_digest(self, name, data, hexdigest): hexdigest = hexdigest.lower() digests = [name] digests.extend(self.constructors_to_test[name]) with open(os_helper.TESTFN, "wb") as f: f.write(data) try: for digest in digests: buf = io.BytesIO(data) buf.seek(0) self.assertEqual( hashlib.file_digest(buf, digest).hexdigest(), hexdigest) with open(os_helper.TESTFN, "rb") as f: digestobj = hashlib.file_digest(f, digest) self.assertEqual(digestobj.hexdigest(), hexdigest) finally: os.unlink(os_helper.TESTFN)