/* ***************** binary-ASCII encoding *************** */
+/**
+ * Get the numeric value corresponding to a character.
+ *
+ * @param a a character
+ * @return corresponding numeric value
+ */
static unsigned int
getValue__ (unsigned char a)
{
return -1;
}
+
/**
* Convert GNUNET_CRYPTO_hash to ASCII encoding. The ASCII encoding is rather
* GNUnet specific. It was chosen such that it only uses characters
result->encoding[wpos] = '\0';
}
+
/**
* Convert ASCII encoding back to GNUNET_CRYPTO_hash
*
unsigned int wpos;
unsigned int bits;
unsigned int vbit;
+ int ret;
if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
return GNUNET_SYSERR;
vbit = 2; /* padding! */
wpos = sizeof (GNUNET_HashCode);
rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
- bits = getValue__ (enc[--rpos]) >> 3;
+ bits = (ret = getValue__ (enc[--rpos])) >> 3;
+ if (-1 == ret)
+ return GNUNET_SYSERR;
while (wpos > 0)
{
GNUNET_assert (rpos > 0);
- bits = (getValue__ (enc[--rpos]) << vbit) | bits;
+ bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
+ if (-1 == ret)
+ return GNUNET_SYSERR;
vbit += 5;
if (vbit >= 8)
{
return GNUNET_OK;
}
+
/**
* Compute the distance between 2 hashcodes. The computation must be
* fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
return (x1 * x2);
}
+
+/**
+ * Create a random hash code.
+ *
+ * @param mode desired quality level
+ * @param result hash code that is randomized
+ */
void
GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
GNUNET_HashCode * result)
result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
}
+
+/**
+ * compute result(delta) = b - a
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to b - a
+ */
void
GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
const GNUNET_HashCode * b,
result->bits[i] = b->bits[i] - a->bits[i];
}
+
+/**
+ * compute result(b) = a + delta
+ *
+ * @param a some hash code
+ * @param delta some hash code
+ * @param result set to a + delta
+ */
void
GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
const GNUNET_HashCode * delta, GNUNET_HashCode * result)
}
+/**
+ * compute result = a ^ b
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to a ^ b
+ */
void
GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
GNUNET_HashCode * result)
/**
* Convert a hashcode into a key.
+ *
+ * @param hc hash code that serves to generate the key
+ * @param skey set to a valid session key
+ * @param iv set to a valid initialization vector
*/
void
GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
}
+
/**
* Determine how many low order bits match in two
* GNUNET_HashCodes. i.e. - 010011 and 011111 share
/**
* Compare function for HashCodes, producing a total ordering
* of all hashcodes.
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
* @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
*/
int
/**
* Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
* in the XOR metric (Kademlia).
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
+ * @param target some hash code
* @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
*/
int