當前位置: 首頁>>代碼示例>>Python>>正文


Python aead.AESCCM屬性代碼示例

本文整理匯總了Python中cryptography.hazmat.primitives.ciphers.aead.AESCCM屬性的典型用法代碼示例。如果您正苦於以下問題:Python aead.AESCCM屬性的具體用法?Python aead.AESCCM怎麽用?Python aead.AESCCM使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在cryptography.hazmat.primitives.ciphers.aead的用法示例。


在下文中一共展示了aead.AESCCM屬性的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _encrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _encrypt(backend, cipher, nonce, data, associated_data, tag_length):
    from cryptography.hazmat.primitives.ciphers.aead import AESCCM
    cipher_name = _aead_cipher_name(cipher)
    ctx = _aead_setup(
        backend, cipher_name, cipher._key, nonce, None, tag_length, _ENCRYPT
    )
    # CCM requires us to pass the length of the data before processing anything
    # However calling this with any other AEAD results in an error
    if isinstance(cipher, AESCCM):
        _set_length(backend, ctx, len(data))

    _process_aad(backend, ctx, associated_data)
    processed_data = _process_data(backend, ctx, data)
    outlen = backend._ffi.new("int *")
    res = backend._lib.EVP_CipherFinal_ex(ctx, backend._ffi.NULL, outlen)
    backend.openssl_assert(res != 0)
    backend.openssl_assert(outlen[0] == 0)
    tag_buf = backend._ffi.new("unsigned char[]", tag_length)
    res = backend._lib.EVP_CIPHER_CTX_ctrl(
        ctx, backend._lib.EVP_CTRL_AEAD_GET_TAG, tag_length, tag_buf
    )
    backend.openssl_assert(res != 0)
    tag = backend._ffi.buffer(tag_buf)[:]

    return processed_data + tag 
開發者ID:tp4a,項目名稱:teleport,代碼行數:27,代碼來源:aead.py

示例2: Encrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def Encrypt(key_ccm, nonce, data, aad):
    aesccm = AESCCM(key_ccm, 8)
    return aesccm.encrypt(nonce, data, aad) 
開發者ID:robertmuth,項目名稱:PyZwaver,代碼行數:5,代碼來源:security.py

示例3: _aead_cipher_name

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _aead_cipher_name(cipher):
    from cryptography.hazmat.primitives.ciphers.aead import (
        AESCCM, AESGCM, ChaCha20Poly1305
    )
    if isinstance(cipher, ChaCha20Poly1305):
        return b"chacha20-poly1305"
    elif isinstance(cipher, AESCCM):
        return "aes-{0}-ccm".format(len(cipher._key) * 8).encode("ascii")
    else:
        assert isinstance(cipher, AESGCM)
        return "aes-{0}-gcm".format(len(cipher._key) * 8).encode("ascii") 
開發者ID:tp4a,項目名稱:teleport,代碼行數:13,代碼來源:aead.py

示例4: _decrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _decrypt(backend, cipher, nonce, data, associated_data, tag_length):
    from cryptography.hazmat.primitives.ciphers.aead import AESCCM
    if len(data) < tag_length:
        raise InvalidTag
    tag = data[-tag_length:]
    data = data[:-tag_length]
    cipher_name = _aead_cipher_name(cipher)
    ctx = _aead_setup(
        backend, cipher_name, cipher._key, nonce, tag, tag_length, _DECRYPT
    )
    # CCM requires us to pass the length of the data before processing anything
    # However calling this with any other AEAD results in an error
    if isinstance(cipher, AESCCM):
        _set_length(backend, ctx, len(data))

    _process_aad(backend, ctx, associated_data)
    # CCM has a different error path if the tag doesn't match. Errors are
    # raised in Update and Final is irrelevant.
    if isinstance(cipher, AESCCM):
        outlen = backend._ffi.new("int *")
        buf = backend._ffi.new("unsigned char[]", len(data))
        res = backend._lib.EVP_CipherUpdate(ctx, buf, outlen, data, len(data))
        if res != 1:
            backend._consume_errors()
            raise InvalidTag

        processed_data = backend._ffi.buffer(buf, outlen[0])[:]
    else:
        processed_data = _process_data(backend, ctx, data)
        outlen = backend._ffi.new("int *")
        res = backend._lib.EVP_CipherFinal_ex(ctx, backend._ffi.NULL, outlen)
        if res == 0:
            backend._consume_errors()
            raise InvalidTag

    return processed_data 
開發者ID:tp4a,項目名稱:teleport,代碼行數:38,代碼來源:aead.py

示例5: _aead_cipher_name

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _aead_cipher_name(cipher):
    from cryptography.hazmat.primitives.ciphers.aead import (
        AESCCM, AESGCM, ChaCha20Poly1305
    )
    if isinstance(cipher, ChaCha20Poly1305):
        return b"chacha20-poly1305"
    elif isinstance(cipher, AESCCM):
        return "aes-{}-ccm".format(len(cipher._key) * 8).encode("ascii")
    else:
        assert isinstance(cipher, AESGCM)
        return "aes-{}-gcm".format(len(cipher._key) * 8).encode("ascii") 
開發者ID:tp4a,項目名稱:teleport,代碼行數:13,代碼來源:aead.py

示例6: _encrypt_ccm

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _encrypt_ccm(secret_key, value, field_max_length=None):
    aesccm = AESCCM(secret_key)
    nonce = os.urandom(AES_CCM_NONCE_LENGTH)
    ct = aesccm.encrypt(nonce, value.encode("utf-8"), None)
    encrypted = base64.b64encode(nonce + ct).decode("utf-8")
    if field_max_length:
        msg = "Tried to encode a value too large for this field"
        assert (len(encrypted) + _RESERVED_FIELD_SPACE) <= field_max_length, msg

    return encrypted 
開發者ID:quay,項目名稱:quay,代碼行數:12,代碼來源:encryption.py

示例7: _decrypt_ccm

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import aead [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESCCM [as 別名]
def _decrypt_ccm(secret_key, value):
    aesccm = AESCCM(secret_key)
    try:
        decoded = base64.b64decode(value)
        nonce = decoded[:AES_CCM_NONCE_LENGTH]
        ct = decoded[AES_CCM_NONCE_LENGTH:]
        decrypted = aesccm.decrypt(nonce, ct, None)
        return decrypted.decode("utf-8")
    except Exception:
        logger.exception("Got exception when trying to decrypt value `%s`", value)
        raise DecryptionFailureException()


# Defines the versions of encryptions we support. This will allow us to upgrade to newer encryption
# protocols (fairly seamlessly) if need be in the future. 
開發者ID:quay,項目名稱:quay,代碼行數:17,代碼來源:encryption.py


注:本文中的cryptography.hazmat.primitives.ciphers.aead.AESCCM屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。