Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
sha1.h
Go to the documentation of this file.
1/* sha1.h
2
3Copyright (c) 2005 Michael D. Leonhard
4
5http://tamale.net/
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11of the Software, and to permit persons to whom the Software is furnished to do
12so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <cstdint>
30#include <string>
31#include <string_view>
32
33typedef uint32_t Uint32;
34
35class SHA1
36{
37private:
38 // fields
39 Uint32 H0 = 0x67452301;
40 Uint32 H1 = 0xefcdab89;
41 Uint32 H2 = 0x98badcfe;
42 Uint32 H3 = 0x10325476;
43 Uint32 H4 = 0xc3d2e1f0;
44 unsigned char bytes[64];
47 void process();
48
49public:
50 SHA1();
51 ~SHA1();
52 void addBytes(const char *data, Uint32 num);
53 inline void addBytes(std::string_view data) {
54 addBytes(data.data(), data.size());
55 }
56 void getDigest(unsigned char *to);
57 inline std::string getDigest() {
58 std::string ret(20, '\000');
59 getDigest(reinterpret_cast<unsigned char*>(ret.data()));
60 return ret;
61 }
62
63 // utility methods
64 static Uint32 lrot(Uint32 x, int bits);
65 static void storeBigEndianUint32(unsigned char *byte, Uint32 num);
66 static void hexPrinter(unsigned char *c, int l);
67};
Definition sha1.h:36
static Uint32 lrot(Uint32 x, int bits)
Definition sha1.cpp:48
void addBytes(const char *data, Uint32 num)
Definition sha1.cpp:137
Uint32 H2
Definition sha1.h:41
Uint32 H0
Definition sha1.h:39
Uint32 H4
Definition sha1.h:43
static void hexPrinter(unsigned char *c, int l)
Definition sha1.cpp:35
Uint32 unprocessedBytes
Definition sha1.h:45
~SHA1()
Definition sha1.cpp:72
static void storeBigEndianUint32(unsigned char *byte, Uint32 num)
Definition sha1.cpp:54
std::string getDigest()
Definition sha1.h:57
void addBytes(std::string_view data)
Definition sha1.h:53
unsigned char bytes[64]
Definition sha1.h:44
void process()
Definition sha1.cpp:81
Uint32 H1
Definition sha1.h:40
SHA1()
Definition sha1.cpp:65
Uint32 size
Definition sha1.h:46
Uint32 H3
Definition sha1.h:42
uint32_t Uint32
Definition sha1.h:33