hl_sha256.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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++ SHA256 implementation is derivative from the sourcecode
  31. * published by Aaron D. Gifford
  32. *
  33. * Copyright (c) 2000-2001, Aaron D. Gifford
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in the
  43. * documentation and/or other materials provided with the distribution.
  44. * 3. Neither the name of the copyright holder nor the names of contributors
  45. * may be used to endorse or promote products derived from this software
  46. * without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  49. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  52. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58. * SUCH DAMAGE.
  59. */
  60. //----------------------------------------------------------------------
  61. /**
  62. * @file hl_sha256.h
  63. * @brief This file contains the declaration of the SHA256 class
  64. * @date Di 25 Sep 2007
  65. */
  66. //----------------------------------------------------------------------
  67. //include protection
  68. #ifndef SHA256_H
  69. #define SHA256_H
  70. //----------------------------------------------------------------------
  71. //lenght defines
  72. #define SHA256_BLOCK_LENGTH 64
  73. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  74. #define SHA256_DIGEST_LENGTH 32
  75. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  76. //----------------------------------------------------------------------
  77. //hl includes
  78. #include "hl_types.h"
  79. //----------------------------------------------------------------------
  80. //typedefs
  81. /**
  82. * Exactly 1 byte
  83. */
  84. typedef hl_uint8 sha2_byte;
  85. /**
  86. * Exactly 4 bytes
  87. */
  88. typedef hl_uint32 sha2_word32;
  89. /**
  90. * Exactly 8 bytes
  91. */
  92. typedef hl_uint64 sha2_word64;
  93. /**
  94. * @brief This struct represents a SHA256-hash context
  95. */
  96. typedef struct HL_SHA256_CTX
  97. {
  98. /**
  99. * state
  100. */
  101. hl_uint32 state[8];
  102. /**
  103. * bitcount
  104. */
  105. hl_uint64 bitcount;
  106. /**
  107. * message buffer
  108. */
  109. hl_uint8 buffer[SHA256_BLOCK_LENGTH];
  110. } HL_SHA256_CTX;
  111. //----------------------------------------------------------------------
  112. /**
  113. * @brief This class represents the implementation of
  114. * the sha256 algorithm.
  115. *
  116. * Basically the class provides three public member-functions
  117. * to create a hash: SHA256_Init(), SHA256_Update() and SHA256_End().
  118. * If you want to create a hash based on a string or file quickly
  119. * you should use the sha256wrapper class instead of SHA256.
  120. */
  121. class SHA256
  122. {
  123. private:
  124. /**
  125. * @brief Finalize the sha256 operation
  126. * @param digest The digest to finalize the operation with.
  127. * @param context The context to finalize.
  128. */
  129. void SHA256_Final(hl_uint8 digest[SHA256_DIGEST_LENGTH],
  130. HL_SHA256_CTX* context);
  131. /**
  132. * @brief Internal data transformation
  133. * @param context The context to use
  134. * @param data The data to transform
  135. */
  136. void SHA256_Transform(HL_SHA256_CTX* context,
  137. const sha2_word32* data);
  138. public:
  139. /**
  140. * @brief Initialize the context
  141. * @param context The context to init.
  142. */
  143. void SHA256_Init(HL_SHA256_CTX *context);
  144. /**
  145. * @brief Updates the context
  146. * @param context The context to update.
  147. * @param data The data for updating the context.
  148. * @param len The length of the given data.
  149. */
  150. void SHA256_Update(HL_SHA256_CTX* context,
  151. const hl_uint8* data,
  152. unsigned int len);
  153. /**
  154. * @brief Ends the sha256 operation and return the
  155. * created hash in the given buffer.
  156. * @param context The context to end.
  157. * @param buffer This OUT-Parameter contains the created
  158. * hash after ending the operation.
  159. */
  160. char* SHA256_End(HL_SHA256_CTX* context,
  161. char buffer[SHA256_DIGEST_STRING_LENGTH]);
  162. };
  163. //----------------------------------------------------------------------
  164. //end of include protection
  165. #endif
  166. //----------------------------------------------------------------------
  167. //EOF