sha.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  23. #include <openssl/sha.h>
  24. #endif
  25. namespace rtc::impl {
  26. namespace {
  27. binary Sha1(const byte *data, size_t size) {
  28. #if USE_GNUTLS
  29. binary output(SHA1_DIGEST_SIZE);
  30. struct sha1_ctx ctx;
  31. sha1_init(&ctx);
  32. sha1_update(&ctx, size, reinterpret_cast<const uint8_t *>(data));
  33. sha1_digest(&ctx, SHA1_DIGEST_SIZE, reinterpret_cast<uint8_t *>(output.data()));
  34. return output;
  35. #else // USE_GNUTLS==0
  36. binary output(SHA_DIGEST_LENGTH);
  37. SHA_CTX ctx;
  38. SHA1_Init(&ctx);
  39. SHA1_Update(&ctx, data, size);
  40. SHA1_Final(reinterpret_cast<unsigned char *>(output.data()), &ctx);
  41. return output;
  42. #endif
  43. }
  44. } // namespace
  45. binary Sha1(const binary &input) { return Sha1(input.data(), input.size()); }
  46. binary Sha1(const string &input) {
  47. return Sha1(reinterpret_cast<const byte *>(input.data()), input.size());
  48. }
  49. } // namespace rtc::impl
  50. #endif