diff options
author | Christian Hesse <mail@eworm.de> | 2013-07-09 10:51:09 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2013-07-09 10:51:09 +0200 |
commit | 5ff2f7acd636131a6b94bc44cbcef2dd02dd4e43 (patch) | |
tree | 3cc2b61db2c857680c43784ed123f5494746f861 /nthash.c | |
parent | 04efc9ddd7b94ed610728ac511ff76ce188031ab (diff) | |
download | nthash-5ff2f7acd636131a6b94bc44cbcef2dd02dd4e43.tar.gz nthash-5ff2f7acd636131a6b94bc44cbcef2dd02dd4e43.tar.zst |
use iconv to convert string to UTF-16LE
This should handle multi byte characters correctly. Please not warnings
about line breaks, etc have been removed.
Diffstat (limited to 'nthash.c')
-rw-r--r-- | nthash.c | 38 |
1 files changed, 19 insertions, 19 deletions
@@ -10,33 +10,34 @@ #include <stdio.h> #include <stdlib.h> +#include <iconv.h> #include <nettle/md4.h> -#define BUF_SIZE 1024 +#define BUF_SIZE 64 +#define TOCODE "UTF-16LE" +#define FROMCODE "UTF-8" int main(int argc, char **argv) { struct md4_ctx ctx; - uint8_t buffer[BUF_SIZE], buffernull[2*BUF_SIZE]; + char buffer[BUF_SIZE], buffernull[2 * BUF_SIZE]; + char *in = buffer, *out = buffernull; uint8_t digest[MD4_DIGEST_SIZE]; - int i, done; + int i; + size_t done, inbytes, outbytes; + iconv_t conv; md4_init(&ctx); while (1) { - done = fread(buffer, 1, sizeof(buffer), stdin); - /* add null bytes to string */ - for (i = 0; i < done; i++) { - if (buffer[i] == 0xa) - fprintf(stderr, "Warning: Password contains line break!\n"); - else if (buffer[i] < 0x20 || buffer[i] == 0x7f) - fprintf(stderr, "Warning: Password contains non-printable control character 0x%x!\n", buffer[i]); - else if (buffer[i] > 0x7f) - fprintf(stderr, "Warning: Password contains non-ASCII character 0x%x!\n", buffer[i]); - buffernull[i*2] = buffer[i]; - buffernull[i*2+1] = 0; - } - md4_update(&ctx, done*2, buffernull); - if (done < sizeof(buffer)) + done = inbytes = fread(buffer, 1, BUF_SIZE, stdin); + outbytes = 2 * inbytes; + + conv = iconv_open(TOCODE, FROMCODE); + iconv(conv, &in, &inbytes, &out, &outbytes); + iconv_close(conv); + + md4_update(&ctx, done * 2 - outbytes, (unsigned char *)buffernull); + if (done < BUF_SIZE) break; } if (ferror(stdin)) @@ -45,8 +46,7 @@ int main(int argc, char **argv) { md4_digest(&ctx, MD4_DIGEST_SIZE, digest); for (i = 0; i < MD4_DIGEST_SIZE -1; i++) - printf("%02x ", digest[i]); - printf("%02x", digest[i]); + printf("%02x", digest[i]); putchar('\n'); return EXIT_SUCCESS; |