chksum.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <unistd.h>
00006 
00007 #include "pool.h"
00008 #include "util.h"
00009 #include "chksum.h"
00010 
00011 #include "md5.h"
00012 #include "sha1.h"
00013 #include "sha2.h"
00014 
00015 struct ctxhandle {
00016   Id type;
00017   int done;
00018   unsigned char result[64];
00019   union {
00020     MD5_CTX md5;
00021     SHA1_CTX sha1;
00022     SHA256_CTX sha256;
00023   } c;
00024 };
00025 
00026 void *
00027 sat_chksum_create(Id type)
00028 {
00029   struct ctxhandle *h;
00030   h = sat_calloc(1, sizeof(*h));
00031   h->type = type;
00032   switch(type)
00033     {
00034     case REPOKEY_TYPE_MD5:
00035       sat_MD5_Init(&h->c.md5);
00036       return h;
00037     case REPOKEY_TYPE_SHA1:
00038       sat_SHA1_Init(&h->c.sha1);
00039       return h;
00040     case REPOKEY_TYPE_SHA256:
00041       sat_SHA256_Init(&h->c.sha256);
00042       return h;
00043     }
00044   free(h);
00045   return 0;
00046 }
00047 
00048 void
00049 sat_chksum_add(void *handle, const void *data, int len)
00050 {
00051   struct ctxhandle *h = handle;
00052   switch(h->type)
00053     {
00054     case REPOKEY_TYPE_MD5:
00055       sat_MD5_Update(&h->c.md5, (void *)data, len);
00056       return;
00057     case REPOKEY_TYPE_SHA1:
00058       sat_SHA1_Update(&h->c.sha1, data, len);
00059       return;
00060     case REPOKEY_TYPE_SHA256:
00061       sat_SHA256_Update(&h->c.sha256, data, len);
00062       return;
00063     default:
00064       return;
00065     }
00066 }
00067 
00068 unsigned char *
00069 sat_chksum_get(void *handle, int *lenp)
00070 {
00071   struct ctxhandle *h = handle;
00072   if (h->done)
00073     return h->result;
00074   switch(h->type)
00075     {
00076     case REPOKEY_TYPE_MD5:
00077       sat_MD5_Final(h->result, &h->c.md5);
00078       h->done = 1;
00079       if (lenp)
00080         *lenp = 16;
00081       return h->result;
00082     case REPOKEY_TYPE_SHA1:
00083       sat_SHA1_Final(&h->c.sha1, h->result);
00084       h->done = 1;
00085       if (lenp)
00086         *lenp = 20;
00087       return h->result;
00088     case REPOKEY_TYPE_SHA256:
00089       sat_SHA256_Final(h->result, &h->c.sha256);
00090       h->done = 1;
00091       if (lenp)
00092         *lenp = 32;
00093       return h->result;
00094     default:
00095       if (lenp)
00096         *lenp = 0;
00097       return 0;
00098     }
00099 }
00100 
00101 void *
00102 sat_chksum_free(void *handle, unsigned char *cp)
00103 {
00104   if (cp)
00105     {
00106       unsigned char *res;
00107       int l;
00108       res = sat_chksum_get(handle, &l);
00109       memcpy(cp, res, l);
00110     }
00111   sat_free(handle);
00112   return 0;
00113 }
00114 
Generated on Mon Dec 12 11:44:12 2011 for satsolver by  doxygen 1.6.3