本文整理汇总了Python中ecdsa.VerifyingKey.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python VerifyingKey.from_string方法的具体用法?Python VerifyingKey.from_string怎么用?Python VerifyingKey.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ecdsa.VerifyingKey
的用法示例。
在下文中一共展示了VerifyingKey.from_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_parent
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def from_parent(parent_key, i):
if i & HARDENED_INDEX:
raise ValueError("Can't generate a hardened child key from a parent public key.")
child = hmac.new(parent_key.chain_code,
parent_key.compressed_key + i.to_bytes(length=4, byteorder='big'),
hashlib.sha512).digest()
child_left, child_right = child[:32], child[32:]
if int.from_bytes(child_left, 'big') >= ecdsa.generator_256.order():
return None
temp_pri_key = SigningKey.from_string(string=child_left, curve=curves.NIST256p)
ki = temp_pri_key.verifying_key.pubkey.point + parent_key.key.pubkey.point
if ki == ellipticcurve.INFINITY:
return None
return HDPublicKey(public_key=VerifyingKey.from_public_point(point=ki, curve=curves.NIST256p),
chain_code=child_right,
index=i,
depth=parent_key.depth + 1,
parent_fingerprint=parent_key.fingerprint)
示例2: ec_mult
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_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()
示例3: mitm_verify
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def mitm_verify(self, sig, expected_xpub):
# First try with Pycoin
try:
from pycoin.key.BIP32Node import BIP32Node
from pycoin.contrib.msg_signing import verify_message
from pycoin.encoding import from_bytes_32
from base64 import b64encode
mk = BIP32Node.from_wallet_key(expected_xpub)
return verify_message(mk, b64encode(sig), msg_hash=from_bytes_32(self.session_key))
except ImportError:
pass
# If Pycoin is not available, do it using ecdsa
from ecdsa import BadSignatureError, SECP256k1, VerifyingKey
pubkey, chaincode = decode_xpub(expected_xpub)
vk = VerifyingKey.from_string(get_pubkey_string(pubkey), curve=SECP256k1)
try:
ok = vk.verify_digest(sig[1:], self.session_key)
except BadSignatureError:
ok = False
return ok
示例4: validate_tx_in
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def validate_tx_in(tx_in, transaction, a_unspent_tx_outs):
referenced_utxo = [utxo for utxo in a_unspent_tx_outs if utxo.tx_out_id == tx_in.tx_out_id and utxo.tx_out_index == tx_in.tx_out_index][0]
if referenced_utxo == []:
print('referenced txOut not found: ' + json.dumps(tx_in))
return False
address = referenced_utxo.address
vk = VerifyingKey.from_string(bytes.fromhex(address), curve=SECP256k1)
try:
vk.verify(bytes.fromhex(tx_in.signature), transaction.id.encode())
except Exception as e:
# change the exception
print('invalid tx_in signature: %s txId: %s address: %s' % (tx_in.signature, transaction.id, referenced_utxo.address))
return False
return True
示例5: sign_tx_in
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def sign_tx_in(transaction, tx_in_index,
private_key, a_unspent_tx_outs):
tx_in = transaction.tx_ins[tx_in_index]
data_to_sign = str(transaction.id)
referenced_utxo = find_unspent_tx_out(tx_in.tx_out_id, tx_in.tx_out_index, a_unspent_tx_outs)
if referenced_utxo is None:
print('could not find referenced txOut')
# throw Error()
return False
referenced_address = referenced_utxo.address
if get_public_key(private_key) != referenced_address:
print('trying to sign an input with private' +
' key that does not match the address that is referenced in tx_in')
# throw Error()
return False
# key = ec.keyFromPrivate(private_key, 'hex')
sk = SigningKey.from_string(private_key, curve=SECP256k1)
signature = binascii.b2a_hex(sk.sign(data_to_sign.encode())).decode()
return signature
示例6: get_child
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def get_child(self, index, hardened=False):
left, right = self.get_hash(index, hardened)
point = ((left * generator_secp256k1)
+ VerifyingKey.from_string(self.key.uncompressed[1:], curve=SECP256k1).pubkey.point)
if point == INFINITY:
raise ValueError('Computed point equals INFINITY')
return ExtendedPublicKey(PublicKey.from_point(point), right, self.depth+1, self.get_fingerprint(), index, False)
示例7: from_bytes
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_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
示例8: get_compressed_pk
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def get_compressed_pk(pk):
"""
Constructs the compressed representation of a SECP256k1 ECDSA public key form a given uncompressed key.
:param pk: The uncompressed SECP256k1 key to be decompressed.
:type pk: hex
:return: The compressed SECP256k1 ECDSA key.
:rtype: hex
"""
ecdsa_pk = VerifyingKey.from_string(unhexlify(pk[2:]), curve=SECP256k1)
compressed_pk = serialize_pk(ecdsa_pk)
return compressed_pk
示例9: hex2key
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def hex2key(hex_key):
key_bytes = unhexlify(hex_key)
if len(hex_key) == 64:
return SigningKey.from_string(key_bytes, curve=NIST256p,
hashfunc=sha256)
elif len(hex_key) == 128:
return VerifyingKey.from_string(key_bytes, curve=NIST256p,
hashfunc=sha256)
else:
raise ValueError("Key in hex form is of the wrong length.")
示例10: get_public_key
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def get_public_key(private_key):
sk = SigningKey.from_string(private_key
, curve=SECP256k1)
vk = sk.get_verifying_key()
return binascii.b2a_hex(vk.to_string()).decode()
示例11: verify
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def verify(self):
vk = VerifyingKey.from_string(bytes.fromhex(self.public_key), curve=SECP256k1)
try:
vk.verify(bytes.fromhex(self.signature), SHA256.new(str(self.id).encode()).digest())
except keys.BadSignatureError:
print('invalid transaction signature')
return False
return True
示例12: sign
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def sign(self, private_key):
data_to_sign = SHA256.new(str(self.id).encode()).digest()
sk = SigningKey.from_string(bytes.fromhex(private_key), curve=SECP256k1)
self.signature = binascii.b2a_hex(sk.sign(data_to_sign)).decode()
示例13: _from_wif
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def _from_wif(self, wif):
"""Creates key from WIFC or WIF format key
Check to_wif for the detailed process. From WIF is the reverse.
Raises
------
ValueError
if the checksum is wrong or if the WIF/WIFC is not from the
configured network.
"""
wif_utf = wif.encode('utf-8')
# decode base58check get key bytes plus checksum
data_bytes = b58decode( wif_utf )
key_bytes = data_bytes[:-4]
checksum = data_bytes[-4:]
# verify key with checksum
data_hash = hashlib.sha256(hashlib.sha256(key_bytes).digest()).digest()
if not checksum == data_hash[0:4]:
raise ValueError('Checksum is wrong. Possible mistype?')
# get network prefix and check with current setup
network_prefix = key_bytes[:1]
if NETWORK_WIF_PREFIXES[get_network()] != network_prefix:
raise ValueError('Using the wrong network!')
# remove network prefix
key_bytes = key_bytes[1:]
# check length of bytes and if > 32 then compressed
# use this to instantite an ecdsa key
if len(key_bytes) > 32:
self.key = SigningKey.from_string(key_bytes[:-1], curve=SECP256k1)
else:
self.key = SigningKey.from_string(key_bytes, curve=SECP256k1)
示例14: VerifySignature
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def VerifySignature(message, signature, public_key, unhex=True):
"""
Verify the integrity of the message.
Args:
message (hexstr or str): the message to verify.
signature (bytearray): the signature belonging to the message.
public_key (ECPoint|bytes): the public key to use for verifying the signature. If `public_key` is of type bytes then it should be raw bytes (i.e. b'\xAA\xBB').
unhex (bool): whether the message should be unhexlified before verifying
Returns:
bool: True if verification passes. False otherwise.
"""
if type(public_key) is EllipticCurve.ECPoint:
pubkey_x = public_key.x.value.to_bytes(32, 'big')
pubkey_y = public_key.y.value.to_bytes(32, 'big')
public_key = pubkey_x + pubkey_y
if unhex:
try:
message = binascii.unhexlify(message)
except binascii.Error:
pass
elif isinstance(message, str):
message = message.encode('utf-8')
if len(public_key) == 33:
public_key = bitcoin.decompress(public_key)
public_key = public_key[1:]
try:
vk = VerifyingKey.from_string(public_key, curve=NIST256p, hashfunc=hashlib.sha256)
res = vk.verify(signature, message, hashfunc=hashlib.sha256)
return res
except Exception:
pass
return False
示例15: sign
# 需要导入模块: from ecdsa import VerifyingKey [as 别名]
# 或者: from ecdsa.VerifyingKey import from_string [as 别名]
def sign(msg, privkey):
from ecdsa import SigningKey
if isinstance(privkey, bytes):
privkey = SigningKey.from_string(privkey)
return privkey.sign(msg)