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


Python Random.new方法代码示例

本文整理汇总了Python中Cryptodome.Random.new方法的典型用法代码示例。如果您正苦于以下问题:Python Random.new方法的具体用法?Python Random.new怎么用?Python Random.new使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cryptodome.Random的用法示例。


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

示例1: encrypt_credential

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def encrypt_credential(raw):
    """
    Encodes data

    :param data: Data to be encoded
    :type data: str
    :returns:  string -- Encoded data
    """
    # pylint: disable=invalid-name,import-error
    import base64
    try:  # The crypto package depends on the library installed (see Wiki)
        from Crypto import Random
        from Crypto.Cipher import AES
        from Crypto.Util import Padding
    except ImportError:
        from Cryptodome import Random
        from Cryptodome.Cipher import AES
        from Cryptodome.Util import Padding
    raw = bytes(Padding.pad(data_to_pad=raw.encode('utf-8'), block_size=__BLOCK_SIZE__))
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(get_crypt_key(), AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8') 
开发者ID:CastagnaIT,项目名称:plugin.video.netflix,代码行数:24,代码来源:credentials.py

示例2: decrypt_credential

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def decrypt_credential(enc, secret=None):
    """
    Decodes data

    :param data: Data to be decoded
    :type data: str
    :returns:  string -- Decoded data
    """
    # pylint: disable=invalid-name,import-error
    import base64
    try:  # The crypto package depends on the library installed (see Wiki)
        from Crypto.Cipher import AES
        from Crypto.Util import Padding
    except ImportError:
        from Cryptodome.Cipher import AES
        from Cryptodome.Util import Padding
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(secret or get_crypt_key(), AES.MODE_CBC, iv)
    decoded = Padding.unpad(
        padded_data=cipher.decrypt(enc[AES.block_size:]),
        block_size=__BLOCK_SIZE__)
    return decoded 
开发者ID:CastagnaIT,项目名称:plugin.video.netflix,代码行数:25,代码来源:credentials.py

示例3: getRandomInteger

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def getRandomInteger(N, randfunc=None):
    """getRandomInteger(N:int, randfunc:callable):long
    Return a random number with at most N bits.

    If randfunc is omitted, then Random.new().read is used.

    This function is for internal use only and may be renamed or removed in
    the future.
    """
    if randfunc is None:
        _import_Random()
        randfunc = Random.new().read

    S = randfunc(N>>3)
    odd_bits = N % 8
    if odd_bits != 0:
        char = ord(randfunc(1)) >> (8-odd_bits)
        S = bchr(char) + S
    value = bytes_to_long(S)
    return value 
开发者ID:haynieresearch,项目名称:jarvis,代码行数:22,代码来源:number.py

示例4: testEncrypt1

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def testEncrypt1(self):
                # Verify encryption using all test vectors
                for test in self._testData:
                        # Build the key
                        comps = [ int(rws(test[0][x]),16) for x in ('n','e') ]
                        key = RSA.construct(comps)
                        # RNG that takes its random numbers from a pool given
                        # at initialization
                        class randGen:
                            def __init__(self, data):
                                self.data = data
                                self.idx = 0
                            def __call__(self, N):
                                r = self.data[self.idx:N]
                                self.idx += N
                                return r
                        # The real test
                        cipher = PKCS.new(key, test[4], randfunc=randGen(t2b(test[3])))
                        ct = cipher.encrypt(t2b(test[1]))
                        self.assertEqual(ct, t2b(test[2])) 
开发者ID:haynieresearch,项目名称:jarvis,代码行数:22,代码来源:test_pkcs1_oaep.py

示例5: testEncryptDecrypt2

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def testEncryptDecrypt2(self):
                # Helper function to monitor what's requested from RNG
                global asked
                def localRng(N):
                    global asked
                    asked += N
                    return self.rng(N)
                # Verify that OAEP is friendly to all hashes
                for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD160):
                    # Verify that encrypt() asks for as many random bytes
                    # as the hash output size
                    asked = 0
                    pt = self.rng(40)
                    cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
                    ct = cipher.encrypt(pt)
                    self.assertEqual(cipher.decrypt(ct), pt)
                    self.assertEqual(asked, hashmod.digest_size) 
开发者ID:haynieresearch,项目名称:jarvis,代码行数:19,代码来源:test_pkcs1_oaep.py

