本文整理匯總了Python中cryptography.hazmat.bindings._openssl.ffi.buffer方法的典型用法代碼示例。如果您正苦於以下問題:Python ffi.buffer方法的具體用法?Python ffi.buffer怎麽用?Python ffi.buffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cryptography.hazmat.bindings._openssl.ffi
的用法示例。
在下文中一共展示了ffi.buffer方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _bn_to_int
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def _bn_to_int(self, bn):
assert bn != self._ffi.NULL
if six.PY3:
# Python 3 has constant time from_bytes, so use that.
bn_num_bytes = self._lib.BN_num_bytes(bn)
bin_ptr = self._ffi.new("unsigned char[]", bn_num_bytes)
bin_len = self._lib.BN_bn2bin(bn, bin_ptr)
# A zero length means the BN has value 0
self.openssl_assert(bin_len >= 0)
return int.from_bytes(self._ffi.buffer(bin_ptr)[:bin_len], "big")
else:
# Under Python 2 the best we can do is hex()
hex_cdata = self._lib.BN_bn2hex(bn)
self.openssl_assert(hex_cdata != self._ffi.NULL)
hex_str = self._ffi.string(hex_cdata)
self._lib.OPENSSL_free(hex_cdata)
return int(hex_str, 16)
示例2: derive_pbkdf2_hmac
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations,
key_material):
buf = self._ffi.new("unsigned char[]", length)
evp_md = self._lib.EVP_get_digestbyname(
algorithm.name.encode("ascii"))
self.openssl_assert(evp_md != self._ffi.NULL)
res = self._lib.PKCS5_PBKDF2_HMAC(
key_material,
len(key_material),
salt,
len(salt),
iterations,
evp_md,
length,
buf
)
self.openssl_assert(res == 1)
return self._ffi.buffer(buf)[:]
示例3: _bn_to_int
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def _bn_to_int(self, bn):
assert bn != self._ffi.NULL
if six.PY3:
# Python 3 has constant time from_bytes, so use that.
bn_num_bytes = self._lib.BN_num_bytes(bn)
bin_ptr = self._ffi.new("unsigned char[]", bn_num_bytes)
bin_len = self._lib.BN_bn2bin(bn, bin_ptr)
# A zero length means the BN has value 0
self.openssl_assert(bin_len >= 0)
return int.from_bytes(self._ffi.buffer(bin_ptr)[:bin_len], "big")
else:
# Under Python 2 the best we can do is hex()
hex_cdata = self._lib.BN_bn2hex(bn)
self.openssl_assert(hex_cdata != self._ffi.NULL)
hex_str = self._ffi.string(hex_cdata)
self._lib.OPENSSL_free(hex_cdata)
return int(hex_str, 16)
示例4: _pem_password_cb
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def _pem_password_cb(buf, size, writing, userdata_handle):
"""
A pem_password_cb function pointer that copied the password to
OpenSSL as required and returns the number of bytes copied.
typedef int pem_password_cb(char *buf, int size,
int rwflag, void *userdata);
Useful for decrypting PKCS8 files and so on.
The userdata pointer must point to a cffi handle of a
_PasswordUserdata instance.
"""
ud = _ffi.from_handle(userdata_handle)
ud.called += 1
if not ud.password:
ud.exception = TypeError(
"Password was not given but private key is encrypted."
)
return -1
elif len(ud.password) < size:
pw_buf = _ffi.buffer(buf, size)
pw_buf[:len(ud.password)] = ud.password
return len(ud.password)
else:
ud.exception = ValueError(
"Passwords longer than {0} bytes are not supported "
"by this backend.".format(size - 1)
)
return 0
示例5: derive_pbkdf2_hmac
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def derive_pbkdf2_hmac(self, algorithm, length, salt, iterations,
key_material):
buf = self._ffi.new("char[]", length)
if self._lib.Cryptography_HAS_PBKDF2_HMAC:
evp_md = self._lib.EVP_get_digestbyname(
algorithm.name.encode("ascii"))
self.openssl_assert(evp_md != self._ffi.NULL)
res = self._lib.PKCS5_PBKDF2_HMAC(
key_material,
len(key_material),
salt,
len(salt),
iterations,
evp_md,
length,
buf
)
self.openssl_assert(res == 1)
else:
if not isinstance(algorithm, hashes.SHA1):
raise UnsupportedAlgorithm(
"This version of OpenSSL only supports PBKDF2HMAC with "
"SHA1.",
_Reasons.UNSUPPORTED_HASH
)
res = self._lib.PKCS5_PBKDF2_HMAC_SHA1(
key_material,
len(key_material),
salt,
len(salt),
iterations,
length,
buf
)
self.openssl_assert(res == 1)
return self._ffi.buffer(buf)[:]
示例6: _read_mem_bio
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def _read_mem_bio(self, bio):
"""
Reads a memory BIO. This only works on memory BIOs.
"""
buf = self._ffi.new("char **")
buf_len = self._lib.BIO_get_mem_data(bio, buf)
self.openssl_assert(buf_len > 0)
self.openssl_assert(buf[0] != self._ffi.NULL)
bio_data = self._ffi.buffer(buf[0], buf_len)[:]
return bio_data
示例7: x509_name_bytes
# 需要導入模塊: from cryptography.hazmat.bindings._openssl import ffi [as 別名]
# 或者: from cryptography.hazmat.bindings._openssl.ffi import buffer [as 別名]
def x509_name_bytes(self, name):
x509_name = _encode_name_gc(self, name)
pp = self._ffi.new("unsigned char **")
res = self._lib.i2d_X509_NAME(x509_name, pp)
self.openssl_assert(pp[0] != self._ffi.NULL)
pp = self._ffi.gc(
pp, lambda pointer: self._lib.OPENSSL_free(pointer[0])
)
self.openssl_assert(res > 0)
return self._ffi.buffer(pp[0], res)[:]