本文整理汇总了Python中rsa.sign方法的典型用法代码示例。如果您正苦于以下问题:Python rsa.sign方法的具体用法?Python rsa.sign怎么用?Python rsa.sign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rsa
的用法示例。
在下文中一共展示了rsa.sign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_main
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def _run_main(self, args, parsed_globals):
signer = CloudFrontSigner(
args.key_pair_id, RSASigner(args.private_key).sign)
date_less_than = parse_to_aware_datetime(args.date_less_than)
date_greater_than = args.date_greater_than
if date_greater_than is not None:
date_greater_than = parse_to_aware_datetime(date_greater_than)
if date_greater_than is not None or args.ip_address is not None:
policy = signer.build_policy(
args.url, date_less_than, date_greater_than=date_greater_than,
ip_address=args.ip_address)
sys.stdout.write(signer.generate_presigned_url(
args.url, policy=policy))
else:
sys.stdout.write(signer.generate_presigned_url(
args.url, date_less_than=date_less_than))
return 0
示例2: _use_cryptography_signer
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def _use_cryptography_signer():
# https://cryptography.io as an RSA backend
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import (
load_pem_private_key
)
def _cloud_front_signer_from_pem(key_id, pem):
key = load_pem_private_key(
pem, password=None, backend=default_backend())
return CloudFrontSigner(
key_id, lambda x: key.sign(x, padding.PKCS1v15(), hashes.SHA1()))
return _cloud_front_signer_from_pem
示例3: _strip_signing_parameters
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def _strip_signing_parameters(self, url):
# Boto3 does not currently support generating URLs that are unsigned. Instead we
# take the signed URLs and strip any querystring params related to signing and expiration.
# Note that this may end up with URLs that are still invalid, especially if params are
# passed in that only work with signed URLs, e.g. response header params.
# The code attempts to strip all query parameters that match names of known parameters
# from v2 and v4 signatures, regardless of the actual signature version used.
split_url = urlsplit(url)
qs = parse_qsl(split_url.query, keep_blank_values=True)
blacklist = {
'x-amz-algorithm', 'x-amz-credential', 'x-amz-date',
'x-amz-expires', 'x-amz-signedheaders', 'x-amz-signature',
'x-amz-security-token', 'awsaccesskeyid', 'expires', 'signature',
}
filtered_qs = ((key, val) for key, val in qs if key.lower() not in blacklist)
# Note: Parameters that did not have a value in the original query string will have
# an '=' sign appended to it, e.g ?foo&bar becomes ?foo=&bar=
joined_qs = ('='.join(keyval) for keyval in filtered_qs)
split_url = split_url._replace(query="&".join(joined_qs))
return split_url.geturl()
示例4: verify_ali_data
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def verify_ali_data(valueDict):
logger.info('verifying data from ali')
sign = valueDict['sign']
# remove sign and sign_type
del valueDict['sign']
if 'sign_type' in valueDict:
del valueDict['sign_type']
# contact string need to verify
temp = []
for key in sorted(valueDict):
if not valueDict[key]:
continue
temp.append('{}={}'.format(key, valueDict[key]))
tempStr = '&'.join(temp)
logger.info('string to verify:{}'.format(tempStr))
return verify(tempStr, sign, settings.ALIPAY['ali_public_key_pem'])
示例5: Creat_Return_Token
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def Creat_Return_Token(self, token_crypto):
tag = bytes("NQZ",encoding="utf8")
# with open('../project/Helper/pubkey.pem','r') as f:
# pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('../project/Helper/privkey.pem','r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
token_message = token_crypto
# token_crypto = rsa.encrypt(token_message.encode(), pubkey)
# 不进行公钥加密
# 直接反馈加上标准内容的信息
token_crypto = bytes(token_crypto, encoding='utf8') + tag
signature = rsa.sign(token_message.encode(), privkey, 'SHA-1')
print("token message encode = ", token_message.encode())
# 利用私钥对信息进行签名
signature = base64.encodestring(signature)
return (token_crypto, signature)
# 返回生成的token 和 sign 签名值
示例6: sign
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def sign(self, message, priv_key=None, hash_method='SHA-1'):
"""
生成明文的哈希签名以便还原后对照
:param message: str
:param priv_key:
:param hash_method: 哈希的模式
:return:
"""
if None == priv_key:
priv_key = self.privkey
return rsa.sign(message.encode(), priv_key, hash_method)
示例7: sign_input_utxo
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def sign_input_utxo(input_utxo_id, privkey):
return rsa.sign(input_utxo_id, privkey, 'SHA-1').encode('hex')
示例8: signSRCTokenUtxoPayload
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def signSRCTokenUtxoPayload(self, payload):
return rsa.sign(payload, self.privkey, 'SHA-1').encode('hex')
示例9: handler
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def handler(self, operation_name=None, request=None, **kwargs):
# This is typically hooked up to the "request-created" event
# from a client's event emitter. When a new request is created
# this method is invoked to sign the request.
# Don't call this method directly.
return self.sign(operation_name, request)
示例10: _choose_signer
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def _choose_signer(self, operation_name, signing_type):
"""
Allow setting the signature version via the choose-signer event.
A value of `botocore.UNSIGNED` means no signing will be performed.
:param operation_name: The operation to sign.
:param signing_type: The type of signing that the signer is to be used
for.
:return: The signature version to sign with.
"""
signing_type_suffix_map = {
'presign-post': '-presign-post',
'presign-url': '-query'
}
suffix = signing_type_suffix_map.get(signing_type, '')
signature_version = self._signature_version
if signature_version is not botocore.UNSIGNED and not \
signature_version.endswith(suffix):
signature_version += suffix
handler, response = self._event_emitter.emit_until_response(
'choose-signer.{0}.{1}'.format(self._service_name, operation_name),
signing_name=self._signing_name, region_name=self._region_name,
signature_version=signature_version)
if response is not None:
signature_version = response
# The suffix needs to be checked again in case we get an improper
# signature version from choose-signer.
if signature_version is not botocore.UNSIGNED and not \
signature_version.endswith(suffix):
signature_version += suffix
return signature_version
示例11: generate_presigned_url
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def generate_presigned_url(self, request_dict, operation_name,
expires_in=3600, region_name=None):
"""Generates a presigned url
:type request_dict: dict
:param request_dict: The prepared request dictionary returned by
``botocore.awsrequest.prepare_request_dict()``
:type operation_name: str
:param operation_name: The operation being signed.
:type expires_in: int
:param expires_in: The number of seconds the presigned url is valid
for. By default it expires in an hour (3600 seconds)
:type region_name: string
:param region_name: The region name to sign the presigned url.
:returns: The presigned url
"""
request = create_request_object(request_dict)
self.sign(operation_name, request, region_name,
'presign-url', expires_in)
request.prepare()
return request.url
示例12: pack_license_key
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def pack_license_key(data, privkey_args):
"""
Pack a dictionary of license key data to a string. You typically call this
function on a server, when a user purchases a license. Eg.:
lk_contents = pack_license_key({'email': 'some@user.com'}, ...)
The parameter `privkey_args` is a dictionary containing values for the RSA
fields "n", "e", "d", "p" and "q". You can generate it with fbs's command
`init_licensing`.
The resulting string is signed to prevent the end user from changing it.
Use the function `unpack_license_key` below to reconstruct `data` from it.
This also verifies that the string was not tampered with.
This function has two non-obvious caveats:
1) It does not obfuscate the data. If `data` contains "key": "value", then
"key": "value" is also visible in the resulting string.
2) Calling this function twice with the same arguments will result in the
same string. This may be undesirable when you generate multiple license keys
for the same user. A simple workaround for this is to add a unique parameter
to `data`, such as the current timestamp.
"""
data_bytes = _dumpb(data)
signature = rsa.sign(data_bytes, PrivateKey(**privkey_args), 'SHA-1')
result = dict(data)
if 'key' in data:
raise ValueError('Data must not contain an element called "key"')
result['key'] = b64encode(signature).decode('ascii')
return json.dumps(result)
示例13: _choose_signer
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def _choose_signer(self, operation_name, signing_type, context):
"""
Allow setting the signature version via the choose-signer event.
A value of `botocore.UNSIGNED` means no signing will be performed.
:param operation_name: The operation to sign.
:param signing_type: The type of signing that the signer is to be used
for.
:return: The signature version to sign with.
"""
signing_type_suffix_map = {
'presign-post': '-presign-post',
'presign-url': '-query'
}
suffix = signing_type_suffix_map.get(signing_type, '')
signature_version = self._signature_version
if signature_version is not botocore.UNSIGNED and not \
signature_version.endswith(suffix):
signature_version += suffix
handler, response = self._event_emitter.emit_until_response(
'choose-signer.{0}.{1}'.format(
self._service_id.hyphenize(), operation_name),
signing_name=self._signing_name, region_name=self._region_name,
signature_version=signature_version, context=context)
if response is not None:
signature_version = response
# The suffix needs to be checked again in case we get an improper
# signature version from choose-signer.
if signature_version is not botocore.UNSIGNED and not \
signature_version.endswith(suffix):
signature_version += suffix
return signature_version
示例14: generate_presigned_url
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def generate_presigned_url(self, request_dict, operation_name,
expires_in=3600, region_name=None,
signing_name=None):
"""Generates a presigned url
:type request_dict: dict
:param request_dict: The prepared request dictionary returned by
``botocore.awsrequest.prepare_request_dict()``
:type operation_name: str
:param operation_name: The operation being signed.
:type expires_in: int
:param expires_in: The number of seconds the presigned url is valid
for. By default it expires in an hour (3600 seconds)
:type region_name: string
:param region_name: The region name to sign the presigned url.
:type signing_name: str
:param signing_name: The name to use for the service when signing.
:returns: The presigned url
"""
request = create_request_object(request_dict)
self.sign(operation_name, request, region_name,
'presign-url', expires_in, signing_name)
request.prepare()
return request.url
示例15: perform_operation
# 需要导入模块: import rsa [as 别名]
# 或者: from rsa import sign [as 别名]
def perform_operation(self, indata, priv_key, cli_args):
"""Signs files."""
hash_method = cli_args[1]
if hash_method not in HASH_METHODS:
raise SystemExit('Invalid hash method, choose one of %s' %
', '.join(HASH_METHODS))
return rsa.sign(indata, priv_key, hash_method)