当前位置: 首页>>代码示例>>Python>>正文


Python utils.int_to_bytes方法代码示例

本文整理汇总了Python中cryptography.utils.int_to_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python utils.int_to_bytes方法的具体用法?Python utils.int_to_bytes怎么用?Python utils.int_to_bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cryptography.utils的用法示例。


在下文中一共展示了utils.int_to_bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _ecc_encode_compressed_point

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _ecc_encode_compressed_point(private_key):
    """Encodes a compressed elliptic curve point
        as described in SEC-1 v2 section 2.3.3
        http://www.secg.org/sec1-v2.pdf

    :param private_key: Private key from which to extract point data
    :type private_key: cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
    :returns: Encoded compressed elliptic curve point
    :rtype: bytes
    :raises NotSupportedError: for non-prime curves
    """
    # key_size is in bits. Convert to bytes and round up
    byte_length = (private_key.curve.key_size + 7) // 8
    public_numbers = private_key.public_key().public_numbers()
    y_map = [b"\x02", b"\x03"]
    # If curve in prime field.
    if private_key.curve.name.startswith("secp"):
        y_order = public_numbers.y % 2
        y = y_map[y_order]
    else:
        raise NotSupportedError("Non-prime curves are not supported at this time")
    return y + int_to_bytes(public_numbers.x, byte_length) 
开发者ID:aws,项目名称:aws-encryption-sdk-python,代码行数:24,代码来源:elliptic_curve.py

示例2: _key_identifier_from_public_key

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _key_identifier_from_public_key(public_key):
    # This is a very slow way to do this.
    serialized = public_key.public_bytes(
        serialization.Encoding.DER,
        serialization.PublicFormat.SubjectPublicKeyInfo
    )
    spki, remaining = decoder.decode(
        serialized, asn1Spec=_SubjectPublicKeyInfo()
    )
    assert not remaining
    # the univ.BitString object is a tuple of bits. We need bytes and
    # pyasn1 really doesn't want to give them to us. To get it we'll
    # build an integer and convert that to bytes.
    bits = 0
    for bit in spki.getComponentByName("subjectPublicKey"):
        bits = bits << 1 | bit

    data = utils.int_to_bytes(bits)
    return hashlib.sha1(data).digest() 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:21,代码来源:extensions.py

