apt @VERSION@
|
00001 // -*- mode: cpp; mode: fold -*- 00002 // Description /*{{{*/ 00003 // $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $ 00004 /* ###################################################################### 00005 00006 SHA{512,256}SumValue - Storage for a SHA-{512,256} hash. 00007 SHA{512,256}Summation - SHA-{512,256} Secure Hash Algorithm. 00008 00009 This is a C++ interface to a set of SHA{512,256}Sum functions, that mirrors 00010 the equivalent MD5 & SHA1 classes. 00011 00012 ##################################################################### */ 00013 /*}}}*/ 00014 #ifndef APTPKG_SHA2_H 00015 #define APTPKG_SHA2_H 00016 00017 #include <string> 00018 #include <cstring> 00019 #include <algorithm> 00020 #include <stdint.h> 00021 00022 #include "sha2_internal.h" 00023 #include "hashsum_template.h" 00024 00025 typedef HashSumValue<512> SHA512SumValue; 00026 typedef HashSumValue<256> SHA256SumValue; 00027 00028 class SHA2SummationBase : public SummationImplementation 00029 { 00030 protected: 00031 bool Done; 00032 public: 00033 bool Add(const unsigned char *inbuf, unsigned long len) = 0; 00034 00035 void Result(); 00036 }; 00037 00038 class SHA256Summation : public SHA2SummationBase 00039 { 00040 SHA256_CTX ctx; 00041 unsigned char Sum[32]; 00042 00043 public: 00044 bool Add(const unsigned char *inbuf, unsigned long len) 00045 { 00046 if (Done) 00047 return false; 00048 SHA256_Update(&ctx, inbuf, len); 00049 return true; 00050 }; 00051 using SummationImplementation::Add; 00052 00053 SHA256SumValue Result() 00054 { 00055 if (!Done) { 00056 SHA256_Final(Sum, &ctx); 00057 Done = true; 00058 } 00059 SHA256SumValue res; 00060 res.Set(Sum); 00061 return res; 00062 }; 00063 SHA256Summation() 00064 { 00065 SHA256_Init(&ctx); 00066 Done = false; 00067 }; 00068 }; 00069 00070 class SHA512Summation : public SHA2SummationBase 00071 { 00072 SHA512_CTX ctx; 00073 unsigned char Sum[64]; 00074 00075 public: 00076 bool Add(const unsigned char *inbuf, unsigned long len) 00077 { 00078 if (Done) 00079 return false; 00080 SHA512_Update(&ctx, inbuf, len); 00081 return true; 00082 }; 00083 using SummationImplementation::Add; 00084 00085 SHA512SumValue Result() 00086 { 00087 if (!Done) { 00088 SHA512_Final(Sum, &ctx); 00089 Done = true; 00090 } 00091 SHA512SumValue res; 00092 res.Set(Sum); 00093 return res; 00094 }; 00095 SHA512Summation() 00096 { 00097 SHA512_Init(&ctx); 00098 Done = false; 00099 }; 00100 }; 00101 00102 00103 #endif