]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/md5.c
Xtypes: Documentation
[libucw.git] / ucw / md5.c
index e692bf844d82ed295b38e0e38b2f5ff09044d007..d244356d89ddd01e7df91ef8f545f976eaf87197 100644 (file)
--- a/ucw/md5.c
+++ b/ucw/md5.c
  * 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() */
 
 #ifdef CPU_LITTLE_ENDIAN
 #define byteReverse(buf, len)  /* Nothing */
 #else
-void byteReverse(byte *buf, uns longs);
+void byteReverse(byte *buf, uint longs);
 
 /*
  * Note: this code is harmless on little-endian machines.
  */
-void byteReverse(byte *buf, uns longs)
+void byteReverse(byte *buf, uint longs)
 {
   u32 t;
   do {
-    t = (u32) ((uns) buf[3] << 8 | buf[2]) << 16 |
-      ((uns) buf[1] << 8 | buf[0]);
+    t = (u32) ((uint) buf[3] << 8 | buf[2]) << 16 |
+      ((uint) buf[1] << 8 | buf[0]);
     *(u32 *) buf = t;
     buf += 4;
   } while (--longs);
@@ -59,7 +59,7 @@ void md5_init(md5_context *ctx)
  * Update context to reflect the concatenation of another buffer full
  * of bytes.
  */
-void md5_update(md5_context *ctx, const byte *buf, uns len)
+void md5_update(md5_context *ctx, const byte *buf, uint len)
 {
   u32 t;
 
@@ -109,7 +109,7 @@ void md5_update(md5_context *ctx, const byte *buf, uns len)
  */
 byte *md5_final(md5_context *ctx)
 {
-  uns count;
+  uint count;
   byte *p;
 
   /* Compute number of bytes mod 64 */
@@ -139,9 +139,7 @@ byte *md5_final(md5_context *ctx)
   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;
@@ -247,10 +245,35 @@ void md5_transform(u32 buf[4], u32 const in[16])
   buf[3] += d;
 }
 
-void md5_hash_buffer(byte *outbuf, const byte *buffer, uns length)
+void md5_hash_buffer(byte *outbuf, const byte *buffer, uint length)
 {
   md5_context c;
   md5_init(&c);
   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