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 "../Foundation/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_HASHING
6#define SC_EXPORT_LIBRARY_HASHING 0
7#endif
8#define SC_HASHING_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_HASHING)
9
10#include "../Foundation/Span.h"
11namespace SC
12{
15
18
29struct Hashing
30{
31 struct Result
32 {
33 static constexpr auto MD5_DIGEST_LENGTH = 16;
34 static constexpr auto SHA1_DIGEST_LENGTH = 20;
35 static constexpr auto SHA256_DIGEST_LENGTH = 32;
36
37 uint8_t hash[32] = {0};
38 size_t size = 0;
39
40 Span<const uint8_t> toBytesSpan() const SC_LANGUAGE_LIFETIME_BOUND { return {hash, size}; }
41 };
48
51
54
55 Hashing(const Hashing&) = delete;
56 Hashing(Hashing&&) = delete;
57 Hashing& operator=(const Hashing&) = delete;
58 Hashing& operator=(Hashing&&) = delete;
59
63 [[nodiscard]] bool add(Span<const uint8_t> data);
64
68 [[nodiscard]] bool getHash(Result& res);
69
73 [[nodiscard]] bool setType(Type newType);
74
75 private:
76#if SC_PLATFORM_APPLE
77 alignas(uint64_t) char buffer[104];
78#elif SC_PLATFORM_WINDOWS
79 alignas(uint64_t) char buffer[16];
80 struct CryptoPrivate;
81 bool destroyHash();
82#elif SC_PLATFORM_LINUX
83 int mainSocket = -1; // Not using FileDescriptor just to avoid dependency...
84 int hashSocket = -1;
85#endif
86 bool inited = false;
87 Type type = TypeMD5;
88};
90} // namespace SC
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:27
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:33
Definition Hashing.h:32
Compute MD5, SHA1 or SHA256 hash for stream of data.
Definition Hashing.h:30
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:43
@ TypeMD5
Compute MD5 hash for the incoming stream of bytes.
Definition Hashing.h:44
@ TypeSHA1
Compute SHA1 hash for the incoming stream of bytes.
Definition Hashing.h:45
@ TypeSHA256
Compute SHA256 hash for the incoming stream of bytes.
Definition Hashing.h:46
bool getHash(Result &res)
Finalize hash computation that has been pushed through Hashing::add.
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29