Generate base58 encode public-private key pair from a hex encoded private key
334846
def key_pair_from_ed25519_key(hex_private_key):
    """"""
    priv_key = crypto.Ed25519SigningKey(bytes.fromhex(hex_private_key)[:32], encoding='bytes')
    public_key = priv_key.get_verifying_key()
    return CryptoKeypair(private_key=priv_key.encode(encoding='base58').decode('utf-8'),
                         public_key=public_key.encode(encoding='base58').decode('utf-8'))
1019784
def create_key_pair(self, cert_type, bits):
        

        pkey = crypto.PKey()
        pkey.generate_key(cert_type, bits)
        return pkey
1000161
function generateKeyPair(scheme) {
  let keypair;
  let keys;

  switch (scheme) {
    case 'ed25519':
    case 'secp256k1':
      keypair = schemes[scheme].genKeyPair();
      keys = {
        scheme: scheme,
        private: keypair.getPrivate('hex'),
        public: keypair.getPublic('hex')
      };
      break;
    default:
      throw new Error('invalid-scheme');
      break;
  }

  return keys;
}
1084671
def from_private_key(path: str) -> SigningKeyType:
        
        key = load_key(path)
        key.pubkey = Base58Encoder.encode(key.vk)
        return key
1038724
function generatePskSecret(ecdhForLocalDevice, remotePeerPublicKey,
                                  pskIdentityField) {
  var sxy = ecdhForLocalDevice.computeSecret(remotePeerPublicKey);
  return HKDF(module.exports.SHA256, sxy, pskIdentityField)
    .derive('', module.exports.AES_256_KEY_SIZE);
}
710232
def encode_hex_secret secret
      # TODO(stevenle): Something further encodes the secret to a 64-char hex
      # string. For now, use adb logcat to figure out what the expected challenge
      # is. Eventually, make sure the encoding matches the server reference
      # implementation:
      #   http://code.google.com/p/google-tv-pairing-protocol/source/browse/src/com/google/polo/pairing/PoloChallengeResponse.java

      encoded_secret = [secret.to_i(16)].pack("N").unpack("cccc")[2..3].pack("c*")

      # Per "Polo Implementation Overview", section 6.1, client key material is
      # hashed first, followed by the server key material, followed by the nonce.
      digest = OpenSSL::Digest::Digest.new('sha256')
      digest << @gtv.ssl_client.cert.public_key.n.to_s(2)       # client modulus
      digest << @gtv.ssl_client.cert.public_key.e.to_s(2)       # client exponent
      digest << @gtv.ssl_client.peer_cert.public_key.n.to_s(2)  # server modulus
      digest << @gtv.ssl_client.peer_cert.public_key.e.to_s(2)  # server exponent

      digest << encoded_secret[encoded_secret.size / 2]
      return digest.digest
    end
218117
def set_privkey_compressed(privkey, compressed=True):
    
    if len(privkey) != 64 and len(privkey) != 66:
        raise ValueError("expected 32-byte private key as a hex string")

    # compressed?
    if compressed and len(privkey) == 64:
        privkey += '01'

    if not compressed and len(privkey) == 66:
        if privkey[-2:] != '01':
            raise ValueError("private key does not end in '01'")

        privkey = privkey[:-2]

    return privkey
176070
def generate_keypair(crypto, seed, password=None):
    
    if crypto in ['eth', 'etc']:
        raise CurrencyNotSupported("Ethereums not yet supported")

    pub_byte, priv_byte = get_magic_bytes(crypto)
    priv = sha256(seed)
    pub = privtopub(priv)

    priv_wif = encode_privkey(priv, 'wif_compressed', vbyte=priv_byte)
    if password:
        # pycrypto etc. must be installed or this will raise ImportError, hence inline import.
        from .bip38 import Bip38EncryptedPrivateKey
        priv_wif = str(Bip38EncryptedPrivateKey.encrypt(crypto, priv_wif, password))

    compressed_pub = encode_pubkey(pub, 'hex_compressed')
    ret = {
        'public': {
            'hex_uncompressed': pub,
            'hex': compressed_pub,
            'address': pubtoaddr(compressed_pub, pub_byte)
        },
        'private': {
            'wif': priv_wif
        }
    }
    if not password:
        # only these are valid when no bip38 password is supplied
        ret['private']['hex'] = encode_privkey(priv, 'hex_compressed', vbyte=priv_byte)
        ret['private']['hex_uncompressed'] = encode_privkey(priv, 'hex', vbyte=priv_byte)
        ret['private']['wif_uncompressed'] = encode_privkey(priv, 'wif', vbyte=priv_byte)

    return ret
