sha.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Copyright (c) 2021 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "sha.hpp"
  19. #if RTC_ENABLE_WEBSOCKET
  20. #if USE_GNUTLS
  21. #include <nettle/sha1.h>
  22. #else // USE_GNUTLS==0
  23. #ifndef OPENSSL_API_COMPAT
  24. #define OPENSSL_API_COMPAT 0x10100000L
  25. #endif
  26. #include <openssl/sha.h>
  27. #endif
  28. namespace rtc::impl {
  29. namespace {
  30. binary Sha1(const byte *data, size_t size) {
  31. #if USE_GNUTLS
  32. binary output(SHA1_DIGEST_SIZE);
  33. struct sha1_ctx ctx;
  34. sha1_init(&ctx);
  35. sha1_update(&ctx, size, reinterpret_cast<const uint8_t *>(data));
  36. sha1_digest(&ctx, SHA1_DIGEST_SIZE, reinterpret_cast<uint8_t *>(output.data()));
  37. return output;
  38. #else // USE_GNUTLS==0
  39. binary output(SHA_DIGEST_LENGTH);
  40. SHA_CTX ctx;
  41. SHA1_Init(&ctx);
  42. SHA1_Update(&ctx, data, size);
  43. SHA1_Final(reinterpret_cast<unsigned char *>(output.data()), &ctx);
  44. return output;
  45. #endif
  46. }
  47. } // namespace
  48. binary Sha1(const binary &input) { return Sha1(input.data(), input.size()); }
  49. binary Sha1(const string &input) {
  50. return Sha1(reinterpret_cast<const byte *>(input.data()), input.size());
  51. }
  52. } // namespace rtc::impl
  53. #endif