From 5ff2f7acd636131a6b94bc44cbcef2dd02dd4e43 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 9 Jul 2013 10:51:09 +0200 Subject: 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. --- Makefile | 2 +- nthash.c | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index e89dd9b..ce6e577 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ all: nthash.c check: $(ECHO) -n "testing123" | ./nthash | \ - $(GREP) '^3e 67 8b 26 6d 1d 44 9e 53 1e 92 be c3 3b 6d 27$$' + $(GREP) '^3e678b266d1d449e531e92bec33b6d27$$' install: $(INSTALL) -D -m0755 nthash $(DESTDIR)/usr/bin/nthash diff --git a/nthash.c b/nthash.c index 5e90c5d..2a8aca0 100644 --- a/nthash.c +++ b/nthash.c @@ -10,33 +10,34 @@ #include #include +#include #include -#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; -- cgit v1.2.3-70-g09d2