aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/crypto/sha.h
blob: 49ec8bd009e022c695fab11e48c954067379a101 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
    };
}

namespace NOpenSsl::NSha224 {
    constexpr size_t DIGEST_LENGTH = 28;
    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;
    };
}