Python zmq.curve_keypair() Examples

The following are 12 code examples of zmq.curve_keypair(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zmq , or try the search function .
Example #1
Source File: certs.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def create_certificates(key_dir, name, metadata=None):
    """Create zmq certificates.
    
    Returns the file paths to the public and secret certificate files.
    """
    public_key, secret_key = zmq.curve_keypair()
    base_filename = os.path.join(key_dir, name)
    secret_key_file = "{0}.key_secret".format(base_filename)
    public_key_file = "{0}.key".format(base_filename)
    now = datetime.datetime.now()

    _write_key_file(public_key_file,
                    _cert_public_banner.format(now),
                    public_key)

    _write_key_file(secret_key_file,
                    _cert_secret_banner.format(now),
                    public_key,
                    secret_key=secret_key,
                    metadata=metadata)

    return public_key_file, secret_key_file 
Example #2
Source File: test_security.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_keypair(self):
        """test curve_keypair"""
        try:
            public, secret = zmq.curve_keypair()
        except zmq.ZMQError:
            raise SkipTest("CURVE unsupported")
        
        self.assertEqual(type(secret), bytes)
        self.assertEqual(type(public), bytes)
        self.assertEqual(len(secret), 40)
        self.assertEqual(len(public), 40)
        
        # verify that it is indeed Z85
        bsecret, bpublic = [ z85.decode(key) for key in (public, secret) ]
        self.assertEqual(type(bsecret), bytes)
        self.assertEqual(type(bpublic), bytes)
        self.assertEqual(len(bsecret), 32)
        self.assertEqual(len(bpublic), 32) 
Example #3
Source File: certs.py    From pySINDy with MIT License 6 votes vote down vote up
def create_certificates(key_dir, name, metadata=None):
    """Create zmq certificates.
    
    Returns the file paths to the public and secret certificate files.
    """
    public_key, secret_key = zmq.curve_keypair()
    base_filename = os.path.join(key_dir, name)
    secret_key_file = "{0}.key_secret".format(base_filename)
    public_key_file = "{0}.key".format(base_filename)
    now = datetime.datetime.now()

    _write_key_file(public_key_file,
                    _cert_public_banner.format(now),
                    public_key)

    _write_key_file(secret_key_file,
                    _cert_secret_banner.format(now),
                    public_key,
                    secret_key=secret_key,
                    metadata=metadata)

    return public_key_file, secret_key_file 
Example #4
Source File: test_security.py    From pySINDy with MIT License 6 votes vote down vote up
def test_keypair(self):
        """test curve_keypair"""
        try:
            public, secret = zmq.curve_keypair()
        except zmq.ZMQError:
            raise SkipTest("CURVE unsupported")
        
        self.assertEqual(type(secret), bytes)
        self.assertEqual(type(public), bytes)
        self.assertEqual(len(secret), 40)
        self.assertEqual(len(public), 40)
        
        # verify that it is indeed Z85
        bsecret, bpublic = [ z85.decode(key) for key in (public, secret) ]
        self.assertEqual(type(bsecret), bytes)
        self.assertEqual(type(bpublic), bytes)
        self.assertEqual(len(bsecret), 32)
        self.assertEqual(len(bpublic), 32) 
Example #5
Source File: test_security.py    From pySINDy with MIT License 6 votes vote down vote up
def test_curve_public(self):
        """test curve_public"""
        try:
            public, secret = zmq.curve_keypair()
        except zmq.ZMQError:
            raise SkipTest("CURVE unsupported")
        if zmq.zmq_version_info() < (4,2):
            raise SkipTest("curve_public is new in libzmq 4.2")

        derived_public = zmq.curve_public(secret)

        self.assertEqual(type(derived_public), bytes)
        self.assertEqual(len(derived_public), 40)

        # verify that it is indeed Z85
        bpublic = z85.decode(derived_public)
        self.assertEqual(type(bpublic), bytes)
        self.assertEqual(len(bpublic), 32)

        # verify that it is equal to the known public key
        self.assertEqual(derived_public, public) 
Example #6
Source File: certs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_certificates(key_dir, name, metadata=None):
    """Create zmq certificates.
    
    Returns the file paths to the public and secret certificate files.
    """
    public_key, secret_key = zmq.curve_keypair()
    base_filename = os.path.join(key_dir, name)
    secret_key_file = "{0}.key_secret".format(base_filename)
    public_key_file = "{0}.key".format(base_filename)
    now = datetime.datetime.now()

    _write_key_file(public_key_file,
                    _cert_public_banner.format(now),
                    public_key)

    _write_key_file(secret_key_file,
                    _cert_secret_banner.format(now),
                    public_key,
                    secret_key=secret_key,
                    metadata=metadata)

    return public_key_file, secret_key_file 
Example #7
Source File: test_security.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def setUp(self):
        if zmq.zmq_version_info() < (4,0):
            raise SkipTest("security is new in libzmq 4.0")
        try:
            zmq.curve_keypair()
        except zmq.ZMQError:
            raise SkipTest("security requires libzmq to be built with CURVE support")
        super(TestSecurity, self).setUp() 
Example #8
Source File: test_security.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_curve(self):
        """test CURVE encryption"""
        server = self.socket(zmq.DEALER)
        server.identity = b'IDENT'
        client = self.socket(zmq.DEALER)
        self.sockets.extend([server, client])
        try:
            server.curve_server = True
        except zmq.ZMQError as e:
            # will raise EINVAL if no CURVE support
            if e.errno == zmq.EINVAL:
                raise SkipTest("CURVE unsupported")
        
        server_public, server_secret = zmq.curve_keypair()
        client_public, client_secret = zmq.curve_keypair()
        
        server.curve_secretkey = server_secret
        server.curve_publickey = server_public
        client.curve_serverkey = server_public
        client.curve_publickey = client_public
        client.curve_secretkey = client_secret
        
        self.assertEqual(server.mechanism, zmq.CURVE)
        self.assertEqual(client.mechanism, zmq.CURVE)
        
        self.assertEqual(server.get(zmq.CURVE_SERVER), True)
        self.assertEqual(client.get(zmq.CURVE_SERVER), False)

        with self.zap():
            iface = 'tcp://127.0.0.1'
            port = server.bind_to_random_port(iface)
            client.connect("%s:%i" % (iface, port))
            self.bounce(server, client) 
Example #9
Source File: helper.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def create_zmq_connection(node, sock_type):
    clientstack = node.clientstack
    sock = zmq.Context().socket(sock_type)
    l_pub_key, l_sec_key = zmq.curve_keypair()
    sock.setsockopt(zmq.IDENTITY, base64.encodebytes(l_pub_key))
    sock.setsockopt(zmq.CURVE_PUBLICKEY, l_pub_key)
    sock.setsockopt(zmq.CURVE_SECRETKEY, l_sec_key)
    sock.setsockopt(zmq.TCP_KEEPALIVE, 1)

    sock.setsockopt(zmq.CURVE_SERVERKEY, clientstack.publicKey)
    sock.connect("tcp://{}:{}".format(clientstack.ha.host, clientstack.ha.port))

    return sock 
Example #10
Source File: client.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def check_zmq_connection(addr_port):
    ctx = zmq.Context()
    sock = ctx.socket(zmq.DEALER)
    l_pub_key, l_sec_key = zmq.curve_keypair()
    sock.setsockopt(zmq.IDENTITY, base64.encodebytes(l_pub_key))
    sock.setsockopt(zmq.CURVE_PUBLICKEY, l_pub_key)
    sock.setsockopt(zmq.CURVE_SECRETKEY, l_sec_key)
    dest = get_dest(SERVER_SEED)
    sock.setsockopt(zmq.CURVE_SERVERKEY, z85.encode(ed_25519_pk_to_curve_25519(base58.b58decode(dest))))

    try:
        sock.connect("{}://{}".format(ZMQ_NETWORK_PROTOCOL, addr_port))
        sock.send_string(TEST_MSG)
    except OSError as e:
        print("ZMQ CHECK PHASE::: Cannot connect to {} because\n {}".format(addr_port, e))
        return False

    print("ZMQ CHECK PHASE:::Waiting {} seconds for response from server".format(WAIT_TIMEOUT))

    ready = zmq.select([sock], [], [], WAIT_TIMEOUT)
    if ready[0]:
        reply = sock.recv(flags=zmq.NOBLOCK)
        reply = reply.decode()
        print("ZMQ CHECK PHASE::: Got reply {}".format(reply))

        if reply != EXPECTED_ZMQ_REPLY:
            print("ZMQ CHECK PHASE:::ZMQ connection test failed. \nGot {} \nbut expected reply {}\n".format(reply, EXPECTED_ZMQ_REPLY))
        print("ZMQ CHECK PHASE:::Got expected response")
        return True
    return False 
Example #11
Source File: test_security.py    From pySINDy with MIT License 5 votes vote down vote up
def setUp(self):
        if zmq.zmq_version_info() < (4,0):
            raise SkipTest("security is new in libzmq 4.0")
        try:
            zmq.curve_keypair()
        except zmq.ZMQError:
            raise SkipTest("security requires libzmq to be built with CURVE support")
        super(TestSecurity, self).setUp() 
Example #12
Source File: test_security.py    From pySINDy with MIT License 5 votes vote down vote up
def test_curve(self):
        """test CURVE encryption"""
        server = self.socket(zmq.DEALER)
        server.identity = b'IDENT'
        client = self.socket(zmq.DEALER)
        self.sockets.extend([server, client])
        try:
            server.curve_server = True
        except zmq.ZMQError as e:
            # will raise EINVAL if no CURVE support
            if e.errno == zmq.EINVAL:
                raise SkipTest("CURVE unsupported")
        
        server_public, server_secret = zmq.curve_keypair()
        client_public, client_secret = zmq.curve_keypair()
        
        server.curve_secretkey = server_secret
        server.curve_publickey = server_public
        client.curve_serverkey = server_public
        client.curve_publickey = client_public
        client.curve_secretkey = client_secret
        
        self.assertEqual(server.mechanism, zmq.CURVE)
        self.assertEqual(client.mechanism, zmq.CURVE)
        
        self.assertEqual(server.get(zmq.CURVE_SERVER), True)
        self.assertEqual(client.get(zmq.CURVE_SERVER), False)

        with self.zap():
            iface = 'tcp://127.0.0.1'
            port = server.bind_to_random_port(iface)
            client.connect("%s:%i" % (iface, port))
            self.bounce(server, client)