aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/digest/md5/md5.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/digest/md5/md5.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/digest/md5/md5.h')
-rw-r--r--library/cpp/digest/md5/md5.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/digest/md5/md5.h b/library/cpp/digest/md5/md5.h
new file mode 100644
index 0000000000..1568567e3c
--- /dev/null
+++ b/library/cpp/digest/md5/md5.h
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <util/system/defaults.h>
+#include <util/generic/string.h>
+#include <util/generic/array_ref.h>
+#include <util/generic/strbuf.h>
+
+class IInputStream;
+
+class MD5 {
+public:
+ MD5() {
+ Init();
+ }
+
+ void Init();
+
+ inline MD5& Update(const void* data, size_t len) {
+ const char* buf = (const char*)data;
+
+ while (len) {
+ // NOTE: we don't want buffSz to be near Max<unsigned int>()
+ // because otherwise integer overflow might happen in UpdatePart
+ const unsigned int buffSz = Min(size_t(Max<unsigned int>() / 2), len);
+
+ UpdatePart(buf, buffSz);
+ buf += buffSz;
+ len -= buffSz;
+ }
+ return *this;
+ }
+
+ inline MD5& Update(const TStringBuf& data) {
+ return Update(data.data(), data.size());
+ }
+
+ void Pad();
+ unsigned char* Final(unsigned char[16]);
+
+ // buf must be char[33];
+ char* End(char* buf);
+
+ // buf must be char[25];
+ char* End_b64(char* buf);
+
+ // 8-byte xor-based mix
+ ui64 EndHalfMix();
+
+ MD5& Update(IInputStream* in);
+
+ /*
+ * Return hex-encoded md5 checksum for given file.
+ *
+ * Return nullptr / empty string if the file does not exist.
+ */
+ static char* File(const char* filename, char* buf);
+ static TString File(const TString& filename);
+
+ static char* Data(const void* data, size_t len, char* buf);
+ static TString Data(TArrayRef<ui8> data);
+ static TString Data(TStringBuf data);
+ static char* Stream(IInputStream* in, char* buf);
+
+ static TString Calc(const TStringBuf& data); // 32-byte hex-encoded
+ static TString CalcRaw(const TStringBuf& data); // 16-byte raw
+
+ static ui64 CalcHalfMix(const TStringBuf& data);
+ static ui64 CalcHalfMix(const char* data, size_t len);
+
+ static bool IsMD5(const TStringBuf& data);
+
+private:
+ void UpdatePart(const void* data, unsigned int len);
+
+private:
+ ui32 State[4]; /* state (ABCD) */
+ ui32 Count[2]; /* number of bits, modulo 2^64 (lsb first) */
+ unsigned char Buffer[64]; /* input buffer */
+};