Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Hashing.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Common/CompilerMacrosExport.h"
5#include "../Common/CompilerMacrosLifetimeBound.h"
6#ifndef SC_EXPORT_LIBRARY_HASHING
7#define SC_EXPORT_LIBRARY_HASHING 0
8#endif
9#define SC_HASHING_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_HASHING)
10
11#include "../Common/PlatformMacrosType.h"
12#include "../Common/Span.h"
13
14namespace SC
15{
18
21
32struct Hashing
33{
34 struct Result
35 {
36 static constexpr auto MD5_DIGEST_LENGTH = 16;
37 static constexpr auto SHA1_DIGEST_LENGTH = 20;
38 static constexpr auto SHA256_DIGEST_LENGTH = 32;
39
40 uint8_t hash[32] = {0};
41 size_t size = 0;
42
43 Span<const uint8_t> toBytesSpan() const SC_LANGUAGE_LIFETIME_BOUND { return {hash, size}; }
44 };
51
54
57
58 Hashing(const Hashing&) = delete;
59 Hashing(Hashing&&) = delete;
60 Hashing& operator=(const Hashing&) = delete;
61 Hashing& operator=(Hashing&&) = delete;
62
66 [[nodiscard]] bool add(Span<const uint8_t> data);
67
71 [[nodiscard]] bool getHash(Result& res);
72
76 [[nodiscard]] bool setType(Type newType);
77
78 private:
79#if SC_PLATFORM_APPLE
80 alignas(uint64_t) char buffer[104];
81#elif SC_PLATFORM_WINDOWS
82 alignas(uint64_t) char buffer[16];
83 struct CryptoPrivate;
84 bool destroyHash();
85#elif SC_PLATFORM_LINUX
86 int mainSocket = -1; // Not using FileDescriptor just to avoid dependency...
87 int hashSocket = -1;
88#endif
89 bool inited = false;
90 Type type = TypeMD5;
91};
93} // namespace SC
Definition Hashing.h:35
Compute MD5, SHA1 or SHA256 hash for stream of data.
Definition Hashing.h:33
Hashing()
Initializes an Hashing struct.
bool add(Span< const uint8_t > data)
Add data to be hashed.
~Hashing()
Destroys an Hashing struct.
bool setType(Type newType)
Set type of hash to compute.
Type
Definition Hashing.h:46
@ TypeMD5
Compute MD5 hash for the incoming stream of bytes.
Definition Hashing.h:47
@ TypeSHA1
Compute SHA1 hash for the incoming stream of bytes.
Definition Hashing.h:48
@ TypeSHA256
Compute SHA256 hash for the incoming stream of bytes.
Definition Hashing.h:49
bool getHash(Result &res)
Finalize hash computation that has been pushed through Hashing::add.