本文整理汇总了Python中ecdsa.SigningKey.from_pem方法的典型用法代码示例。如果您正苦于以下问题:Python SigningKey.from_pem方法的具体用法?Python SigningKey.from_pem怎么用?Python SigningKey.from_pem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ecdsa.SigningKey
的用法示例。
在下文中一共展示了SigningKey.from_pem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_key
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def load_key(self, filename):
"""
Load signing key (from pem file)
"""
default_sk = SigningKey.from_pem(keys_default_pem)
with open(filename, "r") as sk_file:
sk_pem = sk_file.read()
self.sk = SigningKey.from_pem(sk_pem)
sk_hex = "".join(c.encode('hex') for c in self.sk.to_string())
return default_sk.to_string() == self.sk.to_string()
示例2: load_private_key
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def load_private_key(self):
if self.private_key is None:
msg = 'Please, specify the private_key location.'
raise ValueError(msg)
with open(self.private_key, 'rb') as key_file:
return SigningKey.from_pem(key_file.read())
示例3: sign_subscription
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def sign_subscription(signatory, subscription, private_key_string, public_key_string):
"""
sign a subscription (deep hash of criteria, signatory, signature timestamp, public key)
param signatory: Name or identifier of the signing party (the caller)
param subscription: all fields of subscription signed
param private_key_string: private key used for generating signature
param public_key_string: hashed and inserted into signature block for later validation
"""
ecdsa_signing_key = SigningKey.from_pem(private_key_string)
signature_ts = int(time.time())
hashed_items = []
# append criteria for hashing
hashed_items.append(deep_hash(subscription['criteria']))
# append sub create timestamp for hashing
hashed_items.append(subscription['create_ts'])
hashed_items.append(signatory)
hashed_items.append(signature_ts)
hashed_items.append(public_key_string)
verification_hash = final_hash(hashed_items)
signature = ecdsa_signing_key.sign(verification_hash)
digest = signature.encode('base64')
signature_block = assemble_sig_block(subscription, signatory, public_key_string, digest, verification_hash, signature_ts)
return signature_block
示例4: doAuthenticate
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def doAuthenticate(self, msg):
if len(msg.args) == 1 and msg.args[0] == '+':
log.info('%s: Authenticating using SASL.', self.network)
if self.sasl == 'external':
authstring = '+'
elif self.sasl == 'ecdsa-nist256p-challenge':
authstring = base64.b64encode(
self.sasl_username.encode('utf-8')).decode('utf-8')
elif self.sasl == 'plain':
authstring = base64.b64encode('\0'.join([
self.sasl_username,
self.sasl_username,
self.sasl_password
]).encode('utf-8')).decode('utf-8')
self.queueMsg(ircmsgs.IrcMsg(command='AUTHENTICATE', args=(authstring,)))
elif (len(msg.args) == 1 and msg.args[0] != '+' and
self.sasl == 'ecdsa-nist256p-challenge'):
try:
private_key = SigningKey.from_pem(open(self.sasl_ecdsa_key).
read())
authstring = base64.b64encode(
private_key.sign(base64.b64decode(msg.args[0].encode()))).decode('utf-8')
except (BadDigestError, OSError, ValueError) as e:
authstring = "*"
self.queueMsg(ircmsgs.IrcMsg(command='AUTHENTICATE', args=(authstring,)))
示例5: test_sign
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def test_sign(self):
pem = '-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICg7E4NN53YkaWuAwpoqjfAofjzKI7Jq1f532dX+0O6QoAcGBSuBBAAK\noUQDQgAEjZcNa6Kdz6GQwXcUD9iJ+t1tJZCx7hpqBuJV2/IrQBfue8jh8H7Q/4vX\nfAArmNMaGotTpjdnymWlMfszzXJhlw==\n-----END EC PRIVATE KEY-----\n'
signed = utils.sign("message", pem)
sk = SigningKey.from_pem(pem)
vk = sk.get_verifying_key()
print(signed)
signed = binascii.unhexlify(signed)
vk.verify(signed, "message".encode(), hashfunc=hashlib.sha256, sigdecode=ecdsaUtil.sigdecode_der)
示例6: load
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def load(privkeypem):
"""
Load a private key from disk
:param privkeypem:
:return:
"""
with open(privkeypem, "rb") as privfile:
return SigningKey.from_pem(privfile.read())
示例7: __sign_message
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def __sign_message(cls, key_path, message):
key_data = open(key_path).read()
signing_key = SigningKey.from_pem(key_data)
signature = signing_key.sign(
message,
hashfunc=hashlib.sha256,
sigencode=ecdsa.util.sigencode_der
)
signature = base64.b64encode(signature)
return signature
示例8: __init__
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def __init__(self, **kwargs):
try:
config = kwargs['config']
except KeyError:
client_key = kwargs['client_key']
server_key = kwargs['server_key']
identifier = kwargs['identifier']
try:
url = kwargs['url']
except KeyError:
url = 'https://core.bravecollective.net/api'
else:
client_key = config['CORE_AUTH_PRIVATE_KEY']
server_key = config['CORE_AUTH_PUBLIC_KEY']
identifier = config['CORE_AUTH_IDENTIFIER']
try:
url = config['CORE_AUTH_URL']
except KeyError:
url = 'https://core.bravecollective.net/api'
if isinstance(client_key, SigningKey):
priv = client_key
elif hasattr(client_key, 'read'):
priv_pem = client_key.read()
priv = SigningKey.from_pem(priv_pem)
else:
with open(client_key, 'r') as f:
priv_pem = f.read()
priv = SigningKey.from_pem(priv_pem)
if isinstance(server_key, VerifyingKey):
pub = server_key
elif hasattr(server_key, 'read'):
pub_pem = server_key.read()
pub = VerifyingKey.from_pem(pub_pem)
else:
with open(server_key, 'r') as f:
pub_pem = f.read()
pub = VerifyingKey.from_pem(pub_pem)
self.api = API(url, identifier, priv, pub,
requests_session)
示例9: get_signKey
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def get_signKey(self, usrID):
"""
Get the user's sign key
Returns:
The sign key
"""
filename = '/opt/stack/swift/swift/common/middleware/sk.key'
with open(filename, 'r') as f:
sign_key = f.read()
return SigningKey.from_pem(sign_key)
示例10: load_keys
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def load_keys():
'''
Loads the keys from disk or creates a new key pair if that is needed.
'''
if not os.path.isfile(sk_file):
return create_reg_keys()
sk = SigningKey.from_pem(open(sk_file).read())
vk = sk.get_verifying_key()
return sk,vk
示例11: sign_file
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def sign_file(args):
from ecdsa import SigningKey
private_key = SigningKey.from_pem(args.signatureKey.read())
message = args.file.read()
name = os.path.basename(args.file.name)
signature = sign(private_key, message)
with open('%s.sig' % name, 'wb') as sign_file:
sign_file.write(signature)
示例12: load
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def load(path):
with open(path, 'rb') as f:
pem = f.read()
try:
key = RSA.importKey(pem)
if key.n.bit_length() != 2048:
raise Exception("Unsupported RSA bit length, only 2048 supported")
return RSA2048(key)
except ValueError:
key = SigningKey.from_pem(pem)
if key.curve.name != 'NIST256p':
raise Exception("Unsupported ECDSA curve")
return ECDSA256P1(key)
示例13: key_import
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def key_import(self, private_keyfile_path, public_keyfile_path):
self.logger.info(
'key_import(%s, %s)',
private_keyfile_path,
public_keyfile_path)
with open(public_keyfile_path, 'r') as f:
self.public_key = VerifyingKey.from_pem(f.read())
with open(private_keyfile_path, 'r') as f:
self.private_key = SigningKey.from_pem(f.read())
self.key_register(self.public_key)
示例14: send_message
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def send_message(args):
from ecdsa import SigningKey
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
backend = default_backend()
key_size = 2048
message = args.message.read()
public_key = serialization.load_pem_public_key(
args.publicKey.read(),
backend=backend
)
key = generate_key()
key_enc = public_key.encrypt(
key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
signatureKey = SigningKey.from_pem(args.signatureKey.read())
signature = sign(signatureKey, message)
iv, encrypted = encrypt(key, message + signature)
name = os.path.basename(args.message.name)
with open('%s.enc' % name, 'wb') as output:
output.write(key_enc + iv + encrypted)
示例15: sign_transaction
# 需要导入模块: from ecdsa import SigningKey [as 别名]
# 或者: from ecdsa.SigningKey import from_pem [as 别名]
def sign_transaction(signatory,
private_key_string,
public_key_string,
transaction,
log=logging.getLogger(__name__)):
"""
Sign a transaction
:param signatory: Name or identifier of the signing party (the caller)
:param private_key_string:
:param public_key_string:
:param transaction: Transaction to sign
:param log:
:return: Transaction with assembled signature field
"""
hashed_items = []
child_signature = None
if "signature" in transaction:
child_signature = transaction["signature"]
log.info("Saving the child signature: %s" % str(child_signature))
# add old signature digest to hash list if there was one
if 'digest' in child_signature:
log.info("Adding signature digest to hash list")
hashed_items.append(child_signature["digest"])
signature_ts = int(time.time())
log.info("Loading private key from string")
ecdsa_signing_key = SigningKey.from_pem(private_key_string)
# add transaction header info to hashed_items
transaction_header = transaction["header"]
for header_key in transaction_header:
if header_key not in non_included_txn_items:
hashed_items.append(transaction_header[header_key])
# append my signing info for hashing
hashed_items.append(signatory)
hashed_items.append(signature_ts)
hashed_items.append(public_key_string)
log.info("Creating stripped hash")
stripped_hash = final_hash(hashed_items)
log.debug("stripped_transaction_hash=%s" % stripped_hash)
# put hashed transaction payload back and append to hashed_items
if transaction["payload"]:
log.info("Hashing payload")
hashed_items.append(deep_hash(transaction["payload"]))
# generate hash with with hashed payload included
log.info("Generating hash")
hash = final_hash(hashed_items)
else:
hash = stripped_hash
# merge stripped_hash and hash to send for signing
log.info("Merging stripped and full hash")
merged_hash = merge_hashes(stripped_hash, hash)
# sign merged hash
log.info("Signing merged hash")
signature = ecdsa_signing_key.sign(str(merged_hash))
log.info("Base64 encoding the signature")
digest = signature.encode('base64')
assemble_sig_block(transaction, signatory, public_key_string, digest, hash, signature_ts, stripped_hash,
child_signature)
return transaction