334209
def b2a_base58(s):
    """"""
    v, prefix = to_long(256, lambda x: x, iterbytes(s))
    s = from_long(v, prefix, BASE58_BASE, lambda v: BASE58_ALPHABET[v])
    return s.decode("utf8")
304196
def from_pubkey(cls: Type[CRCPubkeyType], pubkey: str) -> CRCPubkeyType:
        
        hash_root = hashlib.sha256()
        hash_root.update(base58.b58decode(pubkey))
        hash_squared = hashlib.sha256()
        hash_squared.update(hash_root.digest())
        b58_checksum = ensure_str(base58.b58encode(hash_squared.digest()))

        crc = b58_checksum[:3]
        return cls(pubkey, crc)
1374411
def ed25519_public_key_to_string(key):
    
    return base64.b64encode(key.public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw,
    ), None).decode('utf-8')
25832
def generate(cls):
        
        return cls(libnacl.randombytes(PrivateKey.SIZE), encoder=encoding.RawEncoder)
696264
def from_b58check(private_key):
        
        b58dec = base58.b58decode_check(private_key)
        version = b58dec[0]
        assert version in [PrivateKey.TESTNET_VERSION,
                           PrivateKey.MAINNET_VERSION]

        return PrivateKey(int.from_bytes(b58dec[1:], 'big'))
673635
def decrypt(private, ciphertext, output):
    
    privatekeydata = json.load(private)
    assert 'pub' in privatekeydata
    pub = load_public_key(privatekeydata['pub'])

    log("Loading private key")
    private_key_error = "Invalid private key"
    assert 'key_ops' in privatekeydata, private_key_error
    assert "decrypt" in privatekeydata['key_ops'], private_key_error
    assert 'p' in privatekeydata, private_key_error
    assert 'q' in privatekeydata, private_key_error
    assert privatekeydata['kty'] == 'DAJ', private_key_error

    _p = phe.util.base64_to_int(privatekeydata['p'])
    _q = phe.util.base64_to_int(privatekeydata['q'])

    private_key = phe.PaillierPrivateKey(pub, _p, _q)

    log("Decrypting ciphertext")
    enc = load_encrypted_number(ciphertext, pub)
    out = private_key.decrypt(enc)
    print(out, file=output)
1320385
def generate_rsa_key_pair():
    """"""

    key = rsa.generate_private_key(
        backend=default_backend(), public_exponent=65537, key_size=2048)

    public_key = key.public_key().public_bytes(
        serialization.Encoding.OpenSSH,
        serialization.PublicFormat.OpenSSH).decode("utf-8")

    pem = key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()).decode("utf-8")

    return public_key, pem
777331
def b58encode(v: bytes) -> str:
    ''''''

    origlen = len(v)
    v = v.lstrip(b'\0')
    newlen = len(v)

    p, acc = 1, 0
    for c in iseq(v[::-1]):
        acc += p * c
        p = p << 8

    result = ''
    while acc > 0:
        acc, mod = divmod(acc, 58)
        result += alphabet[mod]

    return (result + alphabet[0] * (origlen - newlen))[::-1]
813038
function sign(private_key, data) {
  var key = new bitcoinjs.ECKey(new BigInteger(private_key, 16), false);
  var hash = bitcoinjs.crypto.hash256(new Buffer(data, 'hex'));
  return key.sign(hash).toDER().toString('hex');
}