aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/crypto/sha.h
blob: dbc2dfa526d1f01c1e61dcbd0f6bd596a18a3a8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <util/generic/ptr.h>
#include <util/generic/strbuf.h>
#include <util/system/types.h>

#include <array>

struct SHAstate_st;
struct SHA256state_st;

namespace NOpenSsl::NSha1 {
    constexpr size_t DIGEST_LENGTH = 20;
    using TDigest = std::array<ui8, DIGEST_LENGTH>;

    // not fragmented input
    TDigest Calc(const void* data, size_t dataSize);

    inline TDigest Calc(TStringBuf s) {
        return Calc(s.data(), s.length());
    }

    // fragmented input
    class TCalcer {
    public:
        TCalcer();
        ~TCalcer();
        void Update(const void* data, size_t dataSize);

        void Update(TStringBuf s) {
            Update(s.data(), s.length());
        }

        template <typename T>
        void UpdateWithPodValue(const T& value) {
            Update(&value, sizeof(value));
        }

        TDigest Final();

    private:
        THolder<SHAstate_st> Context;
    };
}

namespace NOpenSsl::NSha256 {
    constexpr size_t DIGEST_LENGTH = 32;
    using TDigest = std::array<ui8, DIGEST_LENGTH>;

    // not fragmented input
    TDigest Calc(const void* data, size_t dataSize);

    inline TDigest Calc(TStringBuf s) {
        return Calc(s.data(), s.length());
    }

    // fragmented input
    class TCalcer {
    public:
        TCalcer();
        ~TCalcer();
        void Update(const void* data, size_t dataSize);

        void Update(TStringBuf s) {
            Update(s.data(), s.length());
        }

        template <typename T>
        void UpdateWithPodValue(const T& value) {
            Update(&value, sizeof(value));
        }

        TDigest Final();

    private:
        THolder<SHA256state_st> Context;
    };
}