本文整理汇总了Python中OpenSSL.crypto.sign方法的典型用法代码示例。如果您正苦于以下问题:Python crypto.sign方法的具体用法?Python crypto.sign怎么用?Python crypto.sign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenSSL.crypto
的用法示例。
在下文中一共展示了crypto.sign方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _sign
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def _sign(private_key, data, digest=SHA256):
'''
An internal helper method to sign the 'data' with the 'private_key'.
@type private_key: C{str}
@param private_key: The private key used to sign the 'data', in one of
supported formats.
@type data: C{str}
@param data: The data that needs to be signed.
@type digest: C{str}
@param digest: Digest is a str naming a supported message digest type,
for example 'sha256'.
@rtype: C{str}
@return: Signed string.
'''
# Convert private key in arbitrary format into DER (DER is binary format
# so we get rid of \n / \r\n differences, and line breaks in PEM).
pkey = _load_private_key(_extract_certificate(private_key))
return base64.b64encode(crypto.sign(pkey, data, digest))
示例2: _x509Prep
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def _x509Prep(self, rootApi, req, data):
if rootApi._x509Key is None:
return
payload = '{}{}'.format(req.method, req.url.replace(rootApi._url, ''))
payload = unquote(payload)
if data is not None:
payload += data
signature = base64.b64encode(sign(rootApi._x509Key, payload,
'sha256'))
if sys.version_info[0] >= 3:
signature = signature.decode('ascii')
cookie = ('APIC-Request-Signature={}; '
'APIC-Certificate-Algorithm=v1.0; '
'APIC-Certificate-Fingerprint=fingerprint; '
'APIC-Certificate-DN={}').format(
signature, rootApi._x509Dn)
req.headers['Cookie'] = cookie
示例3: __init__
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def __init__(self, pkey):
"""Constructor.
Args:
pkey, OpenSSL.crypto.PKey (or equiv), The private key to sign with.
"""
self._key = pkey
示例4: sign
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def sign(self, message):
"""Signs a message.
Args:
message: string, Message to be signed.
Returns:
string, The signature of the message for the given key.
"""
return crypto.sign(self._key, message, 'sha256')
示例5: make_signed_jwt
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def make_signed_jwt(signer, payload):
"""Make a signed JWT.
See http://self-issued.info/docs/draft-jones-json-web-token.html.
Args:
signer: crypt.Signer, Cryptographic signer.
payload: dict, Dictionary of data to convert to JSON and then sign.
Returns:
string, The JWT for the payload.
"""
header = {'typ': 'JWT', 'alg': 'RS256'}
segments = [
_urlsafe_b64encode(_json_encode(header)),
_urlsafe_b64encode(_json_encode(payload)),
]
signing_input = '.'.join(segments)
signature = signer.sign(signing_input)
segments.append(_urlsafe_b64encode(signature))
logger.debug(str(segments))
return '.'.join(segments)
示例6: make_signed_jwt
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def make_signed_jwt(signer, payload):
"""Make a signed JWT.
See http://self-issued.info/docs/draft-jones-json-web-token.html.
Args:
signer: crypt.Signer, Cryptographic signer.
payload: dict, Dictionary of data to convert to JSON and then sign.
Returns:
string, The JWT for the payload.
"""
header = {'typ': 'JWT', 'alg': 'RS256'}
segments = [
_urlsafe_b64encode(_json_encode(header)),
_urlsafe_b64encode(_json_encode(payload)),
]
signing_input = '.'.join(segments)
signature = signer.sign(signing_input)
segments.append(_urlsafe_b64encode(signature))
logger.debug(str(segments))
return '.'.join(segments)
示例7: _sign
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def _sign(self, content, digest='SHA256'):
pkey = self.connector.p12.get_privatekey()
return crypto.sign(pkey=pkey, data=content, digest=digest)
示例8: test_success
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def test_success(self):
decrypted_key = _mtls_helper.decrypt_private_key(
ENCRYPTED_EC_PRIVATE_KEY, PASSPHRASE_VALUE
)
private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, decrypted_key)
public_key = crypto.load_publickey(crypto.FILETYPE_PEM, EC_PUBLIC_KEY)
x509 = crypto.X509()
x509.set_pubkey(public_key)
# Test the decrypted key works by signing and verification.
signature = crypto.sign(private_key, b"data", "sha256")
crypto.verify(x509, signature, b"data", "sha256")
示例9: __init__
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def __init__(self, pkey):
"""Constructor.
Args:
pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
"""
self._key = pkey
示例10: sign
# 需要导入模块: from OpenSSL import crypto [as 别名]
# 或者: from OpenSSL.crypto import sign [as 别名]
def sign(self, message):
"""Signs a message.
Args:
message: bytes, Message to be signed.
Returns:
string, The signature of the message for the given key.
"""
message = _to_bytes(message, encoding='utf-8')
return crypto.sign(self._key, message, 'sha256')