Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
3.7 KiB

  1. //========= Copyright � 2005, Valve Inc., All rights reserved. ==========
  2. //
  3. // Purpose: Implementation of SHA-1
  4. //
  5. //=============================================================================
  6. #ifndef CHECKSUM_SHA1_H
  7. #define CHECKSUM_SHA1_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. /*
  12. 100% free public domain implementation of the SHA-1
  13. algorithm by Dominik Reichl <dominik.reichl@t-online.de>
  14. === Test Vectors (from FIPS PUB 180-1) ===
  15. SHA1("abc") =
  16. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  17. SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
  18. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  19. SHA1(A million repetitions of "a") =
  20. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  21. */
  22. #include "tier0/platform.h"
  23. #if !defined(_MINIMUM_BUILD_)
  24. #include <stdio.h> // Needed for file access
  25. #if defined( _PS3 )
  26. #include <sys/memory.h>
  27. #else
  28. #include <memory.h>
  29. #endif
  30. #include <string.h> // Needed for strcat and strcpy
  31. #endif
  32. // If you're compiling big endian, just comment out the following line
  33. #define SHA1_LITTLE_ENDIAN
  34. typedef union
  35. {
  36. uint8 c[64];
  37. uint32 l[16];
  38. } SHA1_WORKSPACE_BLOCK;
  39. // SHA1 hash
  40. const unsigned int k_cubHash = 20;
  41. const unsigned int k_cchHash = 41; // k_cubHash * 2, plus 1 for terminator
  42. #pragma pack( push, 1 )
  43. typedef uint8 SHADigest_t[ k_cubHash ];
  44. #pragma pack( pop )
  45. #if !defined(_MINIMUM_BUILD_)
  46. class CSHA1
  47. #else
  48. class Minimum_CSHA1
  49. #endif
  50. {
  51. public:
  52. // Two different formats for ReportHash(...)
  53. enum
  54. {
  55. REPORT_HEX = 0,
  56. REPORT_DIGIT = 1
  57. };
  58. // Constructor and Destructor
  59. #if !defined(_MINIMUM_BUILD_)
  60. CSHA1();
  61. virtual ~CSHA1() ;
  62. #else
  63. Minimum_CSHA1() ;
  64. ~Minimum_CSHA1() ; // no virtual destructor's in the minimal builds !
  65. #endif
  66. unsigned int m_state[5];
  67. unsigned int m_count[2];
  68. uint8 m_buffer[64];
  69. uint8 m_digest[k_cubHash];
  70. void Reset();
  71. // Update the hash value
  72. void Update( const void *data, unsigned int len );
  73. #if !defined(_MINIMUM_BUILD_)
  74. bool HashFile( const char *szFileName );
  75. #endif
  76. // Finalize hash and report
  77. void Final();
  78. #if !defined(_MINIMUM_BUILD_)
  79. void ReportHash(char *szReport, uint8 uReportType = REPORT_HEX);
  80. #endif
  81. void GetHash(uint8 *uDest);
  82. private:
  83. // Private SHA-1 transformation
  84. void Transform(unsigned int state[5], const uint8 buffer[64]);
  85. // Member variables
  86. uint8 m_workspace[64];
  87. SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
  88. };
  89. #define GenerateHash( hash, pubData, cubData ) { CSHA1 sha1; sha1.Update( (byte *)pubData, cubData ); sha1.Final(); sha1.GetHash( hash ); }
  90. #if !defined(_MINIMUM_BUILD_)
  91. // hash comparison function, for use with CUtlMap/CUtlRBTree
  92. bool HashLessFunc( SHADigest_t const &lhs, SHADigest_t const &rhs );
  93. // utility class for manipulating SHA1 hashes in their compact form
  94. struct CSHA
  95. {
  96. public:
  97. SHADigest_t m_shaDigest;
  98. CSHA()
  99. {
  100. memset( m_shaDigest, 0, k_cubHash );
  101. }
  102. explicit CSHA( const SHADigest_t rhs )
  103. {
  104. memcpy( m_shaDigest, rhs, k_cubHash );
  105. }
  106. SHADigest_t &SHADigest()
  107. {
  108. return m_shaDigest;
  109. }
  110. bool operator<( const CSHA &rhs ) const
  111. {
  112. return memcmp( m_shaDigest, rhs.m_shaDigest, k_cubHash ) < 0;
  113. }
  114. bool operator==( const CSHA &rhs ) const
  115. {
  116. return memcmp( m_shaDigest, rhs.m_shaDigest, k_cubHash ) == 0;
  117. }
  118. bool operator!=( const CSHA &rhs ) const
  119. {
  120. return !(*this == rhs);
  121. }
  122. bool operator==( const SHADigest_t &rhs ) const
  123. {
  124. return memcmp( m_shaDigest, rhs, k_cubHash ) == 0;
  125. }
  126. bool operator!=( const SHADigest_t &rhs ) const
  127. {
  128. return !(*this == rhs);
  129. }
  130. CSHA &operator=( const SHADigest_t rhs )
  131. {
  132. memcpy( m_shaDigest, rhs, k_cubHash );
  133. return *this;
  134. }
  135. void AssignTo( SHADigest_t rhs ) const
  136. {
  137. memcpy( rhs, m_shaDigest, k_cubHash );
  138. }
  139. };
  140. #endif // _MINIMUM_BUILD_
  141. #endif // CHECKSUM_SHA1_H