Esempio n. 1
0
    def test_scalarmult_rejects_wrong_length(self):
        good_key = b'This valid key is 32 bytes long.'

        for bad_key in (b'too short', b'too long' * 100):
            with self.assertRaises(ValueError) as context:
                libnacl.crypto_scalarmult_base(bad_key)
            self.assertEqual(context.exception.args, ('Invalid secret key',))

        self.assertEqual(libnacl.crypto_box_PUBLICKEYBYTES, len(libnacl.crypto_scalarmult_base(good_key)))
Esempio n. 2
0
    def test_scalarmult_rejects_wrong_length(self):
        good_key = b'This valid key is 32 bytes long.'

        for bad_key in (b'too short', b'too long' * 100):
            with self.assertRaises(ValueError) as context:
                libnacl.crypto_scalarmult_base(bad_key)
            self.assertEqual(context.exception.args, ('Invalid secret key', ))

        self.assertEqual(libnacl.crypto_box_PUBLICKEYBYTES,
                         len(libnacl.crypto_scalarmult_base(good_key)))
Esempio n. 3
0
 def __init__(self, sk=None):
     '''
     If a secret key is not passed in then it will be generated
     '''
     if sk is None:
         self.pk, self.sk = libnacl.crypto_box_keypair()
     elif len(sk) == libnacl.crypto_box_SECRETKEYBYTES:
         self.sk = sk
         self.pk = libnacl.crypto_scalarmult_base(sk)
     else:
         raise ValueError('Passed in invalid secret key')
Esempio n. 4
0
    def __init__(self, private_key, encoder=encoding.RawEncoder):
        # Decode the secret_key
        private_key = encoder.decode(private_key)

        # Verify that our seed is the proper size
        if len(private_key) != self.SIZE:
            raise ValueError(
                "The secret key must be exactly %d bytes long" % self.SIZE)

        raw_public_key = libnacl.crypto_scalarmult_base(private_key)

        self._private_key = private_key
        self.public_key = PublicKey(raw_public_key)
Esempio n. 5
0
    def __init__(self, private_key, encoder=encoding.RawEncoder):
        # Decode the secret_key
        private_key = encoder.decode(private_key)

        # Verify that our seed is the proper size
        if len(private_key) != self.SIZE:
            raise ValueError("The secret key must be exactly %d bytes long" %
                             self.SIZE)

        raw_public_key = libnacl.crypto_scalarmult_base(private_key)

        self._private_key = private_key
        self.public_key = PublicKey(raw_public_key)
Esempio n. 6
0
    def refreshKeys(self):
        """
        Refreshes keys as needed so valid set of keys
        """
        if not self.seed:  # no signing key seed so create new one
            self.seed = libnacl.randombytes(libnacl.crypto_sign_SEEDBYTES)

        if not self.sigkey or not self.verkey:  # ensure also sigkey and verkey
            self.verkey, self.sigkey = libnacl.crypto_sign_seed_keypair(
                self.seed)

        if not self.prikey:  # no private encryption key so create new pair
            self.pubkey, self.prikey = libnacl.crypto_box_keypair()

        if not self.pubkey:  # no public decryption key so create one from prikey
            self.pubkey = libnacl.crypto_scalarmult_base(self.prikey)