/thirdparty/liblastfm2/src/fingerprint/Sha256.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 180 lines · 69 code · 30 blank · 81 comment · 1 complexity · 597e2a1000eebe0bbc354ffeeb1e8044 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * $Id: sha256.h 348 2003-02-23 22:12:06Z asaddi $
  27. */
  28. //
  29. /////////// EXAMPLE /////////////////////////////////
  30. //
  31. // SHA256Context sha256;
  32. // SHA256Init (&sha256);
  33. //
  34. // uint8_t* pBuffer = new uint8_t[SHA_BUFFER_SIZE + 7];
  35. // // Ensure it is on a 64-bit boundary.
  36. // INTPTR offs;
  37. // if ((offs = reinterpret_cast<INTPTR>(pBuffer) & 7L))
  38. // pBuffer += 8 - offs;
  39. //
  40. // unsigned int len;
  41. //
  42. // ifstream inFile("test.txt", ios::binary);
  43. //
  44. // for (;;)
  45. // {
  46. // inFile.read( reinterpret_cast<char*>(pBuffer), SHA_BUFFER_SIZE );
  47. // len = inFile.gcount();
  48. //
  49. // if ( len == 0)
  50. // break;
  51. //
  52. // SHA256Update (&sha256, pBuffer, len);
  53. // }
  54. //
  55. // uint8_t hash[SHA256_HASH_SIZE];
  56. // SHA256Final (&sha256, hash);
  57. //
  58. // cout << "Hash: ";
  59. // for (int i = 0; i < SHA256_HASH_SIZE; ++i)
  60. // printf ("%02x", hash[i]);
  61. // cout << endl;
  62. #ifndef _SHA256_H
  63. #define _SHA256_H
  64. // ----------------------------------------------------------------------------
  65. // ----------------------------------------------------------------------------
  66. /* Define to 1 if you have the <inttypes.h> header file. */
  67. #ifndef WIN32
  68. #define HAVE_INTTYPES_H 1
  69. #endif
  70. /* Define to 1 if you have the <memory.h> header file. */
  71. #define HAVE_MEMORY_H 1
  72. /* Define to 1 if you have the <stdint.h> header file. */
  73. #ifndef WIN32
  74. #define HAVE_STDINT_H 1
  75. #endif
  76. /* Define to 1 if you have the <stdlib.h> header file. */
  77. #define HAVE_STDLIB_H 1
  78. /* Define to 1 if you have the <strings.h> header file. */
  79. #define HAVE_STRINGS_H 1
  80. /* Define to 1 if you have the <string.h> header file. */
  81. #define HAVE_STRING_H 1
  82. /* Define to 1 if you have the `strerror' function. */
  83. #ifndef WIN32
  84. #define HAVE_STRERROR 1
  85. #endif
  86. /* Define to 1 if you have the <sys/stat.h> header file. */
  87. #define HAVE_SYS_STAT_H 1
  88. /* Define to 1 if you have the <sys/types.h> header file. */
  89. #ifndef WIN32
  90. #define HAVE_SYS_TYPES_H 1
  91. #endif
  92. /* Define to 1 if you have the <unistd.h> header file. */
  93. #ifndef WIN32
  94. #define HAVE_UNISTD_H 1
  95. #endif
  96. /* Define to 1 if you have the ANSI C header files. */
  97. #define STDC_HEADERS 1
  98. /* Define as `__inline' if that's what the C compiler calls it, or to nothing
  99. if it is not supported. */
  100. #ifdef WIN32
  101. #define inline __inline
  102. #endif
  103. /* Define to `unsigned' if <sys/types.h> does not define. */
  104. /* #undef size_t */
  105. #ifdef WIN32
  106. #define uint64_t unsigned __int64
  107. #define uint32_t unsigned int
  108. #define uint8_t unsigned char
  109. #endif // WIN32
  110. #ifdef WIN32
  111. #define INTPTR intptr_t
  112. #else
  113. #define INTPTR long
  114. #endif
  115. #define SHA_BUFFER_SIZE 65536
  116. // ----------------------------------------------------------------------------
  117. // ----------------------------------------------------------------------------
  118. #if HAVE_INTTYPES_H
  119. # include <inttypes.h>
  120. #else
  121. # if HAVE_STDINT_H
  122. # include <stdint.h>
  123. # endif
  124. #endif
  125. #define SHA256_HASH_SIZE 32
  126. /* Hash size in 32-bit words */
  127. #define SHA256_HASH_WORDS 8
  128. struct _SHA256Context {
  129. uint64_t totalLength;
  130. uint32_t hash[SHA256_HASH_WORDS];
  131. uint32_t bufferLength;
  132. union {
  133. uint32_t words[16];
  134. uint8_t bytes[64];
  135. } buffer;
  136. #ifdef RUNTIME_ENDIAN
  137. int littleEndian;
  138. #endif /* RUNTIME_ENDIAN */
  139. };
  140. typedef struct _SHA256Context SHA256Context;
  141. #ifdef __cplusplus
  142. extern "C" {
  143. #endif
  144. void SHA256Init (SHA256Context *sc);
  145. void SHA256Update (SHA256Context *sc, const void *data, uint32_t len);
  146. void SHA256Final (SHA256Context *sc, uint8_t hash[SHA256_HASH_SIZE]);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* !_SHA256_H */