]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/md5.c
Doc: Documented growing arrays, generic allocators and related things
[libucw.git] / ucw / md5.c
index e692bf844d82ed295b38e0e38b2f5ff09044d007..3f8384ba8cc43036826018ddbcda29110b7fee2d 100644 (file)
--- a/ucw/md5.c
+++ b/ucw/md5.c
@@ -15,8 +15,8 @@
  * will fill a supplied 16-byte array with the digest.
  */
 
  * will fill a supplied 16-byte array with the digest.
  */
 
-#include "ucw/lib.h"
-#include "ucw/md5.h"
+#include <ucw/lib.h>
+#include <ucw/md5.h>
 
 #include <string.h>            /* for memcpy() */
 
 
 #include <string.h>            /* for memcpy() */
 
@@ -139,9 +139,7 @@ byte *md5_final(md5_context *ctx)
   byteReverse(ctx->in, 14);
 
   /* Append length in bits and transform */
   byteReverse(ctx->in, 14);
 
   /* Append length in bits and transform */
-  ((u32 *) ctx->in)[14] = ctx->bits[0];
-  ((u32 *) ctx->in)[15] = ctx->bits[1];
-
+  memcpy(ctx->in + 56, ctx->bits, 8);
   md5_transform(ctx->buf, (u32 *) ctx->in);
   byteReverse((byte *) ctx->buf, 4);
   return (byte *) ctx->buf;
   md5_transform(ctx->buf, (u32 *) ctx->in);
   byteReverse((byte *) ctx->buf, 4);
   return (byte *) ctx->buf;
@@ -254,3 +252,28 @@ void md5_hash_buffer(byte *outbuf, const byte *buffer, uns length)
   md5_update(&c, buffer, length);
   memcpy(outbuf, md5_final(&c), MD5_SIZE);
 }
   md5_update(&c, buffer, length);
   memcpy(outbuf, md5_final(&c), MD5_SIZE);
 }
+
+#ifdef TEST
+
+#include <stdio.h>
+#include <unistd.h>
+#include <ucw/string.h>
+
+int main(void)
+{
+  md5_context hd;
+  byte buf[3];
+  int cnt;
+
+  md5_init(&hd);
+  while ((cnt = read(0, buf, sizeof(buf))) > 0)
+    md5_update(&hd, buf, cnt);
+
+  char text[MD5_HEX_SIZE];
+  mem_to_hex(text, md5_final(&hd), MD5_SIZE, 0);
+  puts(text);
+
+  return 0;
+}
+
+#endif