Python cryptography.exceptions 模块,AlreadyFinalized() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cryptography.exceptions.AlreadyFinalized()

项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = max(
            len(self._buffer) // (self.block_size // 8) - 1,
            0
        )

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = max(
            len(self._buffer) // (self.block_size // 8) - 1,
            0
        )

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = max(
            len(self._buffer) // (self.block_size // 8) - 1,
            0
        )

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if len(self._buffer) != self.block_size // 8:
            raise ValueError("Invalid padding bytes.")

        valid = lib.Cryptography_check_pkcs7_padding(
            self._buffer, self.block_size // 8
        )

        if not valid:
            raise ValueError("Invalid padding bytes.")

        pad_size = six.indexbytes(self._buffer, -1)
        res = self._buffer[:-pad_size]
        self._buffer = None
        return res
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = max(
            len(self._buffer) // (self.block_size // 8) - 1,
            0
        )

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:Docker-XX-Net    作者:kuanghy    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = max(
            len(self._buffer) // (self.block_size // 8) - 1,
            0
        )

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:Docker-XX-Net    作者:kuanghy    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if len(self._buffer) != self.block_size // 8:
            raise ValueError("Invalid padding bytes.")

        valid = _lib.Cryptography_check_pkcs7_padding(
            self._buffer, self.block_size // 8
        )

        if not valid:
            raise ValueError("Invalid padding bytes.")

        pad_size = six.indexbytes(self._buffer, -1)
        res = self._buffer[:-pad_size]
        self._buffer = None
        return res
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_padding_pad(buffer_, block_size, paddingfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    pad_size = block_size // 8 - len(buffer_)
    return buffer_ + paddingfn(pad_size)
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def _byte_padding_pad(buffer_, block_size, paddingfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    pad_size = block_size // 8 - len(buffer_)
    return buffer_ + paddingfn(pad_size)
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_padding_pad(buffer_, block_size, paddingfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    pad_size = block_size // 8 - len(buffer_)
    return buffer_ + paddingfn(pad_size)
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = len(self._buffer) // (self.block_size // 8)

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:Intranet-Penetration    作者:yuxiaokui    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        pad_size = self.block_size // 8 - len(self._buffer)
        result = self._buffer + six.int2byte(pad_size) * pad_size
        self._buffer = None
        return result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_padding_pad(buffer_, block_size, paddingfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    pad_size = block_size // 8 - len(buffer_)
    return buffer_ + paddingfn(pad_size)
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = len(self._buffer) // (self.block_size // 8)

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:MKFQ    作者:maojingios    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        pad_size = self.block_size // 8 - len(self._buffer)
        result = self._buffer + six.int2byte(pad_size) * pad_size
        self._buffer = None
        return result
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = len(self._buffer) // (self.block_size // 8)

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:OneClickDTU    作者:satwikkansal    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        pad_size = self.block_size // 8 - len(self._buffer)
        result = self._buffer + six.int2byte(pad_size) * pad_size
        self._buffer = None
        return result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_padding_pad(buffer_, block_size, paddingfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    pad_size = block_size // 8 - len(buffer_)
    return buffer_ + paddingfn(pad_size)
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_padding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = len(buffer_) // (block_size // 8)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_unpadding_update(buffer_, data, block_size):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if not isinstance(data, bytes):
        raise TypeError("data must be bytes.")

    buffer_ += data

    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

    result = buffer_[:finished_blocks * (block_size // 8)]
    buffer_ = buffer_[finished_blocks * (block_size // 8):]

    return buffer_, result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def _byte_unpadding_check(buffer_, block_size, checkfn):
    if buffer_ is None:
        raise AlreadyFinalized("Context was already finalized.")

    if len(buffer_) != block_size // 8:
        raise ValueError("Invalid padding bytes.")

    valid = checkfn(buffer_, block_size // 8)

    if not valid:
        raise ValueError("Invalid padding bytes.")

    pad_size = six.indexbytes(buffer_, -1)
    return buffer_[:-pad_size]
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def update(self, data):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")

        self._buffer += data

        finished_blocks = len(self._buffer) // (self.block_size // 8)

        result = self._buffer[:finished_blocks * (self.block_size // 8)]
        self._buffer = self._buffer[finished_blocks * (self.block_size // 8):]

        return result
项目:xxNet    作者:drzorm    | 项目源码 | 文件源码
def finalize(self):
        if self._buffer is None:
            raise AlreadyFinalized("Context was already finalized.")

        pad_size = self.block_size // 8 - len(self._buffer)
        result = self._buffer + six.int2byte(pad_size) * pad_size
        self._buffer = None
        return result
项目:python-otr    作者:AGProjects    | 项目源码 | 文件源码
def update(self, data):
        if self._ctx is None:
            raise AlreadyFinalized("Context was already finalized.")
        if not isinstance(data, bytes):
            raise TypeError("data must be bytes.")
        self._ctx.update(data)
项目:python-otr    作者:AGProjects    | 项目源码 | 文件源码
def copy(self):
        if self._ctx is None:
            raise AlreadyFinalized("Context was already finalized.")
        return DSASignatureHashContext(self._mac_key, dsa_key=self._dsa_key, ctx=self._ctx.copy())
项目:python-otr    作者:AGProjects    | 项目源码 | 文件源码
def finalize(self):
        if self._ctx is None:
            raise AlreadyFinalized("Context was already finalized.")
        digest = self._ctx.finalize()
        self._ctx = None
        q = self._dsa_key.parameters.parameter_numbers().q
        # We need this for compatibility with libotr which doesn't truncate its digest to the leftmost q.bit_length() bits
        # when the digest is longer than that as per the DSA specification (see FIPS 186-4, 4.2 & 4.6). Passing digest mod q
        # is the same as passing it unmodified, but this way we avoid the cryptography library truncating the digest as per
        # the specification, which would result in the signature verification failing.
        if self.algorithm.digest_size * 8 > q.bit_length():
            digest = long_to_bytes(bytes_to_long(digest) % q, (q.bit_length() + 7) // 8)
        return digest