示例#1
0
int
md5_finish (md5_t *ctx, void *digest, unsigned int digestlen)
{
  uint8_t buf[MD5_DIGEST_LENGTH];

  if (ctx == NULL || ctx->magic != MD5_MAGIC
      || digest == NULL || digestlen < MD5_DIGEST_LENGTH)
    {
      errno = EINVAL;
      return (-1);
    }

  _md5_append_padding_and_length (ctx);

  /* Note there are no endian issues here, compiler is required to
   * handle bitmasks and shifts correctly
   */
  
  buf[0]  = (A & 0x000000ff);
  buf[1]  = (A & 0x0000ff00) >> 8;
  buf[2]  = (A & 0x00ff0000) >> 16;
  buf[3]  = (A & 0xff000000) >> 24;
  buf[4]  = (B & 0x000000ff);
  buf[5]  = (B & 0x0000ff00) >> 8;
  buf[6]  = (B & 0x00ff0000) >> 16;
  buf[7]  = (B & 0xff000000) >> 24;
  buf[8]  = (C & 0x000000ff);
  buf[9]  = (C & 0x0000ff00) >> 8;
  buf[10] = (C & 0x00ff0000) >> 16;
  buf[11] = (C & 0xff000000) >> 24;
  buf[12] = (D & 0x000000ff);
  buf[13] = (D & 0x0000ff00) >> 8;
  buf[14] = (D & 0x00ff0000) >> 16;
  buf[15] = (D & 0xff000000) >> 24;

  memcpy (digest, buf, MD5_DIGEST_LENGTH);
  ctx->magic = ~MD5_MAGIC;
  return (MD5_DIGEST_LENGTH);
}
示例#2
0
int 
md5_finish(md5_t *ctx, uint8_t *digest, unsigned int digestlen)
{

  if (ctx == NULL || ctx->magic != MD5_MAGIC 
      || digest == NULL || digestlen < MD5_DIGEST_LENGTH) 
    {
      errno = EINVAL;
      return -1;
    } 
  
  _md5_append_padding_and_length(ctx);

  /* Note there are no endian issues here, compiler is required to
   * handle bitmasks and shifts correctly
   */
  
  digest[0]  = (A & 0x000000ff);
  digest[1]  = (A & 0x0000ff00) >> 8;
  digest[2]  = (A & 0x00ff0000) >> 16;
  digest[3]  = (A & 0xff000000) >> 24;
  digest[4]  = (B & 0x000000ff);
  digest[5]  = (B & 0x0000ff00) >> 8;
  digest[6]  = (B & 0x00ff0000) >> 16;
  digest[7]  = (B & 0xff000000) >> 24;
  digest[8]  = (C & 0x000000ff);
  digest[9]  = (C & 0x0000ff00) >> 8;
  digest[10] = (C & 0x00ff0000) >> 16;
  digest[11] = (C & 0xff000000) >> 24;
  digest[12] = (D & 0x000000ff);
  digest[13] = (D & 0x0000ff00) >> 8;
  digest[14] = (D & 0x00ff0000) >> 16;
  digest[15] = (D & 0xff000000) >> 24;
  
  ctx->magic = ~MD5_MAGIC;
  return MD5_DIGEST_LENGTH;
}