コード例 #1
0
    def __init__(self,
                 stream,
                 limit,
                 digest_checker,
                 record_type=None,
                 payload_digest=None,
                 block_digest=None,
                 segment_number=None):

        super(DigestVerifyingReader, self).__init__(stream, limit)

        self.digest_checker = digest_checker

        if record_type == 'revisit':
            block_digest = None
            payload_digest = None
        if segment_number is not None:  #pragma: no cover
            payload_digest = None

        self.payload_digest = payload_digest
        self.block_digest = block_digest

        self.payload_digester = None
        self.payload_digester_obj = None
        self.block_digester = None

        if block_digest:
            try:
                algo, _ = _parse_digest(block_digest)
                self.block_digester = Digester(algo)
            except ValueError:
                self.digest_checker.problem(
                    'unknown hash algorithm name in block digest')
                self.block_digester = None
        if payload_digest:
            try:
                algo, _ = _parse_digest(self.payload_digest)
                self.payload_digester_obj = Digester(algo)
            except ValueError:
                self.digest_checker.problem(
                    'unknown hash algorithm name in payload digest')
                self.payload_digester_obj = None
コード例 #2
0
def test_compare_digest_rfc_3548():
    assert _compare_digest_rfc_3548(None, None) is None
    sha1d = Digester('sha1')
    assert _compare_digest_rfc_3548(sha1d, 'sha1:' + empty_sha1_b32) is True
    assert _compare_digest_rfc_3548(
        sha1d, 'sha1:' + empty_sha1_b32.replace('3I', 'xx')) is False

    assert _compare_digest_rfc_3548(sha1d, 'sha1:' + empty_sha1_b64) is True
    assert _compare_digest_rfc_3548(sha1d,
                                    'sha1:' + empty_sha1_b64_alt) is True
    assert _compare_digest_rfc_3548(sha1d, 'sha1:' + empty_sha1_b16) is True
    assert _compare_digest_rfc_3548(sha1d,
                                    'sha1:' + empty_sha1_b16.lower()) is True

    with pytest.raises(ValueError):
        assert _compare_digest_rfc_3548(sha1d, 'foo') is False
        assert _compare_digest_rfc_3548(sha1d, 'foo:bar') is False
コード例 #3
0
ファイル: recordbuilder.py プロジェクト: wumpus/warcio
 def _create_digester():
     return Digester('sha1')
コード例 #4
0
 def _create_digester(self):
     return Digester(self.digest_algorithm)
コード例 #5
0
class DigestVerifyingReader(LimitReader):
    """
    A reader which verifies the digest of the wrapped reader
    """
    def __init__(self,
                 stream,
                 limit,
                 digest_checker,
                 record_type=None,
                 payload_digest=None,
                 block_digest=None,
                 segment_number=None):

        super(DigestVerifyingReader, self).__init__(stream, limit)

        self.digest_checker = digest_checker

        if record_type == 'revisit':
            block_digest = None
            payload_digest = None
        if segment_number is not None:  #pragma: no cover
            payload_digest = None

        self.payload_digest = payload_digest
        self.block_digest = block_digest

        self.payload_digester = None
        self.payload_digester_obj = None
        self.block_digester = None

        if block_digest:
            try:
                algo, _ = _parse_digest(block_digest)
                self.block_digester = Digester(algo)
            except ValueError:
                self.digest_checker.problem(
                    'unknown hash algorithm name in block digest')
                self.block_digester = None
        if payload_digest:
            try:
                algo, _ = _parse_digest(self.payload_digest)
                self.payload_digester_obj = Digester(algo)
            except ValueError:
                self.digest_checker.problem(
                    'unknown hash algorithm name in payload digest')
                self.payload_digester_obj = None

    def begin_payload(self):
        self.payload_digester = self.payload_digester_obj
        if self.limit == 0:
            check = _compare_digest_rfc_3548(self.payload_digester,
                                             self.payload_digest)
            if check is False:
                self.digest_checker.problem('payload digest failed: {}'.format(
                    self.payload_digest))
                self.payload_digester = None  # prevent double-fire
            elif check is True and self.digest_checker.passed is not False:
                self.digest_checker.passed = True

    def _update(self, buff):
        super(DigestVerifyingReader, self)._update(buff)

        if self.payload_digester:
            self.payload_digester.update(buff)
        if self.block_digester:
            self.block_digester.update(buff)

        if self.limit == 0:
            check = _compare_digest_rfc_3548(self.block_digester,
                                             self.block_digest)
            if check is False:
                self.digest_checker.problem('block digest failed: {}'.format(
                    self.block_digest))
            elif check is True and self.digest_checker.passed is not False:
                self.digest_checker.passed = True
            check = _compare_digest_rfc_3548(self.payload_digester,
                                             self.payload_digest)
            if check is False:
                self.digest_checker.problem('payload digest failed {}'.format(
                    self.payload_digest))
            elif check is True and self.digest_checker.passed is not False:
                self.digest_checker.passed = True

        return buff