How to use the libnacl.crypto_box_beforenm function in libnacl

To help you get started, we’ve selected a few libnacl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github saltstack / libnacl / tests / unit / test_raw_public.py View on Github external
def test_boxnm(self):
        msg = b'Are you suggesting coconuts migrate?'
        # run 1
        nonce1 = libnacl.utils.rand_nonce()
        pk1, sk1 = libnacl.crypto_box_keypair()
        pk2, sk2 = libnacl.crypto_box_keypair()
        k1 = libnacl.crypto_box_beforenm(pk2, sk1)
        k2 = libnacl.crypto_box_beforenm(pk1, sk2)
        enc_msg = libnacl.crypto_box_afternm(msg, nonce1, k1)
        self.assertNotEqual(msg, enc_msg)
        clear_msg = libnacl.crypto_box_open_afternm(enc_msg, nonce1, k2)
        self.assertEqual(clear_msg, msg)
github eavatar / eavatar-me / tests / unit / test_util_crypto.py View on Github external
def test_calc_shared_key_via_two_key_pairs(self):
        bob = libnacl.public.SecretKey()
        alice = libnacl.public.SecretKey()
        alice_key = libnacl.crypto_box_beforenm(bob.pk, alice.sk)
        bob_key = libnacl.crypto_box_beforenm(alice.pk, bob.sk)
        #print(base58.b58encode(bob_key))
        self.assertEqual(bob_key, alice_key)
github saltstack / libnacl / tests / unit / test_raw_public.py View on Github external
def test_boxnm(self):
        msg = b'Are you suggesting coconuts migrate?'
        # run 1
        nonce1 = libnacl.utils.rand_nonce()
        pk1, sk1 = libnacl.crypto_box_keypair()
        pk2, sk2 = libnacl.crypto_box_keypair()
        k1 = libnacl.crypto_box_beforenm(pk2, sk1)
        k2 = libnacl.crypto_box_beforenm(pk1, sk2)
        enc_msg = libnacl.crypto_box_afternm(msg, nonce1, k1)
        self.assertNotEqual(msg, enc_msg)
        clear_msg = libnacl.crypto_box_open_afternm(enc_msg, nonce1, k2)
        self.assertEqual(clear_msg, msg)
github hyperledger / indy-plenum / stp_core / crypto / nacl_wrappers.py View on Github external
def __init__(self, private_key, public_key):
        if private_key and public_key:
            self._shared_key = libnacl.crypto_box_beforenm(
                public_key.encode(encoder=encoding.RawEncoder),
                private_key.encode(encoder=encoding.RawEncoder),
            )
        else:
            self._shared_key = None
github eavatar / eavatar-me / src / ava / util / crypto.py View on Github external
def calc_session_key(pk, sk):
    """
    Calculate the session key from a public key and secret key.

    :param pk: the counterpart's public key.
    :param sk: the own private key
    :return:
    """
    return libnacl.crypto_box_beforenm(pk, sk)
github saltstack / raet / raet / nacling.py View on Github external
def __init__(self, private_key, public_key):
        if private_key and public_key:
            self._shared_key = libnacl.crypto_box_beforenm(
                public_key.encode(encoder=encoding.RawEncoder),
                private_key.encode(encoder=encoding.RawEncoder),
            )
        else:
            self._shared_key = None
github Tribler / py-ipv8 / ipv8 / messaging / anonymization / tunnelcrypto.py View on Github external
def generate_diffie_shared_secret(self, dh_received, key=None):
        if key is None:
            key = self.key

        tmp_key = self.generate_key("curve25519")
        y = tmp_key.key.sk
        Y = tmp_key.key.pk
        shared_secret = libnacl.crypto_box_beforenm(dh_received, y) + libnacl.crypto_box_beforenm(dh_received, key.key.sk)

        AUTH = libnacl.crypto_auth(Y, shared_secret[:32])
        return shared_secret, Y, AUTH
github Tribler / py-ipv8 / ipv8 / messaging / anonymization / tunnelcrypto.py View on Github external
def verify_and_generate_shared_secret(self, dh_secret, dh_received, auth, B):
        shared_secret = libnacl.crypto_box_beforenm(dh_received, dh_secret.key.sk) + libnacl.crypto_box_beforenm(B, dh_secret.key.sk)
        libnacl.crypto_auth_verify(auth, dh_received, shared_secret[:32])

        return shared_secret