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/md5.cpp | |
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/md5.cpp')
-rw-r--r-- | library/cpp/digest/md5/md5.cpp | 94 |
1 files changed, 47 insertions, 47 deletions
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)); } |