Skip to content

Instantly share code, notes, and snippets.

@landswellsong
Created February 25, 2015 19:04
Show Gist options
  • Save landswellsong/395f2f98b83c91355a25 to your computer and use it in GitHub Desktop.
Save landswellsong/395f2f98b83c91355a25 to your computer and use it in GitHub Desktop.
#include <sodium.h>
#include <stdio.h>
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
/* Copied over from DHT.c not to mess around with tox source deps */
int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2)
{
size_t i;
uint8_t distance1, distance2;
for (i = 0; i < CLIENT_ID_SIZE; ++i) {
distance1 = abs(((int8_t *)id)[i] ^ ((int8_t *)id1)[i]);
distance2 = abs(((int8_t *)id)[i] ^ ((int8_t *)id2)[i]);
if (distance1 < distance2)
return 1;
if (distance1 > distance2)
return 2;
}
return 0;
}
int main()
{
long tries=0;
uint8_t pA[crypto_box_PUBLICKEYBYTES], sA[crypto_box_SECRETKEYBYTES];
uint8_t pB[crypto_box_PUBLICKEYBYTES], sB[crypto_box_SECRETKEYBYTES];
uint8_t pC[crypto_box_PUBLICKEYBYTES], sC[crypto_box_SECRETKEYBYTES];
/* Generating an initial pair */
crypto_box_keypair(pA, sA);
crypto_box_keypair(pB, sB);
do
{
crypto_box_keypair(pC, sC);
tries++;
}
while (id_closest(pA, pB, pC) != 2);
printf("Found a closer key after %ld tries\n", tries);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment