当前位置: 首页>>代码示例>>C++>>正文


C++ crypto_box_keypair函数代码示例

本文整理汇总了C++中crypto_box_keypair函数的典型用法代码示例。如果您正苦于以下问题:C++ crypto_box_keypair函数的具体用法?C++ crypto_box_keypair怎么用?C++ crypto_box_keypair使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了crypto_box_keypair函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: create_onion_path

/* Create a new onion path.
 *
 * Create a new onion path out of nodes (nodes is a list of ONION_PATH_LENGTH nodes)
 *
 * new_path must be an empty memory location of atleast Onion_Path size.
 *
 * return -1 on failure.
 * return 0 on success.
 */
int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes)
{
    if (!new_path || !nodes)
        return -1;

    encrypt_precompute(nodes[0].public_key, dht->self_secret_key, new_path->shared_key1);
    memcpy(new_path->public_key1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);

    uint8_t random_public_key[crypto_box_PUBLICKEYBYTES];
    uint8_t random_secret_key[crypto_box_SECRETKEYBYTES];

    crypto_box_keypair(random_public_key, random_secret_key);
    encrypt_precompute(nodes[1].public_key, random_secret_key, new_path->shared_key2);
    memcpy(new_path->public_key2, random_public_key, crypto_box_PUBLICKEYBYTES);

    crypto_box_keypair(random_public_key, random_secret_key);
    encrypt_precompute(nodes[2].public_key, random_secret_key, new_path->shared_key3);
    memcpy(new_path->public_key3, random_public_key, crypto_box_PUBLICKEYBYTES);

    new_path->ip_port1 = nodes[0].ip_port;
    new_path->ip_port2 = nodes[1].ip_port;
    new_path->ip_port3 = nodes[2].ip_port;

    memcpy(new_path->node_public_key1, nodes[0].public_key, crypto_box_PUBLICKEYBYTES);
    memcpy(new_path->node_public_key2, nodes[1].public_key, crypto_box_PUBLICKEYBYTES);
    memcpy(new_path->node_public_key3, nodes[2].public_key, crypto_box_PUBLICKEYBYTES);

    return 0;
}
开发者ID:jingyu,项目名称:toxcore,代码行数:38,代码来源:onion.c

示例2: test_hacl_01

static void test_hacl_01(void)
{
    int res;

    /* Creating keypair ALICE... */
    crypto_box_keypair(alice_pk, alice_sk);

    /* Creating keypair BOB... */
    crypto_box_keypair(bob_pk, bob_sk);

    memset(m, 0, crypto_box_ZEROBYTES);
    memcpy(m + crypto_box_ZEROBYTES, message, MLEN - crypto_box_ZEROBYTES);

    /* Encrypting using pk_bob... */
    crypto_box(c, m, MLEN, n, bob_pk, alice_sk);

    memset(result, '\0', sizeof(result));

    /* Decrypting... */
    res = crypto_box_open(result, c, MLEN, n, alice_pk, bob_sk);

    TEST_ASSERT_EQUAL_INT(0, res);

    memset(r, 0, sizeof(r));
    memcpy(r, result + crypto_box_ZEROBYTES, MLEN - crypto_box_ZEROBYTES);

    TEST_ASSERT_EQUAL_STRING((const char*)message, (const char*)r);
}
开发者ID:A-Paul,项目名称:RIOT,代码行数:28,代码来源:main.c

示例3: main

int main(void)
{
  size_t mlen;
  size_t i;
  int caught;

  for (mlen = 0;mlen < 1000 && mlen + crypto_box_ZEROBYTES < sizeof m;++mlen) {
    crypto_box_keypair(alicepk,alicesk);
    crypto_box_keypair(bobpk,bobsk);
    randombytes(n,crypto_box_NONCEBYTES);
    randombytes(m + crypto_box_ZEROBYTES,mlen);
    crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,bobpk,alicesk);
    caught = 0;
    while (caught < 10) {
      c[rand() % (mlen + crypto_box_ZEROBYTES)] = rand();
      if (crypto_box_open(m2,c,mlen + crypto_box_ZEROBYTES,n,alicepk,bobsk) == 0) {
        for (i = 0;i < mlen + crypto_box_ZEROBYTES;++i)
          if (m2[i] != m[i]) {
            printf("forgery\n");
            return 100;
          }
      } else {
        ++caught;
      }
    }
  }
  return 0;
}
开发者ID:BadaDu,项目名称:telehash-objc,代码行数:28,代码来源:box8.c

示例4: main

