aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/crypto/sha.cpp
blob: ffd999a238a6df4cdba60431eae13778ec3a24c0 (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
#include "sha.h"

#include <util/generic/yexception.h>

#include <openssl/sha.h>

namespace NOpenSsl {
    namespace NSha1 {
        static_assert(DIGEST_LENGTH == SHA_DIGEST_LENGTH);

        TDigest Calc(const void* data, size_t dataSize) {
            TDigest digest;
            Y_ENSURE(SHA1(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
            return digest;
        }

        TCalcer::TCalcer()
            : Context{new SHAstate_st} {
            Y_ENSURE(SHA1_Init(Context.Get()) == 1);
        }

        TCalcer::~TCalcer() {
        }

        void TCalcer::Update(const void* data, size_t dataSize) {
            Y_ENSURE(SHA1_Update(Context.Get(), data, dataSize) == 1);
        }

        TDigest TCalcer::Final() {
            TDigest digest;
            Y_ENSURE(SHA1_Final(digest.data(), Context.Get()) == 1);
            return digest;
        }
    }
    namespace NSha256 {
        static_assert(DIGEST_LENGTH == SHA256_DIGEST_LENGTH);

        TDigest Calc(const void* data, size_t dataSize) {
            TDigest digest;
            Y_ENSURE(SHA256(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
            return digest;
        }

        TCalcer::TCalcer()
            : Context{new SHA256state_st} {
            Y_ENSURE(SHA256_Init(Context.Get()) == 1);
        }

        TCalcer::~TCalcer() {
        }

        void TCalcer::Update(const void* data, size_t dataSize) {
            Y_ENSURE(SHA256_Update(Context.Get(), data, dataSize) == 1);
        }

        TDigest TCalcer::Final() {
            TDigest digest;
            Y_ENSURE(SHA256_Final(digest.data(), Context.Get()) == 1);
            return digest;
        }
    }
    namespace NSha224 {
        static_assert(DIGEST_LENGTH == SHA224_DIGEST_LENGTH);

        TDigest Calc(const void* data, size_t dataSize) {
            TDigest digest;
            Y_ENSURE(SHA224(static_cast<const ui8*>(data), dataSize, digest.data()) != nullptr);
            return digest;
        }

        TCalcer::TCalcer()
            : Context{new SHA256state_st} {
            Y_ENSURE(SHA224_Init(Context.Get()) == 1);
        }

        TCalcer::~TCalcer() {
        }

        void TCalcer::Update(const void* data, size_t dataSize) {
            Y_ENSURE(SHA224_Update(Context.Get(), data, dataSize) == 1);
        }

        TDigest TCalcer::Final() {
            TDigest digest;
            Y_ENSURE(SHA224_Final(digest.data(), Context.Get()) == 1);
            return digest;
        }
    }
}