sha1.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* sha1.h
  2. Copyright (c) 2005 Michael D. Leonhard
  3. http://tamale.net/
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #pragma once
  21. #include <cstdint>
  22. #include <string>
  23. #include <string_view>
  24. typedef uint32_t Uint32;
  25. class SHA1
  26. {
  27. private:
  28. // fields
  29. Uint32 H0 = 0x67452301;
  30. Uint32 H1 = 0xefcdab89;
  31. Uint32 H2 = 0x98badcfe;
  32. Uint32 H3 = 0x10325476;
  33. Uint32 H4 = 0xc3d2e1f0;
  34. unsigned char bytes[64];
  35. Uint32 unprocessedBytes = 0;
  36. Uint32 size = 0;
  37. void process();
  38. public:
  39. SHA1();
  40. ~SHA1();
  41. void addBytes(const char *data, Uint32 num);
  42. inline void addBytes(std::string_view data) {
  43. addBytes(data.data(), data.size());
  44. }
  45. void getDigest(unsigned char *to);
  46. inline std::string getDigest() {
  47. std::string ret(20, '\000');
  48. getDigest(reinterpret_cast<unsigned char*>(ret.data()));
  49. return ret;
  50. }
  51. // utility methods
  52. static Uint32 lrot(Uint32 x, int bits);
  53. static void storeBigEndianUint32(unsigned char *byte, Uint32 num);
  54. static void hexPrinter(unsigned char *c, int l);
  55. };