aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/crypto/sha.cpp
diff options
context:
space:
mode:
authorqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
committerqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
commit22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch)
treebffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/openssl/crypto/sha.cpp
parent332b99e2173f0425444abb759eebcb2fafaa9209 (diff)
downloadydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz
validate canons without yatest_common
Diffstat (limited to 'library/cpp/openssl/crypto/sha.cpp')
-rw-r--r--library/cpp/openssl/crypto/sha.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/library/cpp/openssl/crypto/sha.cpp b/library/cpp/openssl/crypto/sha.cpp
new file mode 100644
index 0000000000..c142b6635e
--- /dev/null
+++ b/library/cpp/openssl/crypto/sha.cpp
@@ -0,0 +1,62 @@
+#include "sha.h"
+
+#include <util/generic/yexception.h>
+
+#include <openssl/sha.h>
+
+namespace NOpenSsl {
+ namespace NSha1 {
+ static_assert(DIGEST_LENGTH == SHA_DIGEST_LENGTH);
+
+ TDigest Calc(const void* data, size_t dataSize) {
+ TDigest digest;
+ Y_ENSURE(SHA1(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
+ return digest;
+ }
+
+ TCalcer::TCalcer()
+ : Context{new SHAstate_st} {
+ Y_ENSURE(SHA1_Init(Context.Get()) == 1);
+ }
+
+ TCalcer::~TCalcer() {
+ }
+
+ void TCalcer::Update(const void* data, size_t dataSize) {
+ Y_ENSURE(SHA1_Update(Context.Get(), data, dataSize) == 1);
+ }
+
+ TDigest TCalcer::Final() {
+ TDigest digest;
+ Y_ENSURE(SHA1_Final(digest.data(), Context.Get()) == 1);
+ return digest;
+ }
+ }
+ namespace NSha256 {
+ static_assert(DIGEST_LENGTH == SHA256_DIGEST_LENGTH);
+
+ TDigest Calc(const void* data, size_t dataSize) {
+ TDigest digest;
+ Y_ENSURE(SHA256(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
+ return digest;
+ }
+
+ TCalcer::TCalcer()
+ : Context{new SHA256state_st} {
+ Y_ENSURE(SHA256_Init(Context.Get()) == 1);
+ }
+
+ TCalcer::~TCalcer() {
+ }
+
+ void TCalcer::Update(const void* data, size_t dataSize) {
+ Y_ENSURE(SHA256_Update(Context.Get(), data, dataSize) == 1);
+ }
+
+ TDigest TCalcer::Final() {
+ TDigest digest;
+ Y_ENSURE(SHA256_Final(digest.data(), Context.Get()) == 1);
+ return digest;
+ }
+ }
+}