sha.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "sha.h"
  2. #include <util/generic/yexception.h>
  3. #include <openssl/sha.h>
  4. namespace NOpenSsl {
  5. namespace NSha1 {
  6. static_assert(DIGEST_LENGTH == SHA_DIGEST_LENGTH);
  7. TDigest Calc(const void* data, size_t dataSize) {
  8. TDigest digest;
  9. Y_ENSURE(SHA1(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
  10. return digest;
  11. }
  12. TCalcer::TCalcer()
  13. : Context{new SHAstate_st} {
  14. Y_ENSURE(SHA1_Init(Context.Get()) == 1);
  15. }
  16. TCalcer::~TCalcer() {
  17. }
  18. void TCalcer::Update(const void* data, size_t dataSize) {
  19. Y_ENSURE(SHA1_Update(Context.Get(), data, dataSize) == 1);
  20. }
  21. TDigest TCalcer::Final() {
  22. TDigest digest;
  23. Y_ENSURE(SHA1_Final(digest.data(), Context.Get()) == 1);
  24. return digest;
  25. }
  26. }
  27. namespace NSha256 {
  28. static_assert(DIGEST_LENGTH == SHA256_DIGEST_LENGTH);
  29. TDigest Calc(const void* data, size_t dataSize) {
  30. TDigest digest;
  31. Y_ENSURE(SHA256(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
  32. return digest;
  33. }
  34. TCalcer::TCalcer()
  35. : Context{new SHA256state_st} {
  36. Y_ENSURE(SHA256_Init(Context.Get()) == 1);
  37. }
  38. TCalcer::~TCalcer() {
  39. }
  40. void TCalcer::Update(const void* data, size_t dataSize) {
  41. Y_ENSURE(SHA256_Update(Context.Get(), data, dataSize) == 1);
  42. }
  43. TDigest TCalcer::Final() {
  44. TDigest digest;
  45. Y_ENSURE(SHA256_Final(digest.data(), Context.Get()) == 1);
  46. return digest;
  47. }
  48. }
  49. namespace NSha224 {
  50. static_assert(DIGEST_LENGTH == SHA224_DIGEST_LENGTH);
  51. TDigest Calc(const void* data, size_t dataSize) {
  52. TDigest digest;
  53. Y_ENSURE(SHA224(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
  54. return digest;
  55. }
  56. TCalcer::TCalcer()
  57. : Context{new SHA256state_st} {
  58. Y_ENSURE(SHA224_Init(Context.Get()) == 1);
  59. }
  60. TCalcer::~TCalcer() {
  61. }
  62. void TCalcer::Update(const void* data, size_t dataSize) {
  63. Y_ENSURE(SHA224_Update(Context.Get(), data, dataSize) == 1);
  64. }
  65. TDigest TCalcer::Final() {
  66. TDigest digest;
  67. Y_ENSURE(SHA224_Final(digest.data(), Context.Get()) == 1);
  68. return digest;
  69. }
  70. }
  71. }