int main() {
	char * padded_message;
	int padded_mlen;
	u8 sk[PRIV_KEY_LEN] = {0};
	u8 pk[PUB_KEY_LEN] = {0};
	u8 sk2[PRIV_KEY_LEN] = {0};
	u8 pk2[PUB_KEY_LEN] = {0};
	u8 nonce[NONCE_LEN] = {0};
	char* message = "This is a cross-platform test of crypto_box/crypto_box_open in TweetNaCl.";
	u8 * ciphertext;
	char *decryptedmessage;

	// randomize nonce
	randombytes(nonce, NONCE_LEN);
	printf("Nonce: \n");
	hexdump((char*)nonce, NONCE_LEN);

	crypto_box_keypair(pk, sk);
	crypto_box_keypair(pk2, sk2);
	
	printf("Public key: \n");
	hexdump((char*)pk, PUB_KEY_LEN);
	printf("\nSecret key: \n");
	hexdump((char*)sk, PRIV_KEY_LEN);
	printf("Public key2: \n");
	hexdump((char*)pk2, PUB_KEY_LEN);
	printf("\nSecret key2: \n");
	hexdump((char*)sk2, PRIV_KEY_LEN);

	padded_mlen = strlen(message) + PADDING_LEN;
	padded_message = (char*) malloc(padded_mlen);
	memset(padded_message, 0, PADDING_LEN);
	memcpy(padded_message + PADDING_LEN, message, strlen(message));

	ciphertext = (u8*) malloc(padded_mlen);

	// we have a string so add 1 byte and NUL it so we can print it
	decryptedmessage = (char*) malloc(padded_mlen+1);
	decryptedmessage[padded_mlen] = '\0';

	printf("crypto_box returned: %d\n",crypto_box(ciphertext, (u8*)padded_message,  padded_mlen, nonce, pk2, sk));

	free(padded_message);
	
	printf("\nCipher text: \n");
	hexdump((char*)ciphertext, padded_mlen);
	printf("crypto_box_open returned: %d\n", crypto_box_open((u8*)decryptedmessage, ciphertext, padded_mlen, nonce, pk, sk2));
	free(ciphertext);
	printf("\nDecrypted text: \n");
	hexdump((char*)decryptedmessage, padded_mlen);

	printf("%s\n", decryptedmessage+32);
	free(decryptedmessage);
	return 0;
}
开发者ID:ultramancool,项目名称:tweetnacl-usable,代码行数:55,代码来源:main.c

示例5: crypto_connect

/* Start a secure connection with other peer who has public_key and ip_port
   returns -1 if failure
   returns crypt_connection_id of the initialized connection if everything went well. */
int crypto_connect(uint8_t *public_key, IP_Port ip_port)
{
    uint32_t i;
    int id = getcryptconnection_id(public_key);
    if (id != -1) {
        IP_Port c_ip = connection_ip(crypto_connections[id].number);
        if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
            return -1;
    }
    for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
        if (crypto_connections[i].status == CONN_NO_CONNECTION) {
            int id = new_connection(ip_port);
            if (id == -1)
                return -1;
            crypto_connections[i].number = id;
            crypto_connections[i].status = CONN_HANDSHAKE_SENT;
            random_nonce(crypto_connections[i].recv_nonce);
            memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
            crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);

            if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
                                     crypto_connections[i].sessionpublic_key) == 1) {
                increment_nonce(crypto_connections[i].recv_nonce);
                return i;
            }
            return -1; /* this should never happen. */
        }
    }
    return -1;
}
开发者ID:mozii,项目名称:ProjectTox-Core,代码行数:33,代码来源:net_crypto.c

示例6: main

int main(int argc,char **argv)
{
  char *d;

  if (!argv[0]) die_usage();
  if (!argv[1]) die_usage();
  d = argv[1];

  umask(022);
  if (mkdir(d,0755) == -1) die_fatal("unable to create directory",d,0);
  if (chdir(d) == -1) die_fatal("unable to chdir to directory",d,0);
  if (mkdir(".expertsonly",0700) == -1) die_fatal("unable to create directory",d,".expertsonly");

  sodium_init();

  crypto_box_keypair(pk,sk);
  create(d,"publickey",pk,sizeof pk);

  randombytes(noncekey,sizeof noncekey);

  umask(077);
  create(d,".expertsonly/secretkey",sk,sizeof sk);
  create(d,".expertsonly/lock",lock,sizeof lock);
  create(d,".expertsonly/noncekey",noncekey,sizeof noncekey);
  create(d,".expertsonly/noncecounter",noncecounter,sizeof noncecounter);

  return 0;
}
开发者ID:aburan28,项目名称:libchloride,代码行数:28,代码来源:curvecpmakekey.c

示例7: crypto_box_keypair

