Tim Ruehsen tim.ruehsen@gmx.de writes:
I meanwhile found the nettle docs on the web (http://www.lysator.liu.se/~nisse/nettle/nettle.html#MD5):
I'm happy you found the documentation. The manual should also be included in debian's nettle-dev package, in info, html and pdf formats.
########### Function: void md5_digest (struct md5_ctx *ctx, size_t length, uint8_t *digest)
Performs final processing and extracts the message digest, writing it to digest. length may be smaller than MD5_DIGEST_SIZE, in which case only the first length octets of the digest are written. ###########
Sorry you find this unclear. Can you suggest a concise improvement?
unsigned char digest[128]; // *should* be large enough, if not we get an error char *algo = <user input> if ((len=hash(algo,text,textlen,digest,digestsize)) < 0) { // error like 'unknown algo', 'digest size too small', ... } else { // got <len> bytes in digest, e.g. print as hex values }
For simplicity, let's assume that algoriths are represented by struct nettle_hash, rather than an ascii name. Then a hash function in that style should be implemented like
int hash (const struct nettle_hash *hash, const uint8_t *msg, size_t length, uint8_t *digest, size_t digest_size) { void *ctx; if (digest_size < hash->digest_size) /* Too small */ return -1;
ctx = alloca(hash->context_size); hash->init(ctx); hash->update(ctx, length, msg); hash->digest(ctx, hash->digest_size, digest); return hash->digest_size; }
I don't think that passing a valid digest size to hash->digest here should be a big deal.
Regards, /Niels