ext/digest/sha1/sha1hl.c


DEFINITIONS

This source file includes following functions.
  1. SHA1_End
  2. SHA1_File
  3. SHA1_Data


   1  /*      $NetBSD: sha1hl.c,v 1.2 2001/03/10 15:55:14 tron Exp $  */
   2  /*      $RoughId: sha1hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $  */
   3  /*      $Id: sha1hl.c,v 1.1 2001/07/13 20:06:14 knu Exp $       */
   4  
   5  /* sha1hl.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  
  14  /* #include "namespace.h" */
  15  
  16  #include "sha1.h"
  17  #include <fcntl.h>
  18  
  19  #include <assert.h>
  20  #include <errno.h>
  21  #include <stdio.h>
  22  #include <stdlib.h>
  23  #if defined(HAVE_UNISTD_H)
  24  # include <unistd.h>
  25  #endif
  26  
  27  #if defined(LIBC_SCCS) && !defined(lint)
  28  /* __RCSID("$NetBSD: sha1hl.c,v 1.2 2001/03/10 15:55:14 tron Exp $"); */
  29  #endif /* LIBC_SCCS and not lint */
  30  
  31  #ifndef _DIAGASSERT
  32  #define _DIAGASSERT(cond)       assert(cond)
  33  #endif
  34  
  35  
  36  /* ARGSUSED */
  37  char *
  38  SHA1_End(ctx, buf)
  39      SHA1_CTX *ctx;
  40      char *buf;
  41  {
  42      int i;
  43      char *p = buf;
  44      uint8_t digest[20];
  45      static const char hex[]="0123456789abcdef";
  46  
  47      _DIAGASSERT(ctx != NULL);
  48      /* buf may be NULL */
  49  
  50      if (p == NULL && (p = malloc(41)) == NULL)
  51          return 0;
  52  
  53      SHA1_Final(digest,ctx);
  54      for (i = 0; i < 20; i++) {
  55          p[i + i] = hex[((uint32_t)digest[i]) >> 4];
  56          p[i + i + 1] = hex[digest[i] & 0x0f];
  57      }
  58      p[i + i] = '\0';
  59      return(p);
  60  }
  61  
  62  char *
  63  SHA1_File (filename, buf)
  64      char *filename;
  65      char *buf;
  66  {
  67      uint8_t buffer[BUFSIZ];
  68      SHA1_CTX ctx;
  69      int fd, num, oerrno;
  70  
  71      _DIAGASSERT(filename != NULL);
  72      /* XXX: buf may be NULL ? */
  73  
  74      SHA1_Init(&ctx);
  75  
  76      if ((fd = open(filename,O_RDONLY)) < 0)
  77          return(0);
  78  
  79      while ((num = read(fd, buffer, sizeof(buffer))) > 0)
  80          SHA1_Update(&ctx, buffer, (size_t)num);
  81  
  82      oerrno = errno;
  83      close(fd);
  84      errno = oerrno;
  85      return(num < 0 ? 0 : SHA1_End(&ctx, buf));
  86  }
  87  
  88  char *
  89  SHA1_Data (data, len, buf)
  90      const uint8_t *data;
  91      size_t len;
  92      char *buf;
  93  {
  94      SHA1_CTX ctx;
  95  
  96      _DIAGASSERT(data != NULL);
  97      /* XXX: buf may be NULL ? */
  98  
  99      SHA1_Init(&ctx);
 100      SHA1_Update(&ctx, data, len);
 101      return(SHA1_End(&ctx, buf));
 102  }