| 549 | self.check_file_digest(name, data, hexdigest) |
| 550 | |
| 551 | def check_file_digest(self, name, data, hexdigest): |
| 552 | hexdigest = hexdigest.lower() |
| 553 | digests = [] |
| 554 | for digest in [name, *self.constructors_to_test[name]]: |
| 555 | try: |
| 556 | if callable(digest): |
| 557 | digest(b"") |
| 558 | else: |
| 559 | hashlib.new(digest) |
| 560 | except ValueError: |
| 561 | # skip, algorithm is blocked by security policy. |
| 562 | continue |
| 563 | digests.append(digest) |
| 564 | |
| 565 | with tempfile.TemporaryFile() as f: |
| 566 | f.write(data) |
| 567 | |
| 568 | for digest in digests: |
| 569 | buf = io.BytesIO(data) |
| 570 | buf.seek(0) |
| 571 | self.assertEqual( |
| 572 | hashlib.file_digest(buf, digest).hexdigest(), hexdigest |
| 573 | ) |
| 574 | f.seek(0) |
| 575 | digestobj = hashlib.file_digest(f, digest) |
| 576 | self.assertEqual(digestobj.hexdigest(), hexdigest) |
| 577 | |
| 578 | def check_no_unicode(self, algorithm_name): |
| 579 | # Unicode objects are not allowed as input. |