Team Fortress 2 Source Code as on 22/4/2020
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.

174 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, 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. #if !defined(_MINIMUM_BUILD_)
  23. #include <stdio.h> // Needed for file access
  24. #if defined( _PS3 )
  25. #include <sys/memory.h>
  26. #else
  27. #include <memory.h>
  28. #endif
  29. #include <string.h> // Needed for strcat and strcpy
  30. #endif
  31. // If you're compiling big endian, just comment out the following line
  32. #define SHA1_LITTLE_ENDIAN
  33. typedef union
  34. {
  35. unsigned char c[64];
  36. unsigned long l[16];
  37. } SHA1_WORKSPACE_BLOCK;
  38. // SHA1 hash
  39. const unsigned int k_cubHash = 20;
  40. const unsigned int k_cchHash = 41; // k_cubHash * 2, plus 1 for terminator
  41. #pragma pack( push, 1 )
  42. typedef unsigned char SHADigest_t[ k_cubHash ];
  43. #pragma pack( pop )
  44. #if !defined(_MINIMUM_BUILD_)
  45. class CSHA1
  46. #else
  47. class Minimum_CSHA1
  48. #endif
  49. {
  50. public:
  51. // Two different formats for ReportHash(...)
  52. enum
  53. {
  54. REPORT_HEX = 0,
  55. REPORT_DIGIT = 1
  56. };
  57. // Constructor and Destructor
  58. #if !defined(_MINIMUM_BUILD_)
  59. CSHA1();
  60. virtual ~CSHA1() ;
  61. #else
  62. Minimum_CSHA1() ;
  63. ~Minimum_CSHA1() ; // no virtual destructor's in the minimal builds !
  64. #endif
  65. unsigned long m_state[5];
  66. unsigned long m_count[2];
  67. unsigned char m_buffer[64];
  68. unsigned char m_digest[k_cubHash];
  69. void Reset();
  70. // Update the hash value
  71. void Update(unsigned char *data, unsigned int len);
  72. #if !defined(_MINIMUM_BUILD_)
  73. bool HashFile(char *szFileName);
  74. #endif
  75. // Finalize hash and report
  76. void Final();
  77. #if !defined(_MINIMUM_BUILD_)
  78. void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
  79. #endif
  80. void GetHash(unsigned char *uDest);
  81. private:
  82. // Private SHA-1 transformation
  83. void Transform(unsigned long state[5], unsigned char buffer[64]);
  84. // Member variables
  85. unsigned char m_workspace[64];
  86. SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
  87. };
  88. #define GenerateHash( hash, pubData, cubData ) { CSHA1 sha1; sha1.Update( (byte *)pubData, cubData ); sha1.Final(); sha1.GetHash( hash ); }
  89. #if !defined(_MINIMUM_BUILD_)
  90. // hash comparison function, for use with CUtlMap/CUtlRBTree
  91. bool HashLessFunc( SHADigest_t const &lhs, SHADigest_t const &rhs );
  92. // utility class for manipulating SHA1 hashes in their compact form
  93. struct CSHA
  94. {
  95. public:
  96. SHADigest_t m_shaDigest;
  97. CSHA()
  98. {
  99. memset( m_shaDigest, 0, k_cubHash );
  100. }
  101. explicit CSHA( const SHADigest_t rhs )
  102. {
  103. memcpy( m_shaDigest, rhs, k_cubHash );
  104. }
  105. SHADigest_t &SHADigest()
  106. {
  107. return m_shaDigest;
  108. }
  109. bool operator<( const CSHA &rhs ) const
  110. {
  111. return memcmp( m_shaDigest, rhs.m_shaDigest, k_cubHash ) < 0;
  112. }
  113. bool operator==( const CSHA &rhs ) const
  114. {
  115. return memcmp( m_shaDigest, rhs.m_shaDigest, k_cubHash ) == 0;
  116. }
  117. bool operator!=( const CSHA &rhs ) const
  118. {
  119. return !(*this == rhs);
  120. }
  121. bool operator==( const SHADigest_t &rhs ) const
  122. {
  123. return memcmp( m_shaDigest, rhs, k_cubHash ) == 0;
  124. }
  125. bool operator!=( const SHADigest_t &rhs ) const
  126. {
  127. return !(*this == rhs);
  128. }
  129. CSHA &operator=( const SHADigest_t rhs )
  130. {
  131. memcpy( m_shaDigest, rhs, k_cubHash );
  132. return *this;
  133. }
  134. void AssignTo( SHADigest_t rhs ) const
  135. {
  136. memcpy( rhs, m_shaDigest, k_cubHash );
  137. }
  138. };
  139. #endif // _MINIMUM_BUILD_
  140. #endif // CHECKSUM_SHA1_H