Joedb 10.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
get_hash.cpp
Go to the documentation of this file.
3
4#include <vector>
5
6namespace joedb
7{
8 ////////////////////////////////////////////////////////////////////////////
10 ////////////////////////////////////////////////////////////////////////////
11 (
12 const Abstract_File &file,
13 int64_t from,
14 int64_t until
15 )
16 {
17 SHA_256 sha_256;
18
19 constexpr uint32_t chunks = 2048;
20 std::vector<char> hashing_buffer(SHA_256::chunk_size * chunks);
21
22 int64_t current = from;
23
24 while (true)
25 {
26 size_t block_size = SHA_256::chunk_size * chunks;
27 if (current + int64_t(block_size) > until)
28 block_size = size_t(until - current);
29
30 file.full_pread(hashing_buffer.data(), block_size, current);
31 current += int64_t(block_size);
32 const uint32_t full_chunks = uint32_t(block_size / SHA_256::chunk_size);
33 for (uint32_t i = 0; i < full_chunks; i++)
34 sha_256.process_chunk(&hashing_buffer[i * SHA_256::chunk_size]);
35
36 const uint32_t remainder = uint32_t(block_size % SHA_256::chunk_size);
37 if (remainder || current >= until)
38 {
40 (
41 &hashing_buffer[full_chunks * SHA_256::chunk_size],
42 uint64_t(current - from)
43 );
44 break;
45 }
46 }
47
48 return sha_256.get_hash();
49 }
50
51 ////////////////////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////////////////////
54 {
55 return get_full_hash(file, 0, file.get_size());
56 }
57
58 ////////////////////////////////////////////////////////////////////////////
59 SHA_256::Hash get_full_hash(const std::string_view s)
60 ////////////////////////////////////////////////////////////////////////////
61 {
63 return get_full_hash(file);
64 }
65
66 ////////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////////
69 (
70 const Abstract_File &file,
71 int64_t from,
72 int64_t until
73 )
74 {
75 std::array<char, 1 << 12> buffer;
76
77 constexpr int buffer_count = 256;
78 const int64_t size = until - from;
79
80 if (size < 4 * int64_t(buffer.size()) * buffer_count)
81 return get_full_hash(file, from, until);
82
83 SHA_256 sha_256;
84
85 for (int i = 0; i < buffer_count; i++)
86 {
87 int64_t buffer_position;
88
89 if (i == 0)
90 buffer_position = from;
91 else if (i == buffer_count - 1)
92 buffer_position = until - int64_t(buffer.size());
93 else
94 {
95 buffer_position = int64_t(buffer.size()) *
96 (
97 (from + (i * size) / (buffer_count - 1)) / int64_t(buffer.size())
98 );
99 }
100
101 file.full_pread(buffer.data(), buffer.size(), buffer_position);
102
103 for (size_t j = 0; j < buffer.size(); j += SHA_256::chunk_size)
104 sha_256.process_chunk(buffer.data() + j);
105 }
106
107 return sha_256.get_hash();
108 }
109}
virtual int64_t get_size() const
Get the size of the file, or -1 if it is unknown.
Compute SHA 256 hash code: https://en.wikipedia.org/wiki/SHA-2.
Definition SHA_256.h:20
const Hash & get_hash() const
Definition SHA_256.h:66
void process_chunk(const char *data)
process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
Definition SHA_256.h:70
static constexpr size_t chunk_size
Definition SHA_256.h:67
std::array< uint32_t, 8 > Hash
Definition SHA_256.h:59
void process_final_chunk(const char *const data, const uint64_t total_length_in_bytes)
process last bytes of the sequence
Definition SHA_256.h:125
SHA_256::Hash get_full_hash(const Abstract_File &file, int64_t from, int64_t until)
Definition get_hash.cpp:11
SHA_256::Hash get_fast_hash(const Abstract_File &file, int64_t from, int64_t until)
Definition get_hash.cpp:69