hl_sha1.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2010 Benjamin Grüdelbach
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1) Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2) Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. //----------------------------------------------------------------------
  29. /*
  30. * The hashlib++ SHA1 implementation is derivative from the sourcecode
  31. * published in RFC 3174
  32. *
  33. * Copyright (C) The Internet Society (2001). All Rights Reserved.
  34. *
  35. * This document and translations of it may be copied and furnished to
  36. * others, and derivative works that comment on or otherwise explain it
  37. * or assist in its implementation may be prepared, copied, published
  38. * and distributed, in whole or in part, without restriction of any
  39. * kind, provided that the above copyright notice and this paragraph are
  40. * included on all such copies and derivative works. However, this
  41. * document itself may not be modified in any way, such as by removing
  42. * the copyright notice or references to the Internet Society or other
  43. * Internet organizations, except as needed for the purpose of
  44. * developing Internet standards in which case the procedures for
  45. * copyrights defined in the Internet Standards process must be
  46. * followed, or as required to translate it into languages other than
  47. * English.
  48. *
  49. * The limited permissions granted above are perpetual and will not be
  50. * revoked by the Internet Society or its successors or assigns.
  51. *
  52. * This document and the information contained herein is provided on an
  53. * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  54. * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  55. * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  56. * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  57. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  58. */
  59. //----------------------------------------------------------------------
  60. /**
  61. * @file hl_sha1.h
  62. * @brief This file contains the declaration of the SHA1 class
  63. * @date Mo 17 Sep 2007
  64. */
  65. //----------------------------------------------------------------------
  66. //include protection
  67. #ifndef SHA1_H
  68. #define SHA1_H
  69. //----------------------------------------------------------------------
  70. //hl includes
  71. #include "hl_types.h"
  72. //----------------------------------------------------------------------
  73. //enums
  74. #ifndef _SHA_enum_
  75. #define _SHA_enum_
  76. enum
  77. {
  78. shaSuccess = 0,
  79. shaNull, /* Null pointer parameter */
  80. shaInputTooLong, /* input data too long */
  81. shaStateError /* called Input after Result */
  82. };
  83. #endif
  84. //----------------------------------------------------------------------
  85. //defines
  86. #define SHA1HashSize 20
  87. //----------------------------------------------------------------------
  88. //structs
  89. /**
  90. * @brief this struct represents a SHA1-hash context.
  91. */
  92. typedef struct HL_SHA1_CTX
  93. {
  94. /** Message Digest */
  95. hl_uint32 Intermediate_Hash[SHA1HashSize/4];
  96. /** Message length in bits */
  97. hl_uint32 Length_Low;
  98. /** Message length in bits */
  99. hl_uint32 Length_High;
  100. /** Index into message block array */
  101. hl_uint16 Message_Block_Index;
  102. /** 512-bit message blocks */
  103. hl_uint8 Message_Block[64];
  104. /** Is the digest computed? */
  105. int Computed;
  106. /** Is the message digest corrupted? */
  107. int Corrupted;
  108. } HL_SHA1_CTX;
  109. //----------------------------------------------------------------------
  110. //class definition
  111. /**
  112. * @brief This class represents the implementation of
  113. * the sha1 algorithm.
  114. *
  115. * Basically the class provides three public member-functions
  116. * to create a hash: SHA1Reset(), SHA1Input() and SHA1Result().
  117. * If you want to create a hash based on a string or file quickly
  118. * you should use the sha1wrapper class instead of SHA1.
  119. */
  120. class SHA1
  121. {
  122. private:
  123. /**
  124. * @brief Internal method to padd the message
  125. *
  126. * According to the standard, the message must
  127. * be padded to an even 512 bits. The first
  128. * padding bit must be a '1'. The last 64 bits
  129. * represent the length of the original message.
  130. * All bits in between should be 0.
  131. * This function will pad the message according
  132. * to those rules by filling the Message_Block array
  133. * accordingly. It will also call the
  134. * ProcessMessageBlock function provided appropriately.
  135. * When it returns, it can be assumed that the message
  136. * digest has been computed.
  137. *
  138. * @param context The context to padd
  139. *
  140. */
  141. void SHA1PadMessage(HL_SHA1_CTX *context);
  142. /**
  143. * @brief This member-function will process the next 512 bits of the
  144. * message stored in the Message_Block array.
  145. *
  146. * Many of the variable names in this code, especially the
  147. * single character names, were used because those were the
  148. * names used in the publication.
  149. *
  150. * @param context The context to process
  151. */
  152. void SHA1ProcessMessageBlock(HL_SHA1_CTX *context);
  153. public:
  154. /**
  155. * @brief Resets the sha1 context and starts a new
  156. * hashprocess
  157. * @param context The context to reset
  158. * @return 0 on succes an error number otherwise
  159. */
  160. int SHA1Reset( HL_SHA1_CTX *context);
  161. /**
  162. * @brief Data input.
  163. *
  164. * This memberfunction add data to the specified
  165. * context.
  166. *
  167. * @param context The context to add data to
  168. * @param message_array The data to add
  169. * @param length The length of the data to add
  170. */
  171. int SHA1Input( HL_SHA1_CTX *context,
  172. const hl_uint8 *message_array,
  173. unsigned int length);
  174. /**
  175. * @brief This ends the sha operation, zeroizing the context
  176. * and returning the computed hash.
  177. *
  178. * @param context The context to get the hash from
  179. * @param Message_Digest This is an OUT parameter which
  180. * contains the hash after the menberfunction returns
  181. * @return 0 on succes, an error-code otherwise
  182. */
  183. int SHA1Result( HL_SHA1_CTX *context,
  184. hl_uint8 Message_Digest[SHA1HashSize]);
  185. };
  186. //----------------------------------------------------------------------
  187. //end of include protection
  188. #endif
  189. //----------------------------------------------------------------------
  190. //EOF