• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python openssl.OpenSSL类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pyelliptic.openssl.OpenSSL的典型用法代码示例。如果您正苦于以下问题:Python OpenSSL类的具体用法?Python OpenSSL怎么用?Python OpenSSL使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了OpenSSL类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_rand

    def test_rand(self):
        "Test basic random number generator correctness"
        # There's no way to check if this really is random (it's an
        # algorithmic prng). Still, check for common mistakes.

        data1 = OpenSSL.rand(10)
        data2 = OpenSSL.rand(10)
        self.assertNotEqual(data1, data2)

        blob = OpenSSL.rand(64000)
        stat_zero = [0] * 8
        stat_one = [0] * 8

        for byte in blob:
            byte = ord(byte)
            for i in range(8):
                bit = byte % 2
                byte = byte >> 1
                if bit:
                    stat_one[i] += 1
                else:
                    stat_zero[i] += 1

        for i in range(8):
            diff = float(abs(stat_zero[i] - stat_one[i]))
            # Probabilistic test can sometimes fail, but it should be VERY rare.
            # Result is usually < 500, 0.04 sets limit at a value 1280
            self.assertTrue(diff / stat_zero[i] < 0.04 * stat_zero[i])
开发者ID:blaa,项目名称:PyBitmessage,代码行数:28,代码来源:tests.py


示例2: point_uncompress

    def point_uncompress(self, x, ybit):
        try:
            bn_x = OpenSSL.BN_bin2bn(x, len(x), 0)

            group = OpenSSL.EC_GROUP_new_by_curve_name(self.curve)
            point = OpenSSL.EC_POINT_new(group)
            OpenSSL.EC_POINT_set_compressed_coordinates_GFp(group, point, bn_x, ybit, 0)

            bn_nx = OpenSSL.BN_new()
            bn_ny = OpenSSL.BN_new()

            if (OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, point, bn_nx, bn_ny, 0)) == 0:
                raise Exception("[OpenSSL] EC_POINT_get_affine_coordinates_GFp FAIL ...")
            nx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn_nx))
            ny = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn_ny))
            OpenSSL.BN_bn2bin(bn_nx, nx)
            nx = nx.raw
            OpenSSL.BN_bn2bin(bn_ny, ny)
            ny = ny.raw

            return (nx, ny)
        finally:
            OpenSSL.BN_free(bn_x)
            OpenSSL.EC_GROUP_free(group)
            OpenSSL.EC_POINT_free(point)
            OpenSSL.BN_free(bn_nx)
            OpenSSL.BN_free(bn_ny)
开发者ID:p1tt,项目名称:pyelliptic,代码行数:27,代码来源:ecc.py


示例3: final

 def final(self):
     i = OpenSSL.c_int(0)
     buffer = OpenSSL.malloc(b"", self.cipher.get_blocksize())
     if (OpenSSL.EVP_CipherFinal_ex(self.ctx, OpenSSL.byref(buffer),
                                    OpenSSL.byref(i))) == 0:
         raise Exception("[OpenSSL] EVP_CipherFinal_ex FAIL ...")
     return buffer.raw[0:i.value]
开发者ID:0-vortex,项目名称:ZeroNet,代码行数:7,代码来源:cipher.py


示例4: pointMult

def pointMult(secret):
    # ctx = OpenSSL.BN_CTX_new() #This value proved to cause Seg Faults on
    # Linux. It turns out that it really didn't speed up EC_POINT_mul anyway.
    k = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve('secp256k1'))
    priv_key = OpenSSL.BN_bin2bn(secret, 32, 0)
    group = OpenSSL.EC_KEY_get0_group(k)
    pub_key = OpenSSL.EC_POINT_new(group)

    OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None)
    OpenSSL.EC_KEY_set_private_key(k, priv_key)
    OpenSSL.EC_KEY_set_public_key(k, pub_key)
    # print 'priv_key',priv_key
    # print 'pub_key',pub_key

    size = OpenSSL.i2o_ECPublicKey(k, 0)
    mb = ctypes.create_string_buffer(size)
    OpenSSL.i2o_ECPublicKey(k, ctypes.byref(ctypes.pointer(mb)))
    # print 'mb.raw', mb.raw.encode('hex'), 'length:', len(mb.raw)
    # print 'mb.raw', mb.raw, 'length:', len(mb.raw)

    OpenSSL.EC_POINT_free(pub_key)
    # OpenSSL.BN_CTX_free(ctx)
    OpenSSL.BN_free(priv_key)
    OpenSSL.EC_KEY_free(k)
    return mb.raw