PyObject *pycrypto_box_keypair(PyObject *self){
  PyObject *pypk, *pysk, *pyret;
  unsigned char pk[crypto_box_PUBLICKEYBYTES];
  unsigned char sk[crypto_box_SECRETKEYBYTES];

  crypto_box_keypair(pk, sk);

  pypk = PyBytes_FromStringAndSize((char *)pk, crypto_box_PUBLICKEYBYTES);

  if (!pypk)
    return (PyObject *)0;

  pysk = PyBytes_FromStringAndSize((char *)sk, crypto_box_SECRETKEYBYTES);

  if (!pysk){
    Py_DECREF(pypk);
    return (PyObject *)0;}

  pyret = PyTuple_New(2);

  if (!pyret){
    Py_DECREF(pypk);
    Py_DECREF(pysk);
    return (PyObject *)0;}

  PyTuple_SET_ITEM(pyret, 0, pypk);
  PyTuple_SET_ITEM(pyret, 1, pysk);
  return pyret;}
开发者ID:JosephSWilliams,项目名称:nacltaia-otr,代码行数:28,代码来源:nacltaia.c

示例8: zmq_curve_keypair

int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key)
{
#if defined (ZMQ_HAVE_CURVE)
#   if crypto_box_PUBLICKEYBYTES != 32 \
    || crypto_box_SECRETKEYBYTES != 32
#       error "CURVE encryption library not built correctly"
#   endif

    uint8_t public_key [32];
    uint8_t secret_key [32];

    int rc = crypto_box_keypair (public_key, secret_key);
    //  Is there a sensible errno to set here?
    if (rc)
        return rc;

    zmq_z85_encode (z85_public_key, public_key, 32);
    zmq_z85_encode (z85_secret_key, secret_key, 32);

    return 0;
#else
    (void) z85_public_key, (void) z85_secret_key;
    errno = ENOTSUP;
    return -1;
#endif
}
开发者ID:kenWeeMuu,项目名称:libzmq,代码行数:26,代码来源:zmq_utils.cpp

示例9: zmq_curve_keypair

int zmq_curve_keypair (char *z85_public_key_, char *z85_secret_key_)
{
#if defined(ZMQ_HAVE_CURVE)
#if crypto_box_PUBLICKEYBYTES != 32 || crypto_box_SECRETKEYBYTES != 32
#error "CURVE encryption library not built correctly"
#endif

    uint8_t public_key[32];
    uint8_t secret_key[32];

    zmq::random_open ();

    int res = crypto_box_keypair (public_key, secret_key);
    zmq_z85_encode (z85_public_key_, public_key, 32);
    zmq_z85_encode (z85_secret_key_, secret_key, 32);

    zmq::random_close ();

    return res;
#else
    (void) z85_public_key_, (void) z85_secret_key_;
    errno = ENOTSUP;
    return -1;
#endif
}
开发者ID:dand-oss,项目名称:libzmq,代码行数:25,代码来源:zmq_utils.cpp

示例10: ekpubencrypt

/*
 * encrypt a file using public key cryptography
 * ephemeral key version that discards sender key pair
 */
static void
ekpubencrypt(const char *pubkeyfile, const char *ident, const char *msgfile, const char *encfile)
{
	struct ekcmsg ekcmsg;
	struct pubkey pubkey;
	uint8_t *msg;
	unsigned long long msglen;
	uint8_t enckey[ENCSECRETBYTES];

	getpubkey(pubkeyfile, ident, &pubkey);

	crypto_box_keypair(ekcmsg.pubkey, enckey);

	msg = readall(msgfile, &msglen);

	memcpy(ekcmsg.ekcalg, EKCALG, 2);
	memcpy(ekcmsg.pubfingerprint, pubkey.fingerprint, FPLEN);

	pubencryptmsg(msg, msglen, ekcmsg.box, pubkey.enckey, enckey);

	explicit_bzero(&enckey, sizeof(enckey));

	writeencfile(encfile, &ekcmsg, sizeof(ekcmsg), "<ephemeral>", msg, msglen);

	xfree(msg, msglen);
}
开发者ID:jwilkins,项目名称:reop,代码行数:30,代码来源:reop.c

示例11: randomstring

void Connection::handle_request(const CryptoIdentity& ci, const RoutingRequest& rq, const Hop& hop, const std::string& route_id, TransportSocket ts) {
  if ( hop.type() != Hop::UP ) return;
  if ( hop.nonce_algo() != Hop::XTEA32 ) return;
  // response is a "cookie packet":
  // tag, route id, nonce (24 bytes), [ConnectionAccept](B<>A')
  std::string cookie_packet;
  cookie_packet.push_back('\0');
  cookie_packet.append(route_id);
  std::string nonce = randomstring(24);
  cookie_packet.append(nonce);
  ConnectionAccept ack;
  ack.set_auth( ConnectionAccept::AUTHENC_BCARD );
  // generate our keys for this connection
  std::string connection_sk;
  std::string connection_pk = crypto_box_keypair(&connection_sk);
  ack.set_sender_pubkey(connection_pk);
  // ...and let the other party store them for us, in encrypted form ofc
  ack.set_cookie( ci.cookies.cookie( rq.sender_pubkey() + connection_sk ) );
  assert(ack.cookie().size() == COOKIE_SIZE);
  std::string encpart;
  ci.encrypt( ack.SerializeAsString()
            , nonce
            , static_cast<PkencAlgo>(rq.enc_algo())
            , rq.sender_pubkey()
            , encpart);
  cookie_packet.append(encpart);
  ts.send(cookie_packet);

}
开发者ID:andreasots,项目名称:vindicat,代码行数:29,代码来源:Connection.cpp

