1 /* ISC license. */
2 
3 #include <string.h>
4 #include <skalibs/uint32.h>
5 #include <skalibs/md5.h>
6 #include "md5-internal.h"
7 
md5_final(MD5Schedule * ctx,char * digest)8 void md5_final (MD5Schedule *ctx, char *digest /* 16 chars */)
9 {
10   unsigned int count = (ctx->bits[0] >> 3) & 0x3F ;
11   unsigned char *p = ctx->in + count ;
12   *p++ = 0x80;
13   count = 63 - count ;
14   if (count < 8)
15   {
16     memset(p, 0, count) ;
17     uint32_little_endian((char *)ctx->in, 16) ;
18     md5_transform(ctx->buf, (uint32_t *)ctx->in) ;
19     memset(ctx->in, 0, 56) ;
20   }
21   else memset(p, 0, count - 8) ;
22   uint32_little_endian((char *)ctx->in, 14) ;
23 
24   memcpy(ctx->in + 56, &ctx->bits[0], 4) ;
25   memcpy(ctx->in + 60, &ctx->bits[1], 4) ;
26 
27   md5_transform(ctx->buf, (uint32_t *)ctx->in) ;
28   uint32_little_endian((char *)ctx->buf, 4) ;
29   memcpy(digest, ctx->buf, 16) ;
30 }
31