本文整理匯總了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)
示例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()
示例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())
示例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))
)
示例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)
示例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)
)
示例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()
示例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
示例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])
示例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)
示例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]
示例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])
示例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)
示例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]
示例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)