Python ecdsa.SigningKey.from_pem() Examples

The following are 11 code examples of ecdsa.SigningKey.from_pem(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ecdsa.SigningKey , or try the search function .
Example #1
Source File: keys.py    From bitcoin_tools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_keys(btc_addr, vault_path=None):
    """ Loads an elliptic curve key pair in PEM format from disk. Keys are stored in their proper objects from the ecdsa
    python library (SigningKey and VerifyingKey respectively)

    :param btc_addr: Bitcoin address associated to the public key of the key pair.
    :type btc_addr: str
    :param vault_path: Path where keys are be stored. Defined in the config file by default.
    :type vault_path: str
    :return: ecdsa key pair as a tuple.
    :rtype: SigningKey, VerifyingKey
    """

    if vault_path is None:
        vault_path = CFG.address_vault

    sk_pem = open(vault_path + btc_addr + '/sk.pem', "r").read()
    pk_pem = open(vault_path + btc_addr + '/pk.pem', "r").read()

    return SigningKey.from_pem(sk_pem), VerifyingKey.from_pem(pk_pem) 
Example #2
Source File: crypto.py    From btcpay-python with MIT License 6 votes vote down vote up
def sign(message, pem):
    message = message.encode()
    sk = SigningKey.from_pem(pem)
    signed = sk.sign(message, hashfunc=hashlib.sha256,
                     sigencode=ecdsaUtil.sigencode_der)
    return binascii.hexlify(signed).decode() 
Example #3
Source File: crypto.py    From btcpay-python with MIT License 5 votes vote down vote up
def get_compressed_public_key_from_pem(pem):
    vks = SigningKey.from_pem(pem).get_verifying_key().to_string()
    bts = binascii.hexlify(vks)
    compressed = compress_key(bts)
    return compressed 
Example #4
Source File: key_utils.py    From bitpay-python with MIT License 5 votes vote down vote up
def get_compressed_public_key_from_pem(pem):
  vks = SigningKey.from_pem(pem).get_verifying_key().to_string()
  bts = binascii.hexlify(vks)
  compressed = compress_key(bts)
  return compressed 
Example #5
Source File: key_utils.py    From bitpay-python with MIT License 5 votes vote down vote up
def sign(message, pem):
  message = message.encode()
  sk = SigningKey.from_pem(pem)
  signed = sk.sign(message, hashfunc=hashlib.sha256, sigencode=ecdsaUtil.sigencode_der)
  return binascii.hexlify(signed).decode() 
Example #6
Source File: key_utils_test.py    From bitpay-python with MIT License 5 votes vote down vote up
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) 
Example #7
Source File: sgx.py    From sgx-kms with Apache License 2.0 5 votes vote down vote up
def generate_key_pair(self, key_dir):
        pub_key_path = os.path.join(key_dir, "public_key.pem")
        priv_key_path = os.path.join(key_dir, "private_key.pem")
        if not os.path.exists(pub_key_path) and not os.path.exists(priv_key_path):
            priv_key = SigningKey.generate(curve=NIST256p)
            pub_key = priv_key.get_verifying_key()
            open(priv_key_path, "w").write(priv_key.to_pem())
            open(pub_key_path, "w").write(pub_key.to_pem())
        else:
            priv_key = SigningKey.from_pem(open(priv_key_path).read())
            pub_key = VerifyingKey.from_pem(open(pub_key_path).read())

        pk64 = pub_key.to_string()
        pk_x, pk_y = pk64[:len(pk64)/2], pk64[len(pk64)/2:]

        hex_priv_key = priv_key.to_string()
        hex_sk = hex_priv_key.encode('hex')

        pk_x = pk_x.encode('hex')
        pk_y = pk_y.encode('hex')
        hex_priv_key_out = [hex_sk[i:i + 2]for i in range(0, len(hex_sk), 2)]
        pk_x_out = [pk_x[i:i + 2] for i in range(0,len(pk_x), 2)]
        pk_y_out = [pk_y[i:i + 2] for i in range(0,len(pk_y), 2)]

        pk_x_out.reverse()
        pk_y_out.reverse()

        pub_key = ""
        for i in range(len(pk_x_out)):
            pub_key = pub_key + pk_x_out[i]
        for i in range(len(pk_y_out)):
            pub_key = pub_key + pk_y_out[i]
        hex_priv_key_out.reverse()
        priv_key = ""
        for i in range(len(hex_priv_key_out)):
            priv_key = priv_key + hex_priv_key_out[i]

        pub_key = base64.b64encode(pub_key + '\0')
        priv_key = base64.b64encode(priv_key + '\0')
        return pub_key , priv_key 
Example #8
Source File: http.py    From storj-python-sdk with MIT License 5 votes vote down vote up
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) 
Example #9
Source File: signing.py    From Adafruit_nRF52_nrfutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load_key(self, filename):
        """
        Load signing key (from pem file)
        """
        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()) 
Example #10
Source File: utils.py    From huobi with MIT License 5 votes vote down vote up
def createPrivateSign(secret_sign, private_key):
    signingkey = SigningKey.from_pem(private_key, hashfunc=hashlib.sha256)
    secret_sign = secret_sign.encode(encoding='UTF8')

    privateSignature = signingkey.sign(secret_sign)
    privateSignature = base64.b64encode(privateSignature)
    return privateSignature 
Example #11
Source File: sgx.py    From sgx-kms with Apache License 2.0 4 votes vote down vote up
def generate_key_pair(self):
        separator = "="
        key_dir = None
        with open("/opt/BarbiE/env.properties") as f:
            for line in f:
                if separator in line:
                    name, value = line.split(separator)
                    if name.strip() == "KEY_PAIR_DIR":
                        key_dir = value.strip()
        pub_key_path = os.path.join(key_dir, "public_key.pem")
        priv_key_path = os.path.join(key_dir, "private_key.pem")
        if not os.path.exists(pub_key_path):
            priv_key = SigningKey.generate(curve=NIST256p)
            pub_key = priv_key.get_verifying_key()
            open(priv_key_path,"w").write(priv_key.to_pem())
            open(pub_key_path,"w").write(pub_key.to_pem())
        else:
            priv_key = SigningKey.from_pem(open(priv_key_path).read())
            pub_key = VerifyingKey.from_pem(open(pub_key_path).read())

        pk64 = pub_key.to_string()
        pk_x, pk_y = pk64[:len(pk64)/2], pk64[len(pk64)/2:]
        hex_priv_key = priv_key.to_string()
        hex_sk = hex_priv_key.encode('hex')

        pk_x = pk_x.encode('hex')
        pk_y = pk_y.encode('hex')
        hex_priv_key_out = [hex_sk[i:i + 2]for i in range(0, len(hex_sk), 2)]
        pk_x_out = [pk_x[i:i + 2] for i in range(0,len(pk_x), 2)]
        pk_y_out = [pk_y[i:i + 2] for i in range(0,len(pk_y), 2)]

        pk_x_out.reverse()
        pk_y_out.reverse()

        pub_key = ""
        for i in range(len(pk_x_out)):
            pub_key = pub_key + pk_x_out[i]
        for i in range(len(pk_y_out)):
            pub_key = pub_key + pk_y_out[i]
        hex_priv_key_out.reverse()
        priv_key = ""
        for i in range(len(hex_priv_key_out)):
            priv_key = priv_key + hex_priv_key_out[i]

        pub_key = base64.b64encode(pub_key + '\0')
        priv_key = base64.b64encode(priv_key + '\0')

        return pub_key , priv_key