本文整理汇总了Python中fastecdsa.curve.P256属性的典型用法代码示例。如果您正苦于以下问题:Python curve.P256属性的具体用法?Python curve.P256怎么用?Python curve.P256使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类fastecdsa.curve
的用法示例。
在下文中一共展示了curve.P256属性的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pub_key
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def get_pub_key(priv_key: str) -> str:
'''
Returns the public key associated with
the private key. 'x' is used to split
up the x and y coordinates of the public
key
'''
pub_key = keys.get_public_key(int(priv_key, 16), curve.P256)
return '{:x}'.format(pub_key.x) + 'x' + '{:x}'.format(pub_key.y)
示例2: test_generate_and_parse_pem
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def test_generate_and_parse_pem(self):
d, Q = gen_keypair(P256)
export_key(d, curve=P256, filepath='p256.key')
export_key(Q, curve=P256, filepath='p256.pub')
parsed_d, parsed_Q = import_key('p256.key')
self.assertEqual(parsed_d, d)
self.assertEqual(parsed_Q, Q)
parsed_d, parsed_Q = import_key('p256.pub')
self.assertTrue(parsed_d is None)
self.assertEqual(parsed_Q, Q)
remove('p256.key')
remove('p256.pub')
示例3: get_new_priv_key
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def get_new_priv_key() -> str:
'''
Returns a random private key in hex format
'''
return '{:x}'.format(keys.gen_private_key(curve.P256))
示例4: pub_key_to_point
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def pub_key_to_point(pub_key: str) -> Point:
'''
Given a public key, return a Point object
(Used to verify signatures)
'''
xs, ys = pub_key.split('x')
return Point(int(xs, 16), int(ys, 16), curve=curve.P256)
示例5: new_keypair
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def new_keypair(self):
""" 生成公私钥的键值对"""
priv_key = keys.gen_private_key(curve.P256)
pub_key = keys.get_public_key(priv_key, curve.P256)
pub_key = "".join([str(pub_key.x), str(pub_key.y)])
self.private_key = priv_key
self.pub_key = pub_key
return priv_key, pub_key
示例6: sign
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def sign(self, message, generic=False):
"""
Sign a raw sequence of bytes
:param message: sequence of bytes, raw format or hexadecimal notation
:param generic: do not specify elliptic curve if set to True
:return: signature in base58 encoding
"""
message = scrub_input(message)
if not self.is_secret:
raise ValueError("Cannot sign without a secret key.")
# Ed25519
if self.curve == b"ed":
digest = pysodium.crypto_generichash(message)
signature = pysodium.crypto_sign_detached(digest, self._secret_key)
# Secp256k1
elif self.curve == b"sp":
pk = secp256k1.PrivateKey(self._secret_key)
signature = pk.ecdsa_serialize_compact(
pk.ecdsa_sign(message, digest=blake2b_32))
# P256
elif self.curve == b"p2":
r, s = sign(msg=message, d=bytes_to_int(self._secret_key), hashfunc=blake2b_32)
signature = int_to_bytes(r) + int_to_bytes(s)
else:
assert False
if generic:
prefix = b'sig'
else:
prefix = self.curve + b'sig'
return base58_encode(signature, prefix).decode()
示例7: verify
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def verify(self, signature, message):
"""
Verify signature, raise exception if it is not valid
:param message: sequance of bytes, raw format or hexadecimal notation
:param signature: a signature in base58 encoding
"""
signature = scrub_input(signature)
message = scrub_input(message)
if not self._public_key:
raise ValueError("Cannot verify without a public key")
if signature[:3] != b'sig': # not generic
if self.curve != signature[:2]: # "sp", "p2" "ed"
raise ValueError("Signature and public key curves mismatch.")
signature = base58_decode(signature)
# Ed25519
if self.curve == b"ed":
digest = pysodium.crypto_generichash(message)
try:
pysodium.crypto_sign_verify_detached(signature, digest, self._public_key)
except ValueError:
raise ValueError('Signature is invalid.')
# Secp256k1
elif self.curve == b"sp":
pk = secp256k1.PublicKey(self._public_key, raw=True)
sig = pk.ecdsa_deserialize_compact(signature)
if not pk.ecdsa_verify(message, sig, digest=blake2b_32):
raise ValueError('Signature is invalid.')
# P256
elif self.curve == b"p2":
pk = SEC1Encoder.decode_public_key(self._public_key, curve=P256)
r, s = bytes_to_int(signature[:32]), bytes_to_int(signature[32:])
if not verify(sig=(r, s), msg=message, Q=pk, hashfunc=blake2b_32):
raise ValueError('Signature is invalid.')
else:
assert False
示例8: elgamal_enc
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def elgamal_enc(pub_key, msg):
k = random.getrandbits(255)
c1 = curve.P256.G * k
c2 = (pub_key * k).x + msg
return c1, c2
示例9: init_flash
# 需要导入模块: from fastecdsa import curve [as 别名]
# 或者: from fastecdsa.curve import P256 [as 别名]
def init_flash():
assert_status(usb.cmd(reset_blob))
client_private = gen_private_key(P256)
client_public = get_public_key(client_private, P256)
partition_flash(flash_layout_hardcoded, client_public)
rsp=usb.cmd(unhex('01'))
assert_status(rsp)
# ^ get device info, contains firmware version which is needed to lookup pubkey for server cert validation
rsp=usb.cmd(unhex('50'))
assert_status(rsp)
flush_changes()
rsp=rsp[2:]
l,=unpack('<L', rsp[:4])
if len(rsp) != l:
raise Exception('Length mismatch')
zeroes, rsp = rsp[4:-400], rsp[-400:]
if zeroes != b'\0' * len(zeroes):
raise Exception('Expected zeroes')
tls.handle_ecdh(rsp)
tls.handle_priv(encrypt_key(client_private, client_public))
tls.open()
# Wipe newly created partitions clean
erase_flash(1)
erase_flash(2)
erase_flash(5)
erase_flash(6)
erase_flash(4)
# Persist certs and keys on cert partition.
write_flash(1, 0, tls.makeTlsFlash())
# Reboot
reboot()