Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sign(self):
msg = (b'Well, that\'s no ordinary rabbit. That\'s the most foul, '
b'cruel, and bad-tempered rodent you ever set eyes on.')
signer = libnacl.sign.Signer()
signed = signer.sign(msg)
signature = signer.signature(msg)
self.assertNotEqual(msg, signed)
veri = libnacl.sign.Verifier(signer.hex_vk())
verified = veri.verify(signed)
verified2 = veri.verify(signature + msg)
self.assertEqual(verified, msg)
self.assertEqual(verified2, msg)
def test_save_load_sign(self):
msg = b'then leap out of the rabbit, taking the French by surprise'
signer = libnacl.sign.Signer()
fh_, sign_path = tempfile.mkstemp()
os.close(fh_)
signer.save(sign_path)
signer_load = libnacl.utils.load_key(sign_path)
signed1 = signer.sign(msg)
signed2 = signer_load.sign(msg)
self.assertEqual(signed1, signed2)
os.remove(sign_path)
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 __init__(self, crypt=None, sign=None):
self.crypt = libnacl.public.SecretKey(crypt)
self.signer = libnacl.sign.Signer(sign)
self.sk = self.crypt.sk
self.seed = self.signer.seed
self.pk = self.crypt.pk
self.vk = self.signer.vk
key_data = json.loads(stream.read())
else:
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 __init__(self, crypt=None, sign=None):
self.crypt = libnacl.public.SecretKey(crypt)
self.signer = libnacl.sign.Signer(sign)
self.sk = self.crypt.sk
self.seed = self.signer.seed
self.pk = self.crypt.pk
self.vk = self.signer.vk
SEED_LENGTH = 32 # Length of the key
crypto_sign_BYTES = 64
SCRYPT_PARAMS = {'N': 4096,
'r': 16,
'p': 1
}
def _ensure_bytes(data):
if isinstance(data, str):
return bytes(data, 'utf-8')
return data
class SigningKey(libnacl.sign.Signer):
def __init__(self, salt, password):
salt = _ensure_bytes(salt)
password = _ensure_bytes(password)
seed = scrypt(password, salt,
SCRYPT_PARAMS['N'], SCRYPT_PARAMS['r'], SCRYPT_PARAMS['p'],
SEED_LENGTH)
super().__init__(seed)
self.pubkey = Base58Encoder.encode(self.vk)
class Base58Encoder(object):
@staticmethod
def encode(data):
return base58.b58encode(data)
@staticmethod
def validate_type(self, value):
try:
Signer(value.decode('hex'))
except (TypeError, ValueError) as exc:
raise ValidationError(exc.message)