diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/digest/md5 | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/digest/md5')
-rw-r--r-- | library/cpp/digest/md5/bench/main.cpp | 24 | ||||
-rw-r--r-- | library/cpp/digest/md5/bench/ya.make | 20 | ||||
-rw-r--r-- | library/cpp/digest/md5/md5.cpp | 94 | ||||
-rw-r--r-- | library/cpp/digest/md5/md5.h | 24 | ||||
-rw-r--r-- | library/cpp/digest/md5/md5_medium_ut.cpp | 14 | ||||
-rw-r--r-- | library/cpp/digest/md5/md5_ut.cpp | 26 | ||||
-rw-r--r-- | library/cpp/digest/md5/medium_ut/ya.make | 12 | ||||
-rw-r--r-- | library/cpp/digest/md5/ut/ya.make | 14 | ||||
-rw-r--r-- | library/cpp/digest/md5/ya.make | 18 |
9 files changed, 123 insertions, 123 deletions
diff --git a/library/cpp/digest/md5/bench/main.cpp b/library/cpp/digest/md5/bench/main.cpp index a987eefe59..70672e6741 100644 --- a/library/cpp/digest/md5/bench/main.cpp +++ b/library/cpp/digest/md5/bench/main.cpp @@ -1,19 +1,19 @@ #include <benchmark/benchmark.h> #include <library/cpp/digest/md5/md5.h> - -#define MD5_DEF(N) \ + +#define MD5_DEF(N) \ static void MD5Benchmark_##N(benchmark::State& st) { \ - char buf[N]; \ + char buf[N]; \ for (auto _ : st) { \ - Y_DO_NOT_OPTIMIZE_AWAY(MD5().Update(buf, sizeof(buf))); \ - } \ + Y_DO_NOT_OPTIMIZE_AWAY(MD5().Update(buf, sizeof(buf))); \ + } \ } \ BENCHMARK(MD5Benchmark_##N); - -MD5_DEF(32) -MD5_DEF(64) -MD5_DEF(128) - -MD5_DEF(1024) -MD5_DEF(2048) + +MD5_DEF(32) +MD5_DEF(64) +MD5_DEF(128) + +MD5_DEF(1024) +MD5_DEF(2048) diff --git a/library/cpp/digest/md5/bench/ya.make b/library/cpp/digest/md5/bench/ya.make index 0c19f330a0..5c3e3c1c42 100644 --- a/library/cpp/digest/md5/bench/ya.make +++ b/library/cpp/digest/md5/bench/ya.make @@ -1,16 +1,16 @@ G_BENCHMARK() - + OWNER( pg g:util ) - -PEERDIR( + +PEERDIR( library/cpp/digest/md5 -) - -SRCS( - main.cpp -) - -END() +) + +SRCS( + main.cpp +) + +END() diff --git a/library/cpp/digest/md5/md5.cpp b/library/cpp/digest/md5/md5.cpp index 4246270741..24a5b69eef 100644 --- a/library/cpp/digest/md5/md5.cpp +++ b/library/cpp/digest/md5/md5.cpp @@ -1,31 +1,31 @@ #include "md5.h" #include <library/cpp/string_utils/base64/base64.h> - + #include <util/stream/input.h> -#include <util/stream/file.h> +#include <util/stream/file.h> #include <util/string/hex.h> #include <contrib/libs/nayuki_md5/md5.h> - -namespace { + +namespace { constexpr size_t MD5_BLOCK_LENGTH = 64; constexpr size_t MD5_PADDING_SHIFT = 56; constexpr size_t MD5_HEX_DIGEST_LENGTH = 32; struct TMd5Stream: public IOutputStream { - inline TMd5Stream(MD5* md5) - : M_(md5) - { - } - + inline TMd5Stream(MD5* md5) + : M_(md5) + { + } + void DoWrite(const void* buf, size_t len) override { - M_->Update(buf, len); - } - - MD5* M_; - }; + M_->Update(buf, len); + } + + MD5* M_; + }; inline TArrayRef<const ui8> MakeUnsignedArrayRef(const void* data, const size_t size) { return MakeArrayRef(static_cast<const ui8*>(data), size); @@ -34,16 +34,16 @@ namespace { inline TArrayRef<const ui8> MakeUnsignedArrayRef(const TArrayRef<const char>& data) { return MakeUnsignedArrayRef(data.data(), data.size()); } -} - -char* MD5::File(const char* filename, char* buf) { - try { +} + +char* MD5::File(const char* filename, char* buf) { + try { TUnbufferedFileInput fi(filename); - - return Stream(&fi, buf); - } catch (...) { + + return Stream(&fi, buf); + } catch (...) { } - + return nullptr; } @@ -77,25 +77,25 @@ TString MD5::Data(TStringBuf data) { } char* MD5::Stream(IInputStream* in, char* buf) { - return MD5().Update(in).End(buf); -} - + return MD5().Update(in).End(buf); +} + MD5& MD5::Update(IInputStream* in) { - TMd5Stream md5(this); - + TMd5Stream md5(this); + TransferData(in, &md5); - - return *this; + + return *this; } static const ui8 PADDING[MD5_BLOCK_LENGTH] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* MD5 initialization. Begins an MD5 operation, writing a new context. */ -void MD5::Init() { +void MD5::Init() { BufferSize = 0; StreamSize = 0; /* Load magic initialization constants. */ @@ -105,7 +105,7 @@ void MD5::Init() { State[3] = 0x10325476; } -/* +/* * MD5 block update operation. Continues an MD5 message-digest * operation, processing another message block, and updating the * context. @@ -141,7 +141,7 @@ void MD5::UpdatePart(TArrayRef<const ui8> data) { * MD5 padding. Adds padding followed by original length. */ -void MD5::Pad() { +void MD5::Pad() { size_t streamSize = StreamSize; const size_t paddingSize = (MD5_PADDING_SHIFT > BufferSize) ? (MD5_PADDING_SHIFT - BufferSize) : (MD5_PADDING_SHIFT + MD5_BLOCK_LENGTH - BufferSize); @@ -169,16 +169,16 @@ ui8* MD5::Final(ui8 digest[16]) { memcpy(digest, State, 16); /* Zeroize sensitive information. */ Init(); - - return digest; + + return digest; } -char* MD5::End(char* buf) { - static const char hex[] = "0123456789abcdef"; +char* MD5::End(char* buf) { + static const char hex[] = "0123456789abcdef"; ui8 digest[16]; - if (!buf) + if (!buf) buf = (char*)malloc(33); - if (!buf) + if (!buf) return nullptr; Final(digest); for (ui8 i = 0; i < MD5_HEX_DIGEST_LENGTH / 2; i++) { @@ -189,14 +189,14 @@ char* MD5::End(char* buf) { return buf; } -char* MD5::End_b64(char* buf) { +char* MD5::End_b64(char* buf) { ui8 digest[16]; - if (!buf) + if (!buf) buf = (char*)malloc(25); - if (!buf) + if (!buf) return nullptr; Final(digest); - Base64Encode(buf, digest, 16); + Base64Encode(buf, digest, 16); buf[24] = '\0'; return buf; } @@ -215,11 +215,11 @@ ui64 MD5::EndHalfMix() { TString MD5::Calc(TStringBuf data) { return Calc(MakeUnsignedArrayRef(data)); } - + TString MD5::Calc(const TArrayRef<const ui8>& data) { return Data(data); } - + TString MD5::CalcRaw(TStringBuf data) { return CalcRaw(MakeUnsignedArrayRef(data)); } diff --git a/library/cpp/digest/md5/md5.h b/library/cpp/digest/md5/md5.h index 014323adea..2c17aa0518 100644 --- a/library/cpp/digest/md5/md5.h +++ b/library/cpp/digest/md5/md5.h @@ -5,30 +5,30 @@ class IInputStream; -class MD5 { +class MD5 { public: MD5() { Init(); } - - void Init(); - + + void Init(); + inline MD5& Update(const void* data, size_t len) { return Update(MakeArrayRef(static_cast<const ui8*>(data), len)); } - + inline MD5& Update(TStringBuf data) { return Update(data.data(), data.size()); } - + inline MD5& Update(const TArrayRef<const ui8> data) { UpdatePart(data); return *this; - } - - void Pad(); + } + + void Pad(); ui8* Final(ui8[16]); - + // buf must be char[33]; char* End(char* buf); @@ -39,7 +39,7 @@ public: ui64 EndHalfMix(); MD5& Update(IInputStream* in); - + /* * Return hex-encoded md5 checksum for given file. * @@ -53,7 +53,7 @@ public: static TString Data(const TArrayRef<const ui8>& data); static TString Data(TStringBuf data); static char* Stream(IInputStream* in, char* buf); - + static TString Calc(TStringBuf data); // 32-byte hex-encoded static TString Calc(const TArrayRef<const ui8>& data); // 32-byte hex-encoded static TString CalcRaw(TStringBuf data); // 16-byte raw diff --git a/library/cpp/digest/md5/md5_medium_ut.cpp b/library/cpp/digest/md5/md5_medium_ut.cpp index 11a8a1276c..a940c5cb66 100644 --- a/library/cpp/digest/md5/md5_medium_ut.cpp +++ b/library/cpp/digest/md5/md5_medium_ut.cpp @@ -1,24 +1,24 @@ -#include "md5.h" - +#include "md5.h" + #include <library/cpp/testing/unittest/registar.h> Y_UNIT_TEST_SUITE(TMD5MediumTest) { Y_UNIT_TEST(TestOverflow) { - if (sizeof(size_t) > sizeof(unsigned int)) { + if (sizeof(size_t) > sizeof(unsigned int)) { const size_t maxUi32 = (size_t)Max<unsigned int>(); TArrayHolder<char> buf(new char[maxUi32]); - + memset(buf.Get(), 0, maxUi32); - + MD5 r; for (int i = 0; i < 5; ++i) { r.Update(buf.Get(), maxUi32); } - + char rs[33]; TString s(r.End(rs)); s.to_lower(); - + UNIT_ASSERT_VALUES_EQUAL(s, "34a5a7ed4f0221310084e21a1e599659"); } } diff --git a/library/cpp/digest/md5/md5_ut.cpp b/library/cpp/digest/md5/md5_ut.cpp index 8216fa463a..1c3e4ad0a9 100644 --- a/library/cpp/digest/md5/md5_ut.cpp +++ b/library/cpp/digest/md5/md5_ut.cpp @@ -1,44 +1,44 @@ -#include "md5.h" - +#include "md5.h" + #include <library/cpp/testing/unittest/registar.h> -#include <util/system/fs.h> -#include <util/stream/file.h> - +#include <util/system/fs.h> +#include <util/stream/file.h> + Y_UNIT_TEST_SUITE(TMD5Test) { Y_UNIT_TEST(TestMD5) { // echo -n 'qwertyuiopqwertyuiopasdfghjklasdfghjkl' | md5sum constexpr const char* b = "qwertyuiopqwertyuiopasdfghjklasdfghjkl"; - + MD5 r; r.Update((const unsigned char*)b, 15); r.Update((const unsigned char*)b + 15, strlen(b) - 15); - + char rs[33]; TString s(r.End(rs)); s.to_lower(); - + UNIT_ASSERT_NO_DIFF(s, TStringBuf("3ac00dd696b966fd74deee3c35a59d8f")); - + TString result = r.Calc(TStringBuf(b)); result.to_lower(); UNIT_ASSERT_NO_DIFF(result, TStringBuf("3ac00dd696b966fd74deee3c35a59d8f")); } - + Y_UNIT_TEST(TestFile) { TString s = NUnitTest::RandomString(1000000, 1); const TString tmpFile = "tmp"; - + { TFixedBufferFileOutput fo(tmpFile); fo.Write(s.data(), s.size()); } - + char fileBuf[100]; char memBuf[100]; TString fileHash = MD5::File(tmpFile.data(), fileBuf); TString memoryHash = MD5::Data((const unsigned char*)s.data(), s.size(), memBuf); - + UNIT_ASSERT_NO_DIFF(fileHash, memoryHash); fileHash = MD5::File(tmpFile); diff --git a/library/cpp/digest/md5/medium_ut/ya.make b/library/cpp/digest/md5/medium_ut/ya.make index 5d4f2bbce7..418c57f086 100644 --- a/library/cpp/digest/md5/medium_ut/ya.make +++ b/library/cpp/digest/md5/medium_ut/ya.make @@ -1,5 +1,5 @@ UNITTEST_FOR(library/cpp/digest/md5) - + SIZE(MEDIUM) TIMEOUT(120) @@ -8,11 +8,11 @@ OWNER( pg g:util ) - -SRCS( + +SRCS( md5_medium_ut.cpp -) - +) + REQUIREMENTS(ram:10) -END() +END() diff --git a/library/cpp/digest/md5/ut/ya.make b/library/cpp/digest/md5/ut/ya.make index d4595d71fd..ad1eddbff2 100644 --- a/library/cpp/digest/md5/ut/ya.make +++ b/library/cpp/digest/md5/ut/ya.make @@ -1,12 +1,12 @@ UNITTEST_FOR(library/cpp/digest/md5) - + OWNER( pg g:util ) - -SRCS( - md5_ut.cpp -) - -END() + +SRCS( + md5_ut.cpp +) + +END() diff --git a/library/cpp/digest/md5/ya.make b/library/cpp/digest/md5/ya.make index 501929c448..c09ec1c326 100644 --- a/library/cpp/digest/md5/ya.make +++ b/library/cpp/digest/md5/ya.make @@ -1,17 +1,17 @@ -LIBRARY() - +LIBRARY() + OWNER( pg g:util ) - -SRCS( - md5.cpp -) - + +SRCS( + md5.cpp +) + PEERDIR( - contrib/libs/nayuki_md5 + contrib/libs/nayuki_md5 library/cpp/string_utils/base64 ) -END() +END() |