示例6: testEncrypt1

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def testEncrypt1(self):
                for test in self._testData:
                        # Build the key
                        key = RSA.importKey(test[0])
                        # RNG that takes its random numbers from a pool given
                        # at initialization
                        class randGen:
                            def __init__(self, data):
                                self.data = data
                                self.idx = 0
                            def __call__(self, N):
                                r = self.data[self.idx:self.idx+N]
                                self.idx += N
                                return r
                        # The real test
                        cipher = PKCS.new(key, randfunc=randGen(t2b(test[3])))
                        ct = cipher.encrypt(b(test[1]))
                        self.assertEqual(ct, t2b(test[2])) 
开发者ID:haynieresearch,项目名称:jarvis,代码行数:20,代码来源:test_pkcs1_15.py

示例7: generate_iv

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def generate_iv() -> bytes:
        return Random.new().read(AES.block_size) 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:4,代码来源:aes_handler.py

示例8: aes_gcm_encrypt_with_iv

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_gcm_encrypt_with_iv(plain_text: bytes, hdr: bytes, key: bytes, iv: bytes):
        cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=iv)
        cipher.update(hdr)
        cipher_text, mac_tag = cipher.encrypt_and_digest(plain_text)
        return mac_tag, cipher_text 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:7,代码来源:aes_handler.py

示例9: aes_gcm_decrypt_with_iv

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_gcm_decrypt_with_iv(cipher_text: bytes, hdr: bytes, mac_tag: bytes, key: bytes, iv: bytes) -> bytes:
        cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=iv)
        cipher.update(hdr)
        try:
            plain_text = cipher.decrypt_and_verify(cipher_text, mac_tag)
        except ValueError:
            plain_text = b""
        except KeyError:
            plain_text = b""
        return plain_text 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:12,代码来源:aes_handler.py

示例10: aes_gcm_encrypt

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_gcm_encrypt(plain_text: bytes, hdr: bytes, key: bytes):
        cipher = AES.new(key=key, mode=AES.MODE_GCM)
        cipher.update(hdr)
        cipher_text, mac_tag = cipher.encrypt_and_digest(plain_text)
        nonce = cipher.nonce
        return nonce, mac_tag, cipher_text 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:8,代码来源:aes_handler.py

示例11: aes_gcm_decrypt

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_gcm_decrypt(cipher_text: bytes, hdr: bytes, nonce: bytes, mac_tag: bytes, key: bytes):
        cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=nonce)
        cipher.update(hdr)
        try:
            plain_text = cipher.decrypt_and_verify(cipher_text, mac_tag)
        except ValueError:
            plain_text = b""
        except KeyError:
            plain_text = b""
        return plain_text 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:12,代码来源:aes_handler.py

示例12: aes_ctr_decrypt

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_ctr_decrypt(cipher_text: bytes, nonce: bytes, key: bytes):
        cipher = AES.new(key=key, mode=AES.MODE_CTR, nonce=nonce)
        plain_text = cipher.decrypt(cipher_text)
        return plain_text 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:6,代码来源:aes_handler.py

示例13: aes_cbc_encrypt

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_cbc_encrypt(plain_text: bytes, key: bytes, iv: bytes = b''):
        if len(iv) == 0:
            iv = AESHandler.generate_iv()
        cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
        return cipher.IV, cipher.encrypt(pad(plain_text, AES.block_size)) 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:7,代码来源:aes_handler.py

示例14: aes_cbc_decrypt

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def aes_cbc_decrypt(cipher_text: bytes, iv: bytes, key: bytes):
        cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
        return unpad(cipher.decrypt(cipher_text), AES.block_size, style='pkcs7') 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:5,代码来源:aes_handler.py

示例15: test_aes_gcm_with_iv

# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import new [as 别名]
def test_aes_gcm_with_iv(self):
        key = b'Sixteen byte key'
        plain_text = b'Attack at dawn'
        hdr = b'To your eyes only'
        iv = Random.new().read(AES.block_size)
        mac, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(plain_text, hdr, key, iv)
        decrypt_out = AESHandler.aes_gcm_decrypt_with_iv(cipher_text, hdr, mac, key, iv)
        self.assertEqual(plain_text, decrypt_out) 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:10,代码来源:test_aes_handler.py


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