/usr/lib/python2.7/dist-packages/libnacl/secret.py is in python-libnacl 1.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # -*- coding: utf-8 -*-
'''
Utilities to make secret box encryption simple
'''
# Import libnacl
import libnacl
import libnacl.utils
import libnacl.base
class SecretBox(libnacl.base.BaseKey):
'''
Manage symetric encryption using the salsa20 algorithm
'''
def __init__(self, key=None):
if key is None:
key = libnacl.utils.salsa_key()
if len(key) != libnacl.crypto_secretbox_KEYBYTES:
raise ValueError('Invalid key')
self.sk = key
def encrypt(self, msg, nonce=None, pack_nonce=True):
'''
Encrypt the given message. If a nonce is not given it will be
generated via the rand_nonce function
'''
if nonce is None:
nonce = libnacl.utils.rand_nonce()
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
raise ValueError('Invalid nonce size')
ctxt = libnacl.crypto_secretbox(msg, nonce, self.sk)
if pack_nonce:
return nonce + ctxt
else:
return nonce, ctxt
def decrypt(self, ctxt, nonce=None):
'''
Decrypt the given message, if no nonce is given the nonce will be
extracted from the message
'''
if nonce is None:
nonce = ctxt[:libnacl.crypto_secretbox_NONCEBYTES]
ctxt = ctxt[libnacl.crypto_secretbox_NONCEBYTES:]
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES:
raise ValueError('Invalid nonce')
return libnacl.crypto_secretbox_open(ctxt, nonce, self.sk)
|