本文整理汇总了Python中cryptography.hazmat.primitives.ciphers.aead.AESGCM属性的典型用法代码示例。如果您正苦于以下问题:Python aead.AESGCM属性的具体用法?Python aead.AESGCM怎么用?Python aead.AESGCM使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cryptography.hazmat.primitives.ciphers.aead
的用法示例。
在下文中一共展示了aead.AESGCM属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: finish_desktop_flow
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def finish_desktop_flow(request: HttpRequest, user_profile: UserProfile,
otp: str) -> HttpResponse:
"""
The desktop otp flow returns to the app (through the clipboard)
a token that allows obtaining (through log_into_subdomain) a logged in session
for the user account we authenticated in this flow.
The token can only be used once and within ExternalAuthResult.LOGIN_KEY_EXPIRATION_SECONDS
of being created, as nothing more powerful is needed for the desktop flow
and this ensures the key can only be used for completing this authentication attempt.
"""
result = ExternalAuthResult(user_profile=user_profile)
token = result.store_data()
key = bytes.fromhex(otp)
iv = os.urandom(12)
desktop_data = (iv + AESGCM(key).encrypt(iv, token.encode(), b"")).hex()
context = {'desktop_data': desktop_data,
'browser_url': reverse('zerver.views.auth.login_page',
kwargs = {'template_name': 'zerver/login.html'}),
'realm_icon_url': realm_icon_url(user_profile.realm)}
return render(request, 'zerver/desktop_redirect.html', context=context)
示例2: get_encrypted
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def get_encrypted(content: str, key: str) -> str:
"""
Encrypt content using a specified key
"""
def create_nonce() -> bytes:
return os.urandom(12)
def encrypt(string: str, enc_key: str) -> bytes:
# String to bytes
enc_key = base64.b64decode(enc_key)
# Create key
aes_gcm = AESGCM(enc_key)
# Create nonce
nonce = create_nonce()
# Create ciphered data
data = string.encode()
ct = aes_gcm.encrypt(nonce, data, None)
return base64.b64encode(nonce + ct)
now = epoch_seconds()
encrypted = encrypt(f'{now}:{content}', key).decode('utf-8')
return encrypted
示例3: enc
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def enc(self, byts, asscd=None):
'''
Encrypt the given bytes and return an envelope dict in msgpack form.
Args:
byts (bytes): The message to be encrypted.
asscd (bytes): Extra data that needs to be authenticated (but not encrypted).
Returns:
bytes: The encrypted message. This is a msgpacked dictionary
containing the IV, ciphertext, and associated data.
'''
iv = os.urandom(16)
encryptor = AESGCM(self.ekey)
byts = encryptor.encrypt(iv, byts, asscd)
envl = {'iv': iv, 'data': byts, 'asscd': asscd}
return s_msgpack.en(envl)
示例4: dec
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def dec(self, byts):
'''
Decode an envelope dict and decrypt the given bytes.
Args:
byts (bytes): Bytes to decrypt.
Returns:
bytes: Decrypted message.
'''
envl = s_msgpack.un(byts)
iv = envl.get('iv', b'')
asscd = envl.get('asscd', b'')
data = envl.get('data', b'')
decryptor = AESGCM(self.ekey)
try:
data = decryptor.decrypt(iv, data, asscd)
except Exception:
logger.exception('Error decrypting data')
return None
return data
示例5: encrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def encrypt( self, plaintext ):
aesgcm = AESGCM(self.key)
nonce = os.urandom(16)
ciphertext = aesgcm.encrypt(nonce, plaintext.encode('utf-8'), None)
return base64.b64encode( nonce + ciphertext ).decode('utf-8')
示例6: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def decrypt( self, ciphertext ):
ciphertext = base64.b64decode(ciphertext)
nonce = ciphertext[:16]
ciphertext = ciphertext[16:]
aesgcm = AESGCM(self.key)
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
return plaintext.decode('utf-8')
示例7: encrypt_str
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def encrypt_str(self, content, key, salt, salt_explicit):
# return the encrypted content prepended with salt_explicit
aesgcm = AESGCM(key)
ciphertext = aesgcm.encrypt(self._build_iv(salt, salt_explicit), content, None)
return struct.pack('!q', salt_explicit) + ciphertext
示例8: decrypt_str
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def decrypt_str(self, content, key, salt):
# content contains the gcm tag and salt_explicit in plaintext
if len(content) < 24:
raise CryptoException("truncated content")
salt_explicit, = struct.unpack_from('!q', content)
aesgcm = AESGCM(key)
return aesgcm.decrypt(self._build_iv(salt, salt_explicit), content[8:], None)
示例9: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def decrypt(self, jsons):
"""Import file is AES GCM encrypted, let's decrypt it.
Based on the import script from Aegis:
https://github.com/beemdevelopment/Aegis/blob/master/scripts/decrypt.py
Format documentation:
https://github.com/beemdevelopment/Aegis/blob/master/docs/vault.md
"""
if not CRYPTOGRAPHY:
raise ImportError(name='cryptography')
password = getpassword(self.prefix)
master_key = None
for slot in jsons['header']['slots']:
if slot['type'] != 1:
continue
kdf = Scrypt(salt=bytes.fromhex(slot['salt']),
length=32,
n=slot['n'],
r=slot['r'],
p=slot['p'],
backend=default_backend())
key = kdf.derive(password.encode("utf-8"))
cipher = AESGCM(key)
param = slot['key_params']
try:
nonce = bytes.fromhex(param['nonce'])
data = bytes.fromhex(slot['key']) + bytes.fromhex(param['tag'])
master_key = cipher.decrypt(nonce=nonce,
data=data,
associated_data=None)
except InvalidTag: # pragma: no cover
pass
if master_key is None: # pragma: no cover
raise FormatError("unable to decrypt the master key.")
cipher = AESGCM(master_key)
param = jsons['header']['params']
content = base64.b64decode(jsons['db']) + bytes.fromhex(param['tag'])
plain = cipher.decrypt(nonce=bytes.fromhex(param['nonce']),
data=content,
associated_data=None)
return plain.decode('utf-8')
示例10: get_encrypted
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import aead [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.aead import AESGCM [as 别名]
def get_encrypted(content: str, key: str) -> str:
"""
Encrypts content with encryption key.
Args:
content: Content to encrypt
key: encryption key from oproxy
Returns:
timestamp: Encrypted content
"""
def create_nonce():
return os.urandom(12)
def encrypt(string, enc_key):
"""
Encrypts string input with encryption key.
Args:
string: String to encrypt
enc_key: Encryption key
Returns:
bytes: Encrypted value
"""
# String to bytes
try:
enc_key = base64.b64decode(enc_key)
except Exception as err:
return_error(f"Error in Microsoft authorization: {str(err)}"
f" Please check authentication related parameters.", error=traceback.format_exc())
# Create key
aes_gcm = AESGCM(enc_key)
# Create nonce
nonce = create_nonce()
# Create ciphered data
data = string.encode()
ct = aes_gcm.encrypt(nonce, data, None)
return base64.b64encode(nonce + ct)
now = MicrosoftClient.epoch_seconds()
encrypted = encrypt(f'{now}:{content}', key).decode('utf-8')
return encrypted