开发者ID:bdholt1,项目名称:PyBitmessage,代码行数:25,代码来源:class_addressGenerator.py


示例5: __set_parameters

 def __set_parameters(self):
     size = OpenSSL.i2d_ECPKParameters(self.os_group, 0)
     mb = ctypes.create_string_buffer(size)
     OpenSSL.i2d_ECPKParameters(self.os_group, ctypes.byref(ctypes.pointer(mb)))
     asntree = [x for x in ASNHelper.consume( mb.raw )][0]
     self.ver, self.field, self.curve, self.G_raw, self.order, self.h = asntree
     
     if self.field[0] == '42.134.72.206.61.1.1': # Prime field
         self.field_type = 'prime'
         self.p = self.field[1]
         self.bitlength = int( math.ceil( math.log( self.p, 2 ) ) )
         self.a = self.curve[0]
         self.b = self.curve[1]
         self.f = lambda x: x**3 + self.a*x + self.b
     elif self.field[0] == '42.134.72.206.61.1.2': # Characteristic two field
         self.field_type = 'power-of-two'
         self.m = self.field[1][0]
         # Maybe bitlength below is not correct..?
         self.bitlength = self.m + 1
         if self.field[1][1] == '42.134.72.206.61.1.2.3.2': # Only one coefficient
             self.poly_coeffs = [self.field[1][2]]
         elif self.field[1][1] == '42.134.72.206.61.1.2.3.3': # Several coefficients
             self.poly_coeffs = self.field[1][2]
         else:
             raise Exception('Unknown field OID %s' % self.field[1][1])
         self.a = self.curve[0]
         self.b = self.curve[1]
     else:
         raise Exception( 'Unknown curve field' )
开发者ID:jesperborgstrup,项目名称:Py-EC,代码行数:29,代码来源:curve.py


示例6: get_pos_y_for_x

def get_pos_y_for_x(pubkey_x, yneg=0):
    key = pub_key = pub_key_x = pub_key_y = None
    try:
        key = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve('secp256k1'))
        group = OpenSSL.EC_KEY_get0_group(key)
        pub_key_x = OpenSSL.BN_bin2bn(pubkey_x, len(pubkey_x), 0)
        pub_key = OpenSSL.EC_POINT_new(group)

        if OpenSSL.EC_POINT_set_compressed_coordinates_GFp(group, pub_key,
                                                           pub_key_x, yneg, 0) == 0:
            raise Exception("[OpenSSL] EC_POINT_set_compressed_coordinates_GFp FAIL ... " + OpenSSL.get_error())


        pub_key_y = OpenSSL.BN_new()
        if (OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, pub_key,
                                                        pub_key_x,
                                                        pub_key_y, 0
                                                       )) == 0:
            raise Exception("[OpenSSL] EC_POINT_get_affine_coordinates_GFp FAIL ... " + OpenSSL.get_error())

        pubkeyy = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(pub_key_y))
        OpenSSL.BN_bn2bin(pub_key_y, pubkeyy)
        pubkeyy = pubkeyy.raw
        field_size = OpenSSL.EC_GROUP_get_degree(OpenSSL.EC_KEY_get0_group(key))
        secret_len = int((field_size + 7) / 8)
        if len(pubkeyy) < secret_len:
            pubkeyy = pubkeyy.rjust(secret_len, b'\0')
        return pubkeyy
    finally:
        if key is not None: OpenSSL.EC_KEY_free(key)
        if pub_key is not None: OpenSSL.EC_POINT_free(pub_key)
        if pub_key_x is not None: OpenSSL.BN_free(pub_key_x)
        if pub_key_y is not None: OpenSSL.BN_free(pub_key_y)
开发者ID:bitcredit-currency,项目名称:lightning,代码行数:33,代码来源:test_onion.py


示例7: update

 def update(self, input):
     i = OpenSSL.c_int(0)
     buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
     inp = OpenSSL.malloc(input, len(input))
     if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
                                 OpenSSL.byref(i), inp, len(input)) == 0:
         raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...")
     return buffer.raw[0:i.value]
开发者ID:0-vortex,项目名称:ZeroNet,代码行数:8,代码来源:cipher.py


示例8: verify

    def verify(self, sig, inputb):
        """
        Verify the signature with the input and the local public key.
        Returns a boolean
        """
        try:
            bsig = OpenSSL.malloc(sig, len(sig))
            binputb = OpenSSL.malloc(inputb, len(inputb))
            digest = OpenSSL.malloc(0, 64)
            dgst_len = OpenSSL.pointer(OpenSSL.c_int(0))
            md_ctx = OpenSSL.EVP_MD_CTX_create()

            key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)

            if key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")

            pub_key_x = OpenSSL.BN_bin2bn(self.pubkey_x, len(self.pubkey_x), 0)
            pub_key_y = OpenSSL.BN_bin2bn(self.pubkey_y, len(self.pubkey_y), 0)
            group = OpenSSL.EC_KEY_get0_group(key)
            pub_key = OpenSSL.EC_POINT_new(group)

            if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(group, pub_key,
                                                            pub_key_x,
                                                            pub_key_y,
                                                            0)) == 0:
                raise Exception(
                    "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
            if (OpenSSL.EC_KEY_set_public_key(key, pub_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
            if (OpenSSL.EC_KEY_check_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")

            OpenSSL.EVP_MD_CTX_init(md_ctx)
            OpenSSL.EVP_DigestInit(md_ctx, OpenSSL.EVP_ecdsa())
            if (OpenSSL.EVP_DigestUpdate(md_ctx, binputb, len(inputb))) == 0:
                raise Exception("[OpenSSL] EVP_DigestUpdate FAIL ...")

            OpenSSL.EVP_DigestFinal(md_ctx, digest, dgst_len)
            ret = OpenSSL.ECDSA_verify(
                0, digest, dgst_len.contents, bsig, len(sig), key)

            if ret == -1:
                return False  # Fail to Check
            else:
                if ret == 0:
                    return False  # Bad signature !
                else:
                    return True  # Good
            return False

        finally:
            OpenSSL.EC_KEY_free(key)
            OpenSSL.BN_free(pub_key_x)
            OpenSSL.BN_free(pub_key_y)
            OpenSSL.EC_POINT_free(pub_key)
            OpenSSL.EVP_MD_CTX_destroy(md_ctx)
开发者ID:AntonioReyes,项目名称:darkmarket,代码行数:57,代码来源:ecc.py


示例9: hmac_sha512

def hmac_sha512(k, m):
    """
    Compute the key and the message with HMAC SHA512
    """
    key = OpenSSL.malloc(k, len(k))
    d = OpenSSL.malloc(m, len(m))
    md = OpenSSL.malloc(0, 64)
    i = OpenSSL.pointer(OpenSSL.c_int(0))
    OpenSSL.HMAC(OpenSSL.EVP_sha512(), key, len(k), d, len(m), md, i)
    return md.raw
开发者ID:zxy9604,项目名称:dogemsg,代码行数:10,代码来源:hash.py


示例10: pbkdf2

def pbkdf2(password, salt=None, i=10000, keylen=64):
    if salt is None:
        salt = OpenSSL.rand(8)
    p_password = OpenSSL.malloc(password, len(password))
    p_salt = OpenSSL.malloc(salt, len(salt))
    output = OpenSSL.malloc(0, keylen)
    OpenSSL.PKCS5_PBKDF2_HMAC(p_password, len(password), p_salt,
                              len(p_salt), i, OpenSSL.EVP_sha256(),
                              keylen, output)
    return salt, output.raw
开发者ID:zxy9604,项目名称:dogemsg,代码行数:10,代码来源:hash.py


示例11: update

    def update(self, input):
        i = OpenSSL.c_int(0)
        buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
        inp = OpenSSL.malloc(input, len(input))
        if inp is None or buffer is None:
            raise Exception("Not enough memory")

        ret = OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
                                       OpenSSL.byref(i), inp, len(input))
        if ret == 0:
            raise Exception("[OpenSSL] EVP_CipherUpdate FAIL: " + str(ret))
        return buffer.raw[0:i.value]
开发者ID:blaa,项目名称:PyBitmessage,代码行数:12,代码来源:cipher.py


示例12: __init__

 def __init__(self, key, iv, do, ciphername='aes-256-cbc'):
     """
     do == 1 => Encrypt; do == 0 => Decrypt
     """
     self.cipher = OpenSSL.get_cipher(ciphername)
     self.ctx = OpenSSL.EVP_CIPHER_CTX_new()
     if do == 1 or do == 0:
         k = OpenSSL.malloc(key, len(key))
         IV = OpenSSL.malloc(iv, len(iv))
         OpenSSL.EVP_CipherInit_ex(
             self.ctx, self.cipher.get_pointer(), 0, k, IV, do)
     else:
         raise Exception("RTFM ...")
开发者ID:0-vortex,项目名称:ZeroNet,代码行数:13,代码来源:cipher.py


示例13: raw_encrypt

 def raw_encrypt(data, pubkey_x, pubkey_y, curve="sect283r1", ephemcurve=None, ciphername="aes-256-cbc"):
     if ephemcurve is None:
         ephemcurve = curve
     ephem = ECC(curve=ephemcurve)
     key = sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
     key_e, key_m = key[:32], key[32:]
     pubkey = ephem.get_pubkey()
     iv = OpenSSL.rand(OpenSSL.get_cipher(ciphername).get_blocksize())
     ctx = Cipher(key_e, iv, 1, ciphername)
     ciphertext = ctx.ciphering(data)
     # ciphertext = iv + pubkey + ctx.ciphering(data) # We will switch to this line after an upgrade period
     mac = hmac_sha256(key_m, ciphertext)
     return iv + pubkey + ciphertext + mac
开发者ID:JonathanCoe,项目名称:PyBitmessage,代码行数:13,代码来源:ecc.py


示例14: raw_encrypt

 def raw_encrypt(data, pubkey_x, pubkey_y, curve='sect283r1',
                 ephemcurve=None, ciphername='aes-256-cbc'):
     if ephemcurve is None:
         ephemcurve = curve
     ephem = ECC(curve=ephemcurve)
     key = sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
     key_e, key_m = key[:32], key[32:]
     pubkey = ephem.get_pubkey()
     iv = OpenSSL.rand(OpenSSL.get_cipher(ciphername).get_blocksize())
     ctx = Cipher(key_e, iv, 1, ciphername)
     ciphertext = ctx.ciphering(data)
     mac = hmac_sha256(key_m, ciphertext)
     return iv + pubkey + ciphertext + mac
开发者ID:AntonioReyes,项目名称:darkmarket,代码行数:13,代码来源:ecc.py


示例15: _encdec

    def _encdec(self, ciphername, msg=None, key=None, iv=None):
        "Helper: Encrypt, then decrypt random message"
        block_size = pyelliptic.Cipher.get_blocksize(ciphername)
        key_size = pyelliptic.Cipher.get_keysize(ciphername)

        # Generate IV, key and random message
        if key is None:
            key = OpenSSL.rand(key_size)
        if iv is None:
            iv = pyelliptic.Cipher.gen_IV(ciphername)
        if msg is None:
            msg = OpenSSL.rand(block_size)

        self.assertEqual(len(iv), block_size)
        self.assertEqual(len(key), key_size)
        self.assertEqual(len(msg), block_size)

        # Create ciphers
        enc_ctx = pyelliptic.Cipher(key=key, iv=iv,
                                    do=pyelliptic.Cipher.ENCRYPT,
                                    ciphername=ciphername,
                                    padding=False)

        dec_ctx = pyelliptic.Cipher(key, iv,
                                    pyelliptic.Cipher.DECRYPT,
                                    ciphername=ciphername,
                                    padding=False)


        # Encrypt with a bit mangled case.
        ciphertext = enc_ctx.update(msg[:10])
        ciphertext += enc_ctx.update('')
        ciphertext += enc_ctx.update(msg[10:])
        ciphertext += enc_ctx.update('')
        ciphertext += enc_ctx.final()

        self.assertEqual(len(msg), block_size)
        self.assertEqual(len(ciphertext), block_size)

        # Result must be of length n*blocksize
        self.assertEqual(len(ciphertext) % block_size, 0, msg="ciphertext has invalid length")
        self.assertEqual(len(ciphertext), len(msg),
                         msg="ciphertext length does not equal msg length and no padding is enabled")


        # Decrypt
        cleartext = dec_ctx.ciphering(ciphertext)

        self.assertEqual(msg, cleartext)
        self.assertNotEqual(msg, ciphertext)
        return msg, ciphertext
开发者ID:blaa,项目名称:PyBitmessage,代码行数:51,代码来源:tests.py


示例16: decrypt

 def decrypt(self, data, ciphername="aes-256-cbc"):
     """
     Decrypt data with ECIES method using the local private key
     """
     blocksize = OpenSSL.get_cipher(ciphername).get_blocksize()
     iv = data[:blocksize]
     i = blocksize
     curve, pubkey_x, pubkey_y, i2 = ECC._decode_pubkey(data[i:])
     i += i2
     ciphertext = data[i : len(data) - 32]
     i += len(ciphertext)
     mac = data[i:]
     key = sha512(self.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
     key_e, key_m = key[:32], key[32:]
     """
     pyelliptic was changed slightly so that the hmac covers the
     iv and pubkey. So let's have an upgrade period where we support
     both the old and the new hmac'ing algorithms.
     https://github.com/yann2192/pyelliptic/issues/17
     """
     if hmac_sha256(key_m, ciphertext) != mac:
         if hmac_sha256(key_m, data[: len(data) - 32]) != mac:
             raise RuntimeError("Fail to verify data")
     ctx = Cipher(key_e, iv, 0, ciphername)
     return ctx.ciphering(ciphertext)
开发者ID:JonathanCoe,项目名称:PyBitmessage,代码行数:25,代码来源:ecc.py


示例17: sign

    def sign(self, inputb):
        """
        Sign the input with ECDSA method and returns the signature
        """
        try:
            size = len(inputb)
            buff = OpenSSL.malloc(inputb, size)
            digest = OpenSSL.malloc(0, 64)
            md_ctx = OpenSSL.EVP_MD_CTX_create()
            dgst_len = OpenSSL.pointer(OpenSSL.c_int(0))
            siglen = OpenSSL.pointer(OpenSSL.c_int(0))
            sig = OpenSSL.malloc(0, 151)

            key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
            if key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")

            priv_key = OpenSSL.BN_bin2bn(self.privkey, len(self.privkey), 0)
            pub_key_x = OpenSSL.BN_bin2bn(self.pubkey_x, len(self.pubkey_x), 0)
            pub_key_y = OpenSSL.BN_bin2bn(self.pubkey_y, len(self.pubkey_y), 0)

            if (OpenSSL.EC_KEY_set_private_key(key, priv_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_private_key FAIL ...")

            group = OpenSSL.EC_KEY_get0_group(key)
            pub_key = OpenSSL.EC_POINT_new(group)

            if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(group, pub_key,
                                                            pub_key_x,
                                                            pub_key_y,
                                                            0)) == 0:
                raise Exception(
                    "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
            if (OpenSSL.EC_KEY_set_public_key(key, pub_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
            if (OpenSSL.EC_KEY_check_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")

            OpenSSL.EVP_MD_CTX_init(md_ctx)
            OpenSSL.EVP_DigestInit(md_ctx, OpenSSL.EVP_ecdsa())

            if (OpenSSL.EVP_DigestUpdate(md_ctx, buff, size)) == 0:
                raise Exception("[OpenSSL] EVP_DigestUpdate FAIL ...")
            OpenSSL.EVP_DigestFinal(md_ctx, digest, dgst_len)
            OpenSSL.ECDSA_sign(0, digest, dgst_len.contents, sig, siglen, key)
            if (OpenSSL.ECDSA_verify(0, digest, dgst_len.contents, sig,
                                     siglen.contents, key)) != 1:
                raise Exception("[OpenSSL] ECDSA_verify FAIL ...")

            return sig.raw[0:siglen.contents.value]

        finally:
            OpenSSL.EC_KEY_free(key)
            OpenSSL.BN_free(pub_key_x)
            OpenSSL.BN_free(pub_key_y)
            OpenSSL.BN_free(priv_key)
            OpenSSL.EC_POINT_free(pub_key)
            OpenSSL.EVP_MD_CTX_destroy(md_ctx)
开发者ID:AntonioReyes,项目名称:darkmarket,代码行数:58,代码来源:ecc.py


示例18: __init__

    def __init__(self, key, iv, do, ciphername='aes-256-cbc', padding=True):
        """
        do == 1 => Encrypt; do == 0 => Decrypt
        """
        assert do in [self.ENCRYPT, self.DECRYPT], "Argument 'do' out of scope"
        self.cipher = OpenSSL.get_cipher(ciphername)
        self.ctx = OpenSSL.EVP_CIPHER_CTX_new()

        keysize = self.cipher.get_keysize()
        assert keysize is None or len(key) == keysize
        assert len(iv) == self.cipher.get_blocksize()

        k = OpenSSL.malloc(key, len(key))
        IV = OpenSSL.malloc(iv, len(iv))
        OpenSSL.EVP_CipherInit_ex(
            self.ctx, self.cipher.get_pointer(), 0, k, IV, do)

        if padding is False:
            # By default PKCS padding is enabled. This case disables it.
            OpenSSL.EVP_CIPHER_CTX_set_padding(self.ctx, 0)
开发者ID:blaa,项目名称:PyBitmessage,代码行数:20,代码来源:cipher.py


示例19: raw_get_ecdh_key

    def raw_get_ecdh_key(self, pubkey_x, pubkey_y):
        try:
            ecdh_keybuffer = OpenSSL.malloc(0, 32)

            other_key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
            if other_key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")

            other_pub_key_x = OpenSSL.BN_bin2bn(pubkey_x, len(pubkey_x), 0)
            other_pub_key_y = OpenSSL.BN_bin2bn(pubkey_y, len(pubkey_y), 0)

            other_group = OpenSSL.EC_KEY_get0_group(other_key)
            other_pub_key = OpenSSL.EC_POINT_new(other_group)

            if (OpenSSL.EC_POINT_set_affine_coordinates_GFp(other_group,
                                                            other_pub_key,
                                                            other_pub_key_x,
                                                            other_pub_key_y,
                                                            0)) == 0:
                raise Exception(
                    "[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
            if (OpenSSL.EC_KEY_set_public_key(other_key, other_pub_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
            if (OpenSSL.EC_KEY_check_key(other_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")

            own_key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
            if own_key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
            own_priv_key = OpenSSL.BN_bin2bn(
                self.privkey, len(self.privkey), 0)

            if (OpenSSL.EC_KEY_set_private_key(own_key, own_priv_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_private_key FAIL ...")

            if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
                OpenSSL.EC_KEY_set_method(own_key, OpenSSL.EC_KEY_OpenSSL())
            else:
                OpenSSL.ECDH_set_method(own_key, OpenSSL.ECDH_OpenSSL())
            ecdh_keylen = OpenSSL.ECDH_compute_key(
                ecdh_keybuffer, 32, other_pub_key, own_key, 0)

            if ecdh_keylen != 32:
                raise Exception("[OpenSSL] ECDH keylen FAIL ...")

            return ecdh_keybuffer.raw

        finally:
            OpenSSL.EC_KEY_free(other_key)
            OpenSSL.BN_free(other_pub_key_x)
            OpenSSL.BN_free(other_pub_key_y)
            OpenSSL.EC_POINT_free(other_pub_key)
            OpenSSL.EC_KEY_free(own_key)
            OpenSSL.BN_free(own_priv_key)
开发者ID:0-vortex,项目名称:ZeroNet,代码行数:54,代码来源:ecc.py


示例20: _generate

    def _generate(self):
        try:
            pub_key_x = OpenSSL.BN_new()
            pub_key_y = OpenSSL.BN_new()

            key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
            if key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
            if (OpenSSL.EC_KEY_generate_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_generate_key FAIL ...")
            if (OpenSSL.EC_KEY_check_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")
            priv_key = OpenSSL.EC_KEY_get0_private_key(key)

            group = OpenSSL.EC_KEY_get0_group(key)
            pub_key = OpenSSL.EC_KEY_get0_public_key(key)

            if (OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, pub_key,
                                                            pub_key_x,
                                                            pub_key_y, 0
                                                            )) == 0:
                raise Exception(
                    "[OpenSSL] EC_POINT_get_affine_coordinates_GFp FAIL ...")

            privkey = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(priv_key))
            pubkeyx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(pub_key_x))
            pubkeyy = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(pub_key_y))
            OpenSSL.BN_bn2bin(priv_key, privkey)
            privkey = privkey.raw
            OpenSSL.BN_bn2bin(pub_key_x, pubkeyx)
            pubkeyx = pubkeyx.raw
            OpenSSL.BN_bn2bin(pub_key_y, pubkeyy)
            pubkeyy = pubkeyy.raw
            self.raw_check_key(privkey, pubkeyx, pubkeyy)

            return privkey, pubkeyx, pubkeyy

        finally:
            OpenSSL.EC_KEY_free(key)
            OpenSSL.BN_free(pub_key_x)
            OpenSSL.BN_free(pub_key_y)
开发者ID:AntonioReyes,项目名称:darkmarket,代码行数:41,代码来源:ecc.py



注:本文中的pyelliptic.openssl.OpenSSL类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pyeloqua.Bulk类代码示例发布时间:2022-05-25
下一篇:
Python hwsimlog.debug函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap