当前位置: 首页>>代码示例>>Python>>正文


Python api.new函数代码示例

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


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

示例1: initialise

    def initialise(self, key, ivector):
        """Initialise this cipher's state with a key and optional ivector

        The key must be a byte string of the same length as this cipher's
        key_len property. If the ivector is required, it must also be a byte
        string of ivector_len length. If not required it may be an empty string
        or None.
        """
        if self._ctx == api.NULL:
            raise ValueError("Cipher object failed to be initialised")
        if len(key) != self.key_len:
            msg = "Key must be {0} bytes. Received {1}".format(
                    self.key_len, len(key))
            raise ValueError(msg)
        c_key = api.new('char[]', key) if bool(key) else api.NULL
        if (len(ivector) if ivector is not None else 0) != self.ivector_len:
            msg = "IVector must be {0} bytes. Received{1}".format(
                    self.ivector_len, len(ivector))
            raise ValueError(msg)
        c_iv = api.new('char[]', ivector) if bool(ivector) else api.NULL
        if not api.EVP_CipherInit_ex(self._ctx,
                api.NULL, api.NULL, c_key, c_iv, -1):
            raise ValueError("Unable to initialise cipher")
        if self.digest is not None:
            self._hmac = hmac.HMAC(key, digestmod=self.digest)
        self._initialised = True
开发者ID:exarkun,项目名称:opentls,代码行数:26,代码来源:cipherlib.py

示例2: set_cipher

 def set_cipher(self, enc):
     cipher = api.EVP_get_cipherbyname(self.algorithm)
     key = api.new('unsigned char[]', self.hexstr_to_numbers(self.key))
     iv = api.NULL
     if self.iv is not None:
         iv = api.new('unsigned char[]', self.hexstr_to_numbers(self.iv))
     api.BIO_set_cipher(self.filter, cipher, key, iv, 1 if enc else 0)
开发者ID:exarkun,项目名称:opentls,代码行数:7,代码来源:test_bio_filter.py

示例3: __init__

    def __init__(self, key, msg=None, digestmod=None):
        """Create a new HMAC object.

        key:       key for the keyed hash object.
        msg:       Initial input for the hash, if provided.
        digestmod: A message digest name. *OR*
                   A module supporting PEP 247.  *OR*
                   A hashlib constructor returning a new hash object.

        If module or hashlib constuctor is passed as digestmod the '__name__'
        and 'args' attributes are searched to find a message digest name. If
        not provied the digestmod defaults to 'md5'.

        Note: key and msg must be a bytes objects.
        """
        if digestmod is None:
            self._md = api.EVP_md5()
        else:
            self._md = self._get_md(digestmod)
        ctx = api.new('HMAC_CTX*')
        self._key = api.new('char[]', key)
        api.HMAC_Init_ex(ctx, api.cast('void*', self._key),
                len(key), self._md, api.NULL)
        cleanup = lambda _: api.HMAC_CTX_cleanup(ctx)
        self._weakref = weakref.ref(self, cleanup)
        self._ctx = ctx
        if msg is not None:
            self.update(msg)
开发者ID:joao-gi,项目名称:opentls,代码行数:28,代码来源:hmac.py

示例4: seed

    def seed(self, state, version=2, entropy=None):
        """Initialize internal state from hashable object.

        None or no argument seeds from current time or from an operating
        system specific randomness source if available.

        For version 2 (the default), all of the bits are used if *a *is a str,
        bytes, or bytearray.  For version 1, the hash() of *a* is used
        instead.

        If *a* is an int, all bits are used.
        """
        if state is None:
            if not api.RAND_status():
                while not api.RAND_status():
                    data = api.new('unsigned char[]', os.urandom(256))
                    api.RAND_seed(data, len(data))
            return
        elif isinstance(state, (str, bytes, bytearray)):
            if version > 1:
                if isinstance(state, str):
                    state = state.encode()
                data = api.new('unsigned char[]', state)
            else:
                state = hash(state)
                data = self._int_to_ubyte(state)
        else:
            data = self._int_to_ubyte(state)
        entropy = entropy if entropy is not None else 8 * len(data)
        api.RAND_add(data, len(data), entropy)
开发者ID:exarkun,项目名称:opentls,代码行数:30,代码来源:random.py

示例5: test_long

 def test_long(self):
     data = api.new('char[]', self.data_long)
     buf = api.new('unsigned char[]', api.EVP_MAX_MD_SIZE)
     size = api.new('unsigned int*')
     api.EVP_DigestUpdate(self.ctx, api.cast('void*', data), len(self.data_long))
     api.EVP_DigestFinal_ex(self.ctx, buf, size)
     hash_value = b''.join('{0:02x}'.format(v).encode() for v in islice(buf, size[0]))
     self.assertEqual(hash_value, self.hash_long)
开发者ID:jschoe71,项目名称:opentls,代码行数:8,代码来源:test_evp_md.py

示例6: set_mode

 def set_mode(self, enc):
     mode = 1 if enc else 0
     key = api.new('unsigned char[]', self.hexstr_to_numbers(self.key))
     iv = api.NULL
     if self.iv is not None:
         iv = api.new('unsigned char[]', self.hexstr_to_numbers(self.iv))
     api.EVP_CipherInit_ex(self.ctx, api.NULL, api.NULL, key, iv, mode)
     if not enc:
         api.EVP_CIPHER_CTX_set_padding(self.ctx, 0)
开发者ID:joao-gi,项目名称:opentls,代码行数:9,代码来源:test_evp_cipher.py

示例7: test_filter_write

 def test_filter_write(self):
     self.set_cipher(enc=True)
     plaintext = api.new('unsigned char[]', self.hexstr_to_numbers(self.plaintext))
     ciphertext = api.new('unsigned char[]', self.hexstr_to_numbers(self.ciphertext))
     output = api.new('unsigned char[]', len(ciphertext))
     api.BIO_write(self.bio, plaintext, len(plaintext))
     api.BIO_flush(self.bio)
     api.BIO_read(self.sink, output, len(output))
     self.assertEqual(api.buffer(ciphertext)[:], api.buffer(output)[:])
开发者ID:exarkun,项目名称:opentls,代码行数:9,代码来源:test_bio_filter.py

示例8: test_quick

 def test_quick(self):
     buff = api.new('unsigned char[]', api.EVP_MAX_MD_SIZE)
     key = api.new('char[]', self.key)
     data = api.new('char[]', self.data)
     size = api.new('unsigned int*')
     api.HMAC(self.md,
             api.cast('void*', key), len(self.key),
             api.cast('void*', data), len(self.data),
             api.cast('void*', buff), size)
     self.assertEqual(self.digest, api.buffer(buff, size[0])[:])
开发者ID:jschoe71,项目名称:opentls,代码行数:10,代码来源:test_hmac.py

示例9: test_filter_read

 def test_filter_read(self):
     self.set_cipher(enc=False)
     plaintext = api.new('unsigned char[]', self.hexstr_to_numbers(self.plaintext))
     ciphertext = api.new('unsigned char[]', self.hexstr_to_numbers(self.ciphertext))
     output = api.new('unsigned char[]', len(plaintext))
     api.BIO_write(self.bio, ciphertext, len(ciphertext))
     api.BIO_write(self.bio, api.new('unsigned char[]', [0] * 1), 1)
     api.BIO_flush(self.bio)
     api.BIO_read(self.sink, output, len(output))
     self.assertEqual(bytes(api.buffer(plaintext)), bytes(api.buffer(output)))
开发者ID:joao-gi,项目名称:opentls,代码行数:10,代码来源:test_bio_filter.py

示例10: test_4096_2_iteration

 def test_4096_2_iteration(self):
     key = b'\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38'
     password = api.new('char[]', b'passwordPASSWORDpassword')
     passlen = 24
     salt = api.new('char[]', b'saltSALTsaltSALTsaltSALTsaltSALTsalt')
     saltlen = 36
     iterations = 4096
     keylen = 25
     out = api.new('unsigned char[]', keylen)
     api.PKCS5_PBKDF2_HMAC_SHA1(password, passlen, salt, saltlen, iterations, keylen, out)
     self.assertEqual(key, api.buffer(out)[:])
开发者ID:exarkun,项目名称:opentls,代码行数:11,代码来源:test_pkcs5.py

示例11: test_4096_iteration

 def test_4096_iteration(self):
     key = b'\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1'
     password = api.new('char[]', b'password')
     passlen = 8
     salt = api.new('char[]', b'salt')
     saltlen = 4
     iterations = 4096
     keylen = 20
     out = api.new('unsigned char[]', keylen)
     api.PKCS5_PBKDF2_HMAC_SHA1(password, passlen, salt, saltlen, iterations, keylen, out)
     self.assertEqual(key, api.buffer(out)[:])
开发者ID:exarkun,项目名称:opentls,代码行数:11,代码来源:test_pkcs5.py

示例12: test_0002_iteration

 def test_0002_iteration(self):
     key = b'\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57'
     password = api.new('char[]', b'password')
     passlen = 8
     salt = api.new('char[]', b'salt')
     saltlen = 4
     iterations = 2
     keylen = 20
     out = api.new('unsigned char[]', keylen)
     api.PKCS5_PBKDF2_HMAC_SHA1(password, passlen, salt, saltlen, iterations, keylen, out)
     self.assertEqual(key, api.buffer(out)[:])
开发者ID:exarkun,项目名称:opentls,代码行数:11,代码来源:test_pkcs5.py

示例13: test_0001_iteration

 def test_0001_iteration(self):
     key = b'\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6'
     password = api.new('char[]', b'password')
     passlen = 8
     salt = api.new('char[]', b'salt')
     saltlen = 4
     iterations = 1
     keylen = 20
     out = api.new('unsigned char[]', keylen)
     api.PKCS5_PBKDF2_HMAC_SHA1(password, passlen, salt, saltlen, iterations, keylen, out)
     self.assertEqual(key, api.buffer(out)[:])
开发者ID:exarkun,项目名称:opentls,代码行数:11,代码来源:test_pkcs5.py

示例14: pbkdf2

def pbkdf2(password, length, salt=None, iterations=1000):
    "Derive a shared secret including encryption key from password"
    if salt is None:
        salt = random.getrandbytes(8)
    c_password = api.new('char[]', password)
    c_salt = api.new('char[]', salt)
    c_key = api.new('unsigned char[]', length)
    api.PKCS5_PBKDF2_HMAC_SHA1(c_password, len(password),
        c_salt, len(salt), iterations, length, c_key)
    secret = Secret(bytes(api.buffer(c_key)), salt, iterations)
    return secret
开发者ID:joao-gi,项目名称:opentls,代码行数:11,代码来源:kdf.py

示例15: test_single_decrypt

 def test_single_decrypt(self):
     self.set_mode(enc=False)
     plaintext = api.new('unsigned char[]', self.hexstr_to_numbers(self.plaintext))
     ciphertext = api.new('unsigned char[]', self.hexstr_to_numbers(self.ciphertext))
     output = api.new('unsigned char[]', len(ciphertext)
             + api.EVP_CIPHER_CTX_block_size(self.ctx) - 1)
     outlen = api.new('int*')
     api.EVP_CipherUpdate(self.ctx, output, outlen, ciphertext, len(ciphertext))
     self.assertEqual(api.buffer(plaintext), api.buffer(output, outlen[0]))
     rval = api.EVP_CipherFinal_ex(self.ctx, output, outlen)
     self.assertEqual(rval, 1)
开发者ID:joao-gi,项目名称:opentls,代码行数:11,代码来源:test_evp_cipher.py


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