本文整理汇总了Python中ecdsa.util.number_to_string方法的典型用法代码示例。如果您正苦于以下问题:Python util.number_to_string方法的具体用法?Python util.number_to_string怎么用?Python util.number_to_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ecdsa.util
的用法示例。
在下文中一共展示了util.number_to_string方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ec_get_public_key_by_private_key
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def ec_get_public_key_by_private_key(private_key: bytes, curve_name) -> bytes:
if curve_name == Curve.P256:
private_key = SigningKey.from_string(string=private_key, curve=curves.NIST256p)
verifying_key = private_key.get_verifying_key()
order = verifying_key.pubkey.order
x_int = verifying_key.pubkey.point.x()
y_int = verifying_key.pubkey.point.y()
x_str = util.number_to_string(x_int, order)
if y_int % 2 == 0:
point_str = util.b('\x02') + x_str
else:
point_str = util.b('\x03') + x_str
elif curve_name == Curve.P224:
raise SDKException(ErrorCode.unsupported_key_type)
elif curve_name == Curve.P384:
raise SDKException(ErrorCode.unsupported_key_type)
elif curve_name == Curve.P521:
raise SDKException(ErrorCode.unsupported_key_type)
else:
raise SDKException(ErrorCode.unsupported_key_type)
return point_str
示例2: encrypt_message
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def encrypt_message(self, message, pubkey):
pk = ser_to_point(pubkey)
if not ecdsa.ecdsa.point_is_valid(generator_secp256k1, pk.x(), pk.y()):
raise Exception('invalid pubkey')
ephemeral_exponent = number_to_string(ecdsa.util.randrange(pow(2,256)), generator_secp256k1.order())
ephemeral = EC_KEY(ephemeral_exponent)
ecdh_key = point_to_ser(pk * ephemeral.privkey.secret_multiplier)
key = hashlib.sha512(ecdh_key).digest()
iv, key_e, key_m = key[0:16], key[16:32], key[32:]
ciphertext = aes_encrypt_with_iv(key_e, iv, message)
ephemeral_pubkey = ephemeral.get_public_key(compressed=True).decode('hex')
encrypted = 'BIE1' + ephemeral_pubkey + ciphertext
mac = hmac.new(key_m, encrypted, hashlib.sha256).digest()
return base64.b64encode(encrypted + mac)
示例3: _CKD_priv
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def _CKD_priv(k, c, s, is_prime):
import hmac
from ecdsa.util import string_to_number, number_to_string
order = generator_secp256k1.order()
keypair = EC_KEY(k)
cK = GetPubKey(keypair.pubkey,True)
data = chr(0) + k + s if is_prime else cK + s
I = hmac.new(c, data, hashlib.sha512).digest()
k_n = number_to_string( (string_to_number(I[0:32]) + string_to_number(k)) % order , order )
c_n = I[32:]
return k_n, c_n
# Child public key derivation function (from public key only)
# K = master public key
# c = master chain code
# n = index of key we want to derive
# This function allows us to find the nth public key, as long as n is
# non-negative. If n is negative, we need the master private key to find it.
示例4: encrypt_message
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def encrypt_message(cls, message, pubkey):
pk = ser_to_point(pubkey)
if not ecdsa.ecdsa.point_is_valid(generator_secp256k1, pk.x(), pk.y()):
raise Exception('invalid pubkey')
ephemeral_exponent = number_to_string(ecdsa.util.randrange(pow(2, 256)),
generator_secp256k1.order())
ephemeral = EC_KEY(ephemeral_exponent)
ecdh_key = point_to_ser(pk * ephemeral.privkey.secret_multiplier)
key = hashlib.sha512(ecdh_key).digest()
iv, key_e, key_m = key[0:16], key[16:32], key[32:]
ciphertext = aes_encrypt_with_iv(key_e, iv, message)
ephemeral_pubkey = ephemeral.get_public_key(compressed=True).decode('hex')
encrypted = 'BIE1' + ephemeral_pubkey + ciphertext
mac = hmac.new(key_m, encrypted, hashlib.sha256).digest()
return base64.b64encode(encrypted + mac)
示例5: _CKD_priv
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def _CKD_priv(k, c, s, is_prime):
order = generator_secp256k1.order()
keypair = EC_KEY(k)
cK = GetPubKey(keypair.pubkey, True)
data = chr(0) + k + s if is_prime else cK + s
I = hmac.new(c, data, hashlib.sha512).digest()
k_n = number_to_string((string_to_number(I[0:32]) + string_to_number(k)) % order, order)
c_n = I[32:]
return k_n, c_n
# Child public key derivation function (from public key only)
# K = master public key
# c = master chain code
# n = index of key we want to derive
# This function allows us to find the nth public key, as long as n is
# non-negative. If n is negative, we need the master private key to find it.
示例6: ec_mult
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def ec_mult(self, his_pubkey):
# - second call: given the pubkey of far side, calculate the shared pt on curve
# - creates session key based on that
from ecdsa.curves import SECP256k1
from ecdsa import VerifyingKey
from ecdsa.util import number_to_string
# Validate his pubkey a little: this call will check it's on the curve.
assert len(his_pubkey) == 64
his_pubkey = VerifyingKey.from_string(his_pubkey, curve=SECP256k1, hashfunc=sha256)
#print("his pubkey = %s" % b2a_hex(his_pubkey.to_string()))
# do the D-H thing
pt = self.my_key.privkey.secret_multiplier * his_pubkey.pubkey.point
# final key is sha256 of that point, serialized (64 bytes).
order = SECP256k1.order
kk = number_to_string(pt.x(), order) + number_to_string(pt.y(), order)
del self.my_key
return sha256(kk).digest()
示例7: __init__
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def __init__(self, public_key: VerifyingKey, chain_code, index, depth, parent_fingerprint=b'\x00\x00\x00\x00'):
super().__init__(public_key, chain_code, index, depth, parent_fingerprint)
x_str = util.number_to_string(self._key.pubkey.point.x(), self._key.pubkey.order)
if self._key.pubkey.point.y() % 2 == 0:
self._compressed_key = util.b('\x02') + x_str
else:
self._compressed_key = util.b('\x03') + x_str
示例8: from_bytes
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def from_bytes(cls, data: bytes):
""" Generates either a HDPublicKey from the underlying bytes.
The serialization must conform to the description in:
https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
"""
if len(data) < 78:
raise ValueError("b must be at least 78 bytes long.")
version = int.from_bytes(data[:4], 'big')
depth = data[4]
parent_fingerprint = data[5:9]
index = int.from_bytes(data[9:13], 'big')
chain_code = data[13:45]
key_bytes = data[45:78]
if version != HDPublicKey.__VERSION:
raise ValueError('invalid HD Public Key.')
if key_bytes[0] != 0x02 and key_bytes[0] != 0x03:
raise ValueError("First byte of public key must be 0x02 or 0x03!")
# The curve of points satisfying y^2 = x^3 + a*x + b (mod p).
curve = ecdsa.curve_256
x = util.string_to_number(key_bytes[1:])
y = (x * x * x + curve.a() * x + curve.b()) % curve.p()
y = numbertheory.square_root_mod_prime(y, curve.p())
if (key_bytes[0] == 0x03 and y % 2 == 0) or (key_bytes[0] == 0x02 and y % 2 != 0):
y = (y * -1) % curve.p()
order = curves.NIST256p.order
s_key = util.number_to_string(x, order) + util.number_to_string(y, order)
public_key = VerifyingKey.from_string(string=s_key, curve=curves.NIST256p)
rv = cls(
public_key=public_key,
chain_code=chain_code,
index=index,
depth=depth,
parent_fingerprint=parent_fingerprint)
return rv
示例9: __init__
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def __init__(self, key, chain_code: bytes, index: int, depth: int, parent_fingerprint: bytes = b''):
if isinstance(key, int):
key = util.number_to_string(key, curves.NIST256p.order)
private_key = SigningKey.from_string(string=key, curve=curves.NIST256p)
super().__init__(private_key, chain_code, index, depth, parent_fingerprint)
self._public_key = HDPublicKey(
public_key=self._key.verifying_key,
chain_code=self._chain_code,
index=self._index,
depth=self._depth,
parent_fingerprint=self._parent_fingerprint
)
示例10: _CKD_pub
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def _CKD_pub(cK, c, s):
import hmac
from ecdsa.util import string_to_number, number_to_string
order = generator_secp256k1.order()
I = hmac.new(c, cK + s, hashlib.sha512).digest()
curve = SECP256k1
pubkey_point = string_to_number(I[0:32])*curve.generator + ser_to_point(cK)
public_key = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
c_n = I[32:]
cK_n = GetPubKey(public_key.pubkey,True)
return cK_n, c_n
示例11: _do_test_crypto
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def _do_test_crypto(self, message):
G = generator_secp256k1
_r = G.order()
pvk = ecdsa.util.randrange( pow(2,256) ) %_r
Pub = pvk*G
pubkey_c = point_to_ser(Pub,True)
#pubkey_u = point_to_ser(Pub,False)
addr_c = public_key_to_bc_address(pubkey_c)
#addr_u = public_key_to_bc_address(pubkey_u)
#print "Private key ", '%064x'%pvk
eck = EC_KEY(number_to_string(pvk,_r))
#print "Compressed public key ", pubkey_c.encode('hex')
enc = EC_KEY.encrypt_message(message, pubkey_c)
dec = eck.decrypt_message(enc)
assert dec == message
#print "Uncompressed public key", pubkey_u.encode('hex')
#enc2 = EC_KEY.encrypt_message(message, pubkey_u)
dec2 = eck.decrypt_message(enc)
assert dec2 == message
signature = eck.sign_message(message, True, addr_c)
#print signature
EC_KEY.verify_message(addr_c, signature, message)
示例12: serialize_pk
# 需要导入模块: from ecdsa import util [as 别名]
# 或者: from ecdsa.util import number_to_string [as 别名]
def serialize_pk(pk, compressed=True):
""" Serializes a ecdsa.VerifyingKey (public key).
:param compressed: Indicates if the serialized public key will be either compressed or uncompressed.
:type compressed: bool
:param pk: ECDSA VerifyingKey object (public key to be serialized).
:type pk: ecdsa.VerifyingKey
:return: serialized public key.
:rtype: hex str
"""
# Updated with code based on PR #54 from python-ecdsa until the PR gets merged:
# https://github.com/warner/python-ecdsa/pull/54
x_str = number_to_string(pk.pubkey.point.x(), pk.pubkey.order)
if compressed:
if pk.pubkey.point.y() & 1:
prefix = '03'
else:
prefix = '02'
s_key = prefix + hexlify(x_str)
else:
s_key = '04' + hexlify(pk.to_string())
return s_key