ext/digest/rmd160/rmd160hl.c


DEFINITIONS

This source file includes following functions.
  1. RMD160_End
  2. RMD160_File
  3. RMD160_Data


   1  /*      $NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $     */
   2  /*      $RoughId: rmd160hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $        */
   3  /*      $Id: rmd160hl.c,v 1.1 2001/07/13 20:06:14 knu Exp $     */
   4  
   5  /* rmd160hl.c
   6   * ----------------------------------------------------------------------------
   7   * "THE BEER-WARE LICENSE" (Revision 42):
   8   * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
   9   * can do whatever you want with this stuff. If we meet some day, and you think
  10   * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
  11   * ----------------------------------------------------------------------------
  12   *
  13   * from OpenBSD: rmd160hl.c,v 1.2 1999/08/17 09:13:12 millert Exp $
  14   */  
  15  
  16  #include "rmd160.h"
  17  
  18  #ifndef lint
  19  /* __RCSID("$NetBSD: rmd160hl.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); */
  20  #endif  /* not lint */
  21  
  22  /* #include "namespace.h" */
  23  
  24  #include <assert.h>
  25  #include <errno.h>
  26  #include <fcntl.h>
  27  #include <stdio.h>
  28  #include <stdlib.h>
  29  #if defined(HAVE_UNISTD_H)
  30  # include <unistd.h>
  31  #endif
  32  
  33  #ifndef _DIAGASSERT
  34  #define _DIAGASSERT(cond)       assert(cond)
  35  #endif
  36  
  37  
  38  char *
  39  RMD160_End(RMD160_CTX *ctx, char *buf)
  40  {
  41      size_t i;
  42      char *p = buf;
  43      uint8_t digest[20];
  44      static const char hex[]="0123456789abcdef";
  45  
  46      _DIAGASSERT(ctx != NULL);
  47      /* buf may be NULL */
  48  
  49      if (p == NULL && (p = malloc(41)) == NULL)
  50          return 0;
  51  
  52      RMD160_Final(digest,ctx);
  53      for (i = 0; i < 20; i++) {
  54          p[i + i] = hex[(uint32_t)digest[i] >> 4];
  55          p[i + i + 1] = hex[digest[i] & 0x0f];
  56      }
  57      p[i + i] = '\0';
  58      return(p);
  59  }
  60  
  61  char *
  62  RMD160_File(char *filename, char *buf)
  63  {
  64      uint8_t buffer[BUFSIZ];
  65      RMD160_CTX ctx;
  66      int fd, num, oerrno;
  67  
  68      _DIAGASSERT(filename != NULL);
  69      /* XXX: buf may be NULL ? */
  70  
  71      RMD160_Init(&ctx);
  72  
  73      if ((fd = open(filename, O_RDONLY)) < 0)
  74          return(0);
  75  
  76      while ((num = read(fd, buffer, sizeof(buffer))) > 0)
  77          RMD160_Update(&ctx, buffer, (size_t)num);
  78  
  79      oerrno = errno;
  80      close(fd);
  81      errno = oerrno;
  82      return(num < 0 ? 0 : RMD160_End(&ctx, buf));
  83  }
  84  
  85  char *
  86  RMD160_Data(const uint8_t *data, size_t len, char *buf)
  87  {
  88      RMD160_CTX ctx;
  89  
  90      _DIAGASSERT(data != NULL);
  91      /* XXX: buf may be NULL ? */
  92  
  93      RMD160_Init(&ctx);
  94      RMD160_Update(&ctx, data, len);
  95      return(RMD160_End(&ctx, buf));
  96  }