示例3: test_fromBlobECDSA

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def test_fromBlobECDSA(self):
        """
        Key.fromString generates ECDSA keys from blobs.
        """
        from cryptography import utils

        ecPublicData = {
            'x': keydata.ECDatanistp256['x'],
            'y': keydata.ECDatanistp256['y'],
            'curve': keydata.ECDatanistp256['curve']
            }

        ecblob = (common.NS(ecPublicData['curve']) +
                  common.NS(ecPublicData['curve'][-8:]) +
                  common.NS(b'\x04' +
                    utils.int_to_bytes(ecPublicData['x'],  32) +
                    utils.int_to_bytes(ecPublicData['y'],  32))
            )

        eckey = keys.Key.fromString(ecblob)
        self.assertTrue(eckey.isPublic())
        self.assertEqual(ecPublicData, eckey.data()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:24,代码来源:test_keys.py

示例4: test_blobEC

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def test_blobEC(self):
        """
        Return the over-the-wire SSH format of the EC public key.
        """
        from cryptography import utils

        byteLength = (self.ecObj.curve.key_size + 7) // 8
        self.assertEqual(
            keys.Key(self.ecObj).blob(),
            common.NS(keydata.ECDatanistp256['curve']) +
            common.NS(keydata.ECDatanistp256['curve'][-8:]) +
            common.NS(b'\x04' +
               utils.int_to_bytes(
                 self.ecObj.private_numbers().public_numbers.x, byteLength) +
                   utils.int_to_bytes(
                   self.ecObj.private_numbers().public_numbers.y, byteLength))
            ) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:19,代码来源:test_keys.py

示例5: _ssh_write_mpint

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _ssh_write_mpint(value):
    data = utils.int_to_bytes(value)
    if six.indexbytes(data, 0) & 0x80:
        data = b"\x00" + data
    return _ssh_write_string(data)


# Code from _openssh_public_key_bytes at:
# https://github.com/pyca/cryptography/tree/6b08aba7f1eb296461528328a3c9871fa7594fc4/src/cryptography/hazmat/backends/openssl#L1616
# Taken from upstream cryptography since the version we have is too old
# and doesn't have this code (yet) 
开发者ID:Pagure,项目名称:pagure,代码行数:13,代码来源:tasks_mirror.py

示例6: encode_point

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def encode_point(self):
        # key_size is in bits. Convert to bytes and round up
        byte_length = (self.curve.key_size + 7) // 8
        return (
            b'\x04' + utils.int_to_bytes(self.x, byte_length) +
            utils.int_to_bytes(self.y, byte_length)
        ) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:9,代码来源:ec.py

示例7: _key_identifier_from_public_key

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _key_identifier_from_public_key(public_key):
    if isinstance(public_key, RSAPublicKey):
        data = public_key.public_bytes(
            serialization.Encoding.DER,
            serialization.PublicFormat.PKCS1,
        )
    elif isinstance(public_key, EllipticCurvePublicKey):
        data = public_key.public_numbers().encode_point()
    else:
        # This is a very slow way to do this.
        serialized = public_key.public_bytes(
            serialization.Encoding.DER,
            serialization.PublicFormat.SubjectPublicKeyInfo
        )
        spki, remaining = decoder.decode(
            serialized, asn1Spec=_SubjectPublicKeyInfo()
        )
        assert not remaining
        # the univ.BitString object is a tuple of bits. We need bytes and
        # pyasn1 really doesn't want to give them to us. To get it we'll
        # build an integer and convert that to bytes.
        bits = 0
        for bit in spki.getComponentByName("subjectPublicKey"):
            bits = bits << 1 | bit

        data = utils.int_to_bytes(bits)

    return hashlib.sha1(data).digest() 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:30,代码来源:extensions.py

示例8: _valid_byte_length

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _valid_byte_length(self, value):
        if not isinstance(value, int):
            raise TypeError('value must be of type int')

        value_bin = utils.int_to_bytes(1, value)
        if not 1 <= len(value_bin) <= 4:
            return False
        return True 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:10,代码来源:kbkdf.py

示例9: _generate_fixed_input

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _generate_fixed_input(self):
        if self._fixed_data and isinstance(self._fixed_data, bytes):
            return self._fixed_data

        l = utils.int_to_bytes(self._length * 8, self._llen)

        return b"".join([self._label, b"\x00", self._context, l]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:9,代码来源:kbkdf.py

示例10: _ssh_write_mpint

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _ssh_write_mpint(value):
    data = utils.int_to_bytes(value)
    if six.indexbytes(data, 0) & 0x80:
        data = b"\x00" + data
    return _ssh_write_string(data) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:7,代码来源:serialization.py

示例11: derive

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def derive(self, key_material):
        if self._used:
            raise AlreadyFinalized

        if not isinstance(key_material, bytes):
            raise TypeError('key_material must be bytes')
        self._used = True

        # inverse floor division (equivalent to ceiling)
        rounds = -(-self._length // self._algorithm.digest_size)

        output = [b'']

        # For counter mode, the number of iterations shall not be
        # larger than 2^r-1, where r <= 32 is the binary length of the counter
        # This ensures that the counter values used as an input to the
        # PRF will not repeat during a particular call to the KDF function.
        r_bin = utils.int_to_bytes(1, self._rlen)
        if rounds > pow(2, len(r_bin) * 8) - 1:
            raise ValueError('There are too many iterations.')

        for i in range(1, rounds + 1):
            h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)

            counter = utils.int_to_bytes(i, self._rlen)
            if self._location == CounterLocation.BeforeFixed:
                h.update(counter)

            h.update(self._generate_fixed_input())

            if self._location == CounterLocation.AfterFixed:
                h.update(counter)

            output.append(h.finalize())

        return b''.join(output)[:self._length] 
开发者ID:tp4a,项目名称:teleport,代码行数:38,代码来源:kbkdf.py

示例12: _generate_fixed_input

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def _generate_fixed_input(self):
        if self._fixed_data and isinstance(self._fixed_data, bytes):
            return self._fixed_data

        l_val = utils.int_to_bytes(self._length * 8, self._llen)

        return b"".join([self._label, b"\x00", self._context, l_val]) 
开发者ID:tp4a,项目名称:teleport,代码行数:9,代码来源:kbkdf.py

示例13: encode_der

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def encode_der(tag, *children):
    length = 0
    for child in children:
        length += len(child)
    chunks = [six.int2byte(tag)]
    if length < 0x80:
        chunks.append(six.int2byte(length))
    else:
        length_bytes = int_to_bytes(length)
        chunks.append(six.int2byte(0x80 | len(length_bytes)))
        chunks.append(length_bytes)
    chunks.extend(children)
    return b"".join(chunks) 
开发者ID:tp4a,项目名称:teleport,代码行数:15,代码来源:_der.py

示例14: derive

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def derive(self, key_material):
        if self._used:
            raise AlreadyFinalized

        utils._check_byteslike("key_material", key_material)
        self._used = True

        # inverse floor division (equivalent to ceiling)
        rounds = -(-self._length // self._algorithm.digest_size)

        output = [b'']

        # For counter mode, the number of iterations shall not be
        # larger than 2^r-1, where r <= 32 is the binary length of the counter
        # This ensures that the counter values used as an input to the
        # PRF will not repeat during a particular call to the KDF function.
        r_bin = utils.int_to_bytes(1, self._rlen)
        if rounds > pow(2, len(r_bin) * 8) - 1:
            raise ValueError('There are too many iterations.')

        for i in range(1, rounds + 1):
            h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)

            counter = utils.int_to_bytes(i, self._rlen)
            if self._location == CounterLocation.BeforeFixed:
                h.update(counter)

            h.update(self._generate_fixed_input())

            if self._location == CounterLocation.AfterFixed:
                h.update(counter)

            output.append(h.finalize())

        return b''.join(output)[:self._length] 
开发者ID:tp4a,项目名称:teleport,代码行数:37,代码来源:kbkdf.py

示例15: encode_der_integer

# 需要导入模块: from cryptography import utils [as 别名]
# 或者: from cryptography.utils import int_to_bytes [as 别名]
def encode_der_integer(x):
    if not isinstance(x, six.integer_types):
        raise ValueError("Value must be an integer")
    if x < 0:
        raise ValueError("Negative integers are not supported")
    n = x.bit_length() // 8 + 1
    return int_to_bytes(x, n) 
开发者ID:tp4a,项目名称:teleport,代码行数:9,代码来源:_der.py


注:本文中的cryptography.utils.int_to_bytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。