def _bcrypt_generate_pair(algorithm, bit_size=None, curve=None):
if algorithm == '<STR_LIT>':<EOL><INDENT>alg_constant = BcryptConst.BCRYPT_RSA_ALGORITHM<EOL>struct_type = '<STR_LIT>'<EOL>private_blob_type = BcryptConst.BCRYPT_RSAFULLPRIVATE_BLOB<EOL>public_blob_type = BcryptConst.BCRYPT_RSAPUBLIC_BLOB<EOL><DEDENT>elif algorithm == '<STR_LIT>':<EOL><INDENT>alg_constant = BcryptConst.BCRYPT_DSA_ALGORITHM<EOL>if bit_size > <NUM_LIT>:<EOL><INDENT>struct_type = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>struct_type = '<STR_LIT>'<EOL><DEDENT>private_blob_type = BcryptConst.BCRYPT_DSA_PRIVATE_BLOB<EOL>public_blob_type = BcryptConst.BCRYPT_DSA_PUBLIC_BLOB<EOL><DEDENT>else:<EOL><INDENT>alg_constant = {<EOL>'<STR_LIT>': BcryptConst.BCRYPT_ECDSA_P256_ALGORITHM,<EOL>'<STR_LIT>': BcryptConst.BCRYPT_ECDSA_P384_ALGORITHM,<EOL>'<STR_LIT>': BcryptConst.BCRYPT_ECDSA_P521_ALGORITHM,<EOL>}[curve]<EOL>bit_size = {<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>}[curve]<EOL>struct_type = '<STR_LIT>'<EOL>private_blob_type = BcryptConst.BCRYPT_ECCPRIVATE_BLOB<EOL>public_blob_type = BcryptConst.BCRYPT_ECCPUBLIC_BLOB<EOL><DEDENT>alg_handle = open_alg_handle(alg_constant)<EOL>key_handle_pointer = new(bcrypt, '<STR_LIT>')<EOL>res = bcrypt.BCryptGenerateKeyPair(alg_handle, key_handle_pointer, bit_size, <NUM_LIT:0>)<EOL>handle_error(res)<EOL>key_handle = unwrap(key_handle_pointer)<EOL>res = bcrypt.BCryptFinalizeKeyPair(key_handle, <NUM_LIT:0>)<EOL>handle_error(res)<EOL>private_out_len = new(bcrypt, '<STR_LIT>')<EOL>res = bcrypt.BCryptExportKey(key_handle, null(), private_blob_type, null(), <NUM_LIT:0>, private_out_len, <NUM_LIT:0>)<EOL>handle_error(res)<EOL>private_buffer_length = deref(private_out_len)<EOL>private_buffer = buffer_from_bytes(private_buffer_length)<EOL>res = bcrypt.BCryptExportKey(<EOL>key_handle,<EOL>null(),<EOL>private_blob_type,<EOL>private_buffer,<EOL>private_buffer_length,<EOL>private_out_len,<EOL><NUM_LIT:0><EOL>)<EOL>handle_error(res)<EOL>private_blob_struct_pointer = struct_from_buffer(bcrypt, struct_type, private_buffer)<EOL>private_blob_struct = unwrap(private_blob_struct_pointer)<EOL>struct_size = sizeof(bcrypt, private_blob_struct)<EOL>private_blob = bytes_from_buffer(private_buffer, private_buffer_length)[struct_size:]<EOL>if algorithm == '<STR_LIT>':<EOL><INDENT>private_key = _bcrypt_interpret_rsa_key_blob('<STR_LIT>', private_blob_struct, private_blob)<EOL><DEDENT>elif algorithm == '<STR_LIT>':<EOL><INDENT>if bit_size > <NUM_LIT>:<EOL><INDENT>private_key = _bcrypt_interpret_dsa_key_blob('<STR_LIT>', <NUM_LIT:2>, private_blob_struct, private_blob)<EOL><DEDENT>else:<EOL><INDENT>private_key = _bcrypt_interpret_dsa_key_blob('<STR_LIT>', <NUM_LIT:1>, private_blob_struct, private_blob)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>private_key = _bcrypt_interpret_ec_key_blob('<STR_LIT>', private_blob_struct, private_blob)<EOL><DEDENT>public_out_len = new(bcrypt, '<STR_LIT>')<EOL>res = bcrypt.BCryptExportKey(key_handle, null(), public_blob_type, null(), <NUM_LIT:0>, public_out_len, <NUM_LIT:0>)<EOL>handle_error(res)<EOL>public_buffer_length = deref(public_out_len)<EOL>public_buffer = buffer_from_bytes(public_buffer_length)<EOL>res = bcrypt.BCryptExportKey(<EOL>key_handle,<EOL>null(),<EOL>public_blob_type,<EOL>public_buffer,<EOL>public_buffer_length,<EOL>public_out_len,<EOL><NUM_LIT:0><EOL>)<EOL>handle_error(res)<EOL>public_blob_struct_pointer = struct_from_buffer(bcrypt, struct_type, public_buffer)<EOL>public_blob_struct = unwrap(public_blob_struct_pointer)<EOL>struct_size = sizeof(bcrypt, public_blob_struct)<EOL>public_blob = bytes_from_buffer(public_buffer, public_buffer_length)[struct_size:]<EOL>if algorithm == '<STR_LIT>':<EOL><INDENT>public_key = _bcrypt_interpret_rsa_key_blob('<STR_LIT>', public_blob_struct, public_blob)<EOL><DEDENT>elif algorithm == '<STR_LIT>':<EOL><INDENT>if bit_size > <NUM_LIT>:<EOL><INDENT>public_key = _bcrypt_interpret_dsa_key_blob('<STR_LIT>', <NUM_LIT:2>, public_blob_struct, public_blob)<EOL><DEDENT>else:<EOL><INDENT>public_key = _bcrypt_interpret_dsa_key_blob('<STR_LIT>', <NUM_LIT:1>, public_blob_struct, public_blob)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>public_key = _bcrypt_interpret_ec_key_blob('<STR_LIT>', public_blob_struct, public_blob)<EOL><DEDENT>return (load_public_key(public_key), load_private_key(private_key))<EOL>
Generates a public/private key pair using CNG

:param algorithm:
    The key algorithm - "rsa", "dsa" or "ec"

:param bit_size:
    An integer - used for "rsa" and "dsa". For "rsa" the value maye be 1024,
    2048, 3072 or 4096. For "dsa" the value may be 1024, plus 2048 or 3072
    if on Windows 8 or newer.

:param curve:
    A unicode string - used for "ec" keys. Valid values include "secp256r1",
    "secp384r1" and "secp521r1".

:raises:
    ValueError - when any of the parameters contain an invalid value
    TypeError - when any of the parameters are of the wrong type
    OSError - when an error is returned by the OS crypto library

:return:
    A 2-element tuple of (PublicKey, PrivateKey). The contents of each key
    may be saved by calling .asn1.dump().