stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
hash.cpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#include "pch.hpp"
7
8using namespace std;
9#ifdef _WIN32
10using namespace Microsoft::VisualStudio::CppUnitTestFramework;
11
12namespace Microsoft {
13 namespace VisualStudio {
14 namespace CppUnitTestFramework {
15 static std::wstring ToString(const stdex::md5_t& q)
16 {
18 wstring str;
19 enc.encode(str, &q, sizeof(q));
20 return str;
21 }
22
23 static std::wstring ToString(const stdex::sha1_t& q)
24 {
26 wstring str;
27 enc.encode(str, &q, sizeof(q));
28 return str;
29 }
30 }
31 }
32}
33#endif
34
35namespace UnitTests
36{
37 TEST_CLASS(hash)
38 {
39 public:
40 TEST_METHOD(crc32)
41 {
43 static const char data[] = "This is a test.";
44 h.hash(data, sizeof(data) - sizeof(*data));
45 h.finalize();
46 Assert::AreEqual<stdex::crc32_t>(0xc6c3c95d, h);
47 }
48
49 TEST_METHOD(md5)
50 {
52 static const char data[] = "This is a test.";
53 h.hash(data, sizeof(data) - sizeof(*data));
54 h.finalize();
55 Assert::AreEqual<stdex::md5_t>({0x12,0x0e,0xa8,0xa2,0x5e,0x5d,0x48,0x7b,0xf6,0x8b,0x5f,0x70,0x96,0x44,0x00,0x19}, h);
56 }
57
58 TEST_METHOD(sha1)
59 {
61 static const char data[] = "This is a test.";
62 h.hash(data, sizeof(data) - sizeof(*data));
63 h.finalize();
64 Assert::AreEqual<stdex::sha1_t>({0xaf,0xa6,0xc8,0xb3,0xa2,0xfa,0xe9,0x57,0x85,0xdc,0x7d,0x96,0x85,0xa5,0x78,0x35,0xd7,0x03,0xac,0x88}, h);
65 }
66 };
67}
virtual void hash(_In_reads_bytes_opt_(length) const void *data, size_t length)
Hashes block of data.
Definition hash.hpp:76
Hashes as CRC32.
Definition hash.hpp:174
virtual void finalize()
Finalizes hash value.
Definition hash.hpp:248
virtual void hash(_In_reads_bytes_opt_(length) const void *data, size_t length)
Hashes block of data.
Definition hash.hpp:186
Hexadecimal encoding session.
Definition hex.hpp:19
void encode(std::basic_string< T, TR, AX > &out, const void *data, size_t size)
Encodes one block of information, and appends it to the output.
Definition hex.hpp:35
Hashes as MD5.
Definition hash.hpp:303
virtual void finalize()
Finalizes hash value.
Definition hash.hpp:319
Hashes as SHA1.
Definition hash.hpp:504
virtual void finalize()
Finalizes hash value.
Definition hash.hpp:523
MD2 hash value.
Definition hash.hpp:258
SHA hash value.
Definition hash.hpp:458