44
55#include < util/string/hex.h>
66
7+ namespace {
8+
9+ template <typename T, size_t N>
10+ void FillArrayFromProto (T (&array)[N], const NProtoBuf::RepeatedField<T>& proto) {
11+ for (int i = 0 ; i < proto.size (); ++i) {
12+ if (static_cast <size_t >(i) < std::size (array)) {
13+ array[i] = proto.Get (i);
14+ }
15+ }
16+ }
17+
18+ } // anonymous
19+
720namespace NKikimr ::NBackup {
821
922class TSHA256 : public IChecksum {
@@ -16,20 +29,49 @@ class TSHA256 : public IChecksum {
1629 SHA256_Update (&Context, data.data (), data.size ());
1730 }
1831
19- TString Serialize () override {
32+ TString Finalize () override {
2033 unsigned char hash[SHA256_DIGEST_LENGTH];
2134 SHA256_Final (hash, &Context);
2235 return to_lower (HexEncode (hash, SHA256_DIGEST_LENGTH));
2336 }
2437
38+ TChecksumState GetState () const override {
39+ TChecksumState state;
40+ auto & sha256State = *state.MutableSha256State ();
41+
42+ for (ui32 h : Context.h ) {
43+ sha256State.AddH (h);
44+ }
45+ sha256State.SetNh (Context.Nh );
46+ sha256State.SetNl (Context.Nl );
47+ for (ui32 data : Context.data ) {
48+ sha256State.AddData (data);
49+ }
50+ sha256State.SetNum (Context.num );
51+ sha256State.SetMdLen (Context.md_len );
52+
53+ return state;
54+ }
55+
56+ void Continue (const TChecksumState& state) override {
57+ const auto & sha256State = state.GetSha256State ();
58+ SHA256_Init (&Context);
59+ FillArrayFromProto (Context.h , sha256State.GetH ());
60+ Context.Nh = sha256State.GetNh ();
61+ Context.Nl = sha256State.GetNl ();
62+ FillArrayFromProto (Context.data , sha256State.GetData ());
63+ Context.num = sha256State.GetNum ();
64+ Context.md_len = sha256State.GetMdLen ();
65+ }
66+
2567private:
2668 SHA256_CTX Context;
2769};
2870
2971TString ComputeChecksum (TStringBuf data) {
3072 IChecksum::TPtr checksum (CreateChecksum ());
3173 checksum->AddData (data);
32- return checksum->Serialize ();
74+ return checksum->Finalize ();
3375}
3476
3577IChecksum* CreateChecksum () {
@@ -40,4 +82,4 @@ TString ChecksumKey(const TString& objKey) {
4082 return objKey + " .sha256" ;
4183}
4284
43- } // NKikimr::NDataShard
85+ } // NKikimr::NBackup
0 commit comments