Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sk_decoded = base58.b58decode(sk_base58)
hash = pyscrypt.hash(password=b"demouser",
salt=b"demouser",
N=1024,
r=1,
p=1,
dkLen=32)
sk = base58.b58encode(hash)
hex_sk = binascii.b2a_hex(hash)
print('Secret Key:', sk, 'length: ', len(sk))
self.assertEqual(sk_base58, sk)
self.assertEqual(sk_decoded, hash)
#print(sk)
keypair = libnacl.public.SecretKey(hash)
# 2ipFYsqXnrw4Mt2RUWzEQntAH1FEFB8R52rAT3eExn9S
pk_base58 = base58.b58encode(keypair.pk)
pk_decoded = base58.b58decode(pk_base58)
self.assertEqual(pk_decoded, keypair.pk)
print('Public Key:', pk_base58, 'length: ', len(pk_base58))
print("XID: ", crypto.key_to_xid(keypair.pk))
def test_packet_min_length(self):
"""
Check that an empty NaCl message takes exactly 40 bytes (24
bytes nonce and 16 bytes NaCl authenticator).
"""
a = libnacl.public.SecretKey()
b = libnacl.public.SecretKey()
box = libnacl.public.Box(sk=a.sk, pk=b.pk)
nonce, data = box.encrypt(b'', pack_nonce=False)
assert len(nonce) == 24
assert len(data) == 16
except ValueError as exc:
raise GatewayKeyError('Invalid key format') from exc
type_ = Key.Type(type_)
# Check type
if type_ != expected_type:
raise GatewayKeyError('Invalid key type: {}, expected: {}'.format(
type_, expected_type
))
# De-hexlify
key = libnacl.encode.hex_decode(key)
# Convert to SecretKey or PublicKey
if type_ == Key.Type.private:
key = libnacl.public.SecretKey(key)
elif type_ == Key.Type.public:
key = libnacl.public.PublicKey(key)
return key
the contents of the file
'''
with open(path, 'rb') as fp_:
packaged = fp_.read()
if serial == 'msgpack':
import msgpack
key_data = msgpack.loads(packaged)
elif serial == 'json':
import json
key_data = json.loads(packaged.decode(encoding='UTF-8'))
if 'priv' in key_data and 'sign' 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:
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'])
raise ValueError('Found no key data')
def sealedbox_decrypt(data, **kwargs):
'''
Decrypt data using a secret key that was encrypted using a public key with `nacl.sealedbox_encrypt`.
CLI Examples:
.. code-block:: bash
salt-call nacl.sealedbox_decrypt pEXHQM6cuaF7A=
salt-call --local nacl.sealedbox_decrypt data='pEXHQM6cuaF7A=' sk_file=/etc/salt/pki/master/nacl
salt-call --local nacl.sealedbox_decrypt data='pEXHQM6cuaF7A=' sk='YmFkcGFzcwo='
'''
if data is None:
return None
sk = _get_sk(**kwargs)
keypair = libnacl.public.SecretKey(sk)
b = libnacl.sealed.SealedBox(keypair)
return b.decrypt(base64.b64decode(data))
CLI Examples:
.. code-block:: bash
salt-call nacl.sealedbox_decrypt pEXHQM6cuaF7A=
salt-call --local nacl.sealedbox_decrypt data='pEXHQM6cuaF7A=' sk_file=/etc/salt/pki/master/nacl
salt-call --local nacl.sealedbox_decrypt data='pEXHQM6cuaF7A=' sk='YmFkcGFzcwo='
'''
if data is None:
return None
# ensure data is in bytes
data = salt.utils.stringutils.to_bytes(data)
sk = _get_sk(**kwargs)
keypair = libnacl.public.SecretKey(sk)
b = libnacl.sealed.SealedBox(keypair)
return b.decrypt(base64.b64decode(data))
def init_keypair(fname=None):
"""Create a new encryption
keypair; stored in file fname
if provided. The keypair object
is returned.
"""
kp = public.SecretKey()
if fname:
# Note: handles correct file permissions
kp.save(fname)
return kp