示例12: mechanism_t

zmq::curve_client_t::curve_client_t (const options_t &options_) :
    mechanism_t (options_),
    state (send_hello),
    cn_nonce(1),
    cn_peer_nonce(1),
    sync()
{
    int rc;
    memcpy (public_key, options_.curve_public_key, crypto_box_PUBLICKEYBYTES);
    memcpy (secret_key, options_.curve_secret_key, crypto_box_SECRETKEYBYTES);
    memcpy (server_key, options_.curve_server_key, crypto_box_PUBLICKEYBYTES);
    scoped_lock_t lock (sync);
#if defined(HAVE_TWEETNACL)
    // allow opening of /dev/urandom
    unsigned char tmpbytes[4];
    randombytes(tmpbytes, 4);
#else
    rc = sodium_init ();
    zmq_assert (rc != -1);
#endif

    //  Generate short-term key pair
    rc = crypto_box_keypair (cn_public, cn_secret);
    zmq_assert (rc == 0);
}
开发者ID:HJoYer,项目名称:libzmq,代码行数:25,代码来源:curve_client.cpp

示例13: tunnel_initClient

void tunnel_initClient(struct tunnel *t, uint64_t tid, void *serverPublickey) {
  t->tid   = tid;
  t->state = TUNNEL_STATE_CLIENT_PRE_HANDSHAKE;
  crypto_box_keypair(t->localPublickey, t->localSecretkey);
  memcpy(t->remotePublickey, serverPublickey, sizeof t->remotePublickey);
  memset(t->nonce, 0, sizeof t->nonce);
}
开发者ID:gillsoft,项目名称:minimalt,代码行数:7,代码来源:tunnel.c

示例14: main

int main (void)
{
#ifdef HAVE_LIBSODIUM
#   if crypto_box_PUBLICKEYBYTES != 32 \
    || crypto_box_SECRETKEYBYTES != 32
#   error "libsodium not built correctly"
#   endif
    
    puts ("This tool generates a CurveZMQ keypair, as two printable strings you can");
    puts ("use in configuration files or source code. The encoding uses Z85, which");
    puts ("is a base-85 format that is described in 0MQ RFC 32, and which has an");
    puts ("implementation in the z85_codec.h source used by this tool. The keypair");
    puts ("always works with the secret key held by one party and the public key");
    puts ("distributed (securely!) to peers wishing to connect to it.");

    uint8_t public_key [32];
    uint8_t secret_key [32];

    int rc = crypto_box_keypair (public_key, secret_key);
    assert (rc == 0);
    
    char encoded [41];
    zmq_z85_encode (encoded, public_key, 32);
    puts ("\n== CURVE PUBLIC KEY ==");
    puts (encoded);
    
    zmq_z85_encode (encoded, secret_key, 32);
    puts ("\n== CURVE SECRET KEY ==");
    puts (encoded);

#else
    puts ("To build curve_keygen, please install libsodium and then rebuild libzmq.");
#endif
    exit (0);
}
开发者ID:pijyoi,项目名称:zeromq4-x,代码行数:35,代码来源:curve_keygen.c

示例15: main

int main() {
  int fd[4], i;
  byte key_pub [2][pub_len],
       key_priv[2][priv_len];

  /* open files */
  for (i=0; i<4; i++) {
    if ((fd[i] = open(name[i], O_WRONLY|O_CREAT|O_EXCL,
		      (i%2) ? 0400 : 0444)) == -1) {
      fprintf(stderr, 
	      (errno == EEXIST)
	      ? "%s already exists\n" : "could not create file %s\n",
	      name[i]);
      error_cleanup(i);
    }
  }

  /* generate keys */
  for (i=0; i<2; i++) {
    crypto_box_keypair(key_pub[i], key_priv[i]);
    if ((write(fd[2*i  ], key_pub[i],  pub_len)  != pub_len)
     || (write(fd[2*i+1], key_priv[i], priv_len) != priv_len)
     || (close(fd[2*i  ]) == -1)
     || (close(fd[2*i+1]) == -1)) {
      error_cleanup(4);
    }
  }

  return 0;
}
开发者ID:FreifunkAdvanced,项目名称:hbbp,代码行数:30,代码来源:hbbp_keygen.c


注:本文中的crypto_box_keypair函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。