/usr/lib/python2.7/dist-packages/libnacl/utils.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | # -*- coding: utf-8 -*-
import struct
import time
# Import nacl libs
import libnacl
import libnacl.encode
import libnacl.secret
import libnacl.public
import libnacl.sign
import libnacl.dual
def load_key(path_or_file, serial='json'):
'''
Read in a key from a file and return the applicable key object based on
the contents of the file
'''
if hasattr(path_or_file, 'read'):
stream = path_or_file
else:
if serial == 'json':
stream = open(path_or_file, 'r')
else:
stream = open(path_or_file, 'rb')
try:
if serial == 'msgpack':
import msgpack
key_data = msgpack.load(stream)
elif serial == 'json':
import json
key_data = json.loads(stream.read(), encoding='UTF-8')
finally:
if stream != path_or_file:
stream.close()
if 'priv' in key_data and 'sign' in key_data and 'pub' in key_data:
return libnacl.dual.DualSecret(
libnacl.encode.hex_decode(key_data['priv']),
libnacl.encode.hex_decode(key_data['sign']))
elif 'priv' in key_data and 'pub' in key_data:
return libnacl.public.SecretKey(
libnacl.encode.hex_decode(key_data['priv']))
elif 'sign' in key_data:
return libnacl.sign.Signer(
libnacl.encode.hex_decode(key_data['sign']))
elif 'pub' in key_data:
return libnacl.public.PublicKey(
libnacl.encode.hex_decode(key_data['pub']))
elif 'verify' in key_data:
return libnacl.sign.Verifier(key_data['verify'])
elif 'priv' in key_data:
return libnacl.secret.SecretBox(
libnacl.encode.hex_decode(key_data['priv']))
raise ValueError('Found no key data')
def salsa_key():
'''
Generates a salsa2020 key
'''
return libnacl.randombytes(libnacl.crypto_secretbox_KEYBYTES)
def rand_nonce():
'''
Generates and returns a random bytestring of the size defined in libsodium
as crypto_box_NONCEBYTES
'''
return libnacl.randombytes(libnacl.crypto_box_NONCEBYTES)
def time_nonce():
'''
Generates and returns a nonce as in rand_nonce() but using a timestamp for the first 8 bytes.
This function now exists mostly for backwards compatibility, as rand_nonce() is usually preferred.
'''
nonce = rand_nonce()
return (struct.pack('=d', time.time()) + nonce)[:len(nonce)]
|