Sirikata
|
00001 /* Sirikata Utilities -- Sirikata Cryptography Utilities 00002 * Sha256.hpp 00003 * 00004 * Copyright (c) 2009, Daniel Reiter Horn 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef _SIRIKATA_SHA256_HPP_ 00034 #define _SIRIKATA_SHA256_HPP_ 00035 00036 #include <string> 00037 #include <cstdlib> 00038 #include <cstring> 00039 #include <exception> 00040 #include "Platform.hpp" 00041 #include "Array.hpp" 00042 00043 namespace Sirikata { 00044 00045 class SIRIKATA_EXPORT SHA256 { 00046 friend class SHA256Context; 00047 public: 00048 enum {static_size=32,hex_size=64}; 00049 typedef Array<unsigned char, static_size> Digest; 00050 private: 00051 Digest mData; 00052 public: 00058 struct Hasher { 00059 size_t operator() (const SHA256 &hash) const{ 00060 return *((size_t*)(hash.rawData().data())); 00061 } 00062 }; 00063 00064 unsigned int size()const{ 00065 return static_size; 00066 } 00071 std::string convertToHexString()const; 00072 std::string toString() const { return convertToHexString(); } 00076 Array<char,hex_size> convertToHex()const; 00080 const Digest &rawData()const { 00081 return mData; 00082 } 00083 00084 bool operator==(const SHA256& other) const { 00085 return mData==other.mData; 00086 } 00087 bool operator!=(const SHA256& other) const { 00088 return !(mData==other.mData); 00089 } 00090 bool operator<(const SHA256& other) const{ 00091 return mData<other.mData; 00092 } 00099 static SHA256 convertFromHex(const char*digest); 00105 static SHA256 convertFromHex(const std::string&digest); 00110 static SHA256 convertFromBinary(const Digest&digest) { 00111 return convertFromBinary(digest.data()); 00112 } 00118 static SHA256 convertFromBinary(const void*digest){ 00119 SHA256 retval; 00120 memcpy(retval.mData.data(),digest,static_size); 00121 return retval; 00122 } 00129 static SHA256 computeDigest(const void *data, size_t length); 00135 static SHA256 computeDigest(const std::string&data); 00139 static const SHA256&null(){ 00140 static SHA256 null0; 00141 static char empty_array[static_size]={0}; 00142 static void* result=std::memcpy(null0.mData.data(),empty_array,static_size); 00143 return result?null0:null0; 00144 } 00148 static const SHA256&emptyDigest() { 00149 static SHA256 empty=computeDigest(NULL,0); 00150 return empty; 00151 } 00152 00153 friend inline std::ostream &operator <<(std::ostream &os, const SHA256&shasum) { 00154 return os << shasum.convertToHexString(); 00155 } 00156 00157 }; 00158 00160 class SIRIKATA_EXPORT SHA256Context { 00162 SHA256Context(const SHA256Context &other); 00163 SHA256Context & operator=(const SHA256Context &other); 00164 00166 void *mCtx; 00167 00171 SHA256 mRetval; 00172 public: 00174 SHA256Context(); 00176 ~SHA256Context(); 00177 00179 void update(const void *data, size_t length); 00181 inline void update(const std::string &data) { 00182 update(data.data(), data.length()); 00183 } 00185 void updateZeros(size_t length); 00186 00188 const SHA256& get(); 00189 }; 00190 00191 } 00192 00193 #endif //_SIRIKATA_SHA256_HPP_