# -*- coding: utf-8 -*-

# Import nacl libs
import libnacl
import libnacl.encode

# Import python libs
import datetime
import binascii


class BaseKey(object):
    '''
    Include methods for key management convenience
    '''
    def hex_sk(self):
        if hasattr(self, 'sk'):
            return libnacl.encode.hex_encode(self.sk)
        else:
            return ''

    def hex_pk(self):
        if hasattr(self, 'pk'):
            return libnacl.encode.hex_encode(self.pk)

    def hex_vk(self):
        if hasattr(self, 'vk'):
            return libnacl.encode.hex_encode(self.vk)

    def hex_seed(self):
        if hasattr(self, 'seed'):
            return libnacl.encode.hex_encode(self.seed)


def salsa_key():
    '''
    Generates a salsa2020 key
    '''
    return libnacl.randombytes(libnacl.crypto_secretbox_KEYBYTES)


def time_nonce():
    '''
    Generates a safe nonce

    The nonce generated here is done by grabbing the 20 digit microsecond
    timestamp and appending 4 random chars
    '''
    nonce = '{0:%Y%m%d%H%M%S%f}{1}'.format(
            datetime.datetime.now(),
            binascii.hexlify(libnacl.randombytes(2)).decode(encoding='UTF-8'))
    return nonce.encode(encoding='UTF-8')

# -*- coding: utf-8 -*-

# Import nacl libs
import libnacl
import libnacl.encode

# Import python libs
import time
import binascii


class BaseKey(object):
    '''
    Include methods for key management convenience
    '''
    def hex_sk(self):
        if hasattr(self, 'sk'):
            return libnacl.encode.hex_encode(self.sk)
        else:
            return ''

    def hex_pk(self):
        if hasattr(self, 'pk'):
            return libnacl.encode.hex_encode(self.pk)

    def hex_vk(self):
        if hasattr(self, 'vk'):
            return libnacl.encode.hex_encode(self.vk)

    def hex_seed(self):
        if hasattr(self, 'seed'):
            return libnacl.encode.hex_encode(self.seed)


def salsa_key():
    '''
    Generates a salsa2020 key
    '''
    return libnacl.randombytes(libnacl.crypto_secretbox_KEYBYTES)


def time_nonce():
    '''
    Generates a safe nonce

    The nonce generated here is done by grabbing the 20 digit microsecond
    timestamp and appending 4 random chars
    '''
    nonce = '{0}{1}'.format(
            str(int(time.time() * 1000000)),
            binascii.hexlify(libnacl.randombytes(24)).decode(encoding='UTF-8'))
    return nonce.encode(encoding='UTF-8')[:libnacl.crypto_box_NONCEBYTES]

Make the nonce more secure and faster to generate
Make the nonce more secure and faster to generate
