-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.hpp
73 lines (55 loc) · 2.7 KB
/
Hash.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (c) (2023) SPHINX_ORG
* Authors:
* - (C kusuma) <[email protected]>
* GitHub: (https://github.com/chykusuma)
* Contributors:
* - (Contributor 1) <[email protected]>
* Github: (https://github.com/yourgit)
* - (Contributor 2) <[email protected]>
* Github: (https://github.com/yourgit)
*/
#ifndef HASH_HPP
#define HASH_HPP
#pragma once
#include <string>
#include <vector>
#include "lib/Swifftx/SHA3.h"
namespace SPHINXHash {
std::string SPHINX_256(const std::string& message) {
hashState state;
BitSequence hashval[SWIFFTX_OUTPUT_BLOCK_SIZE];
// These lines declare variables state of type hashState and hashval as an array of BitSequence
// with a size of SWIFFTX_OUTPUT_BLOCK_SIZE. These variables will be used to store the hash state
// and the resulting hash value, respectively.
// Initialize the hash state with the intended hash length
Init(&state, 256); // Replace "256" with the desired hash length
// This line initializes the state variable by calling the Init function from the SWIFFTX library.
// The second argument 256 specifies the desired hash length in bits.
// IT can replace 256 with the desired hash length.
// Update the hash state with the message
Update(&state, reinterpret_cast<const BitSequence*>(message.c_str()), message.length() * 8);
// This line updates the hash state by calling the Update function from the SWIFFTX library.
// It takes the state variable, casts the message string to a const BitSequence*, and provides
// the length of the message in bits. This step processes the input message and updates the hash
// state accordingly.
// Finalize the hash computation and obtain the hash value
Final(&state, hashval);
// This line finalizes the hash computation by calling the Final function from the SWIFFTX library.
// It takes the state variable and the hashval array as arguments. This step computes the final hash
// value based on the processed input message and stores it in the hashval array.
// Convert the hash value to a hexadecimal string
std::string result;
for (int i = 0; i < SWIFFTX_OUTPUT_BLOCK_SIZE; ++i) {
char hex[3];
sprintf(hex, "%02x", hashval[i]);
result += hex;
}
// These lines convert the hashval array to a hexadecimal string representation. It iterates over
// each element of the hashval array, converts it to a two-digit hexadecimal representation using
// sprintf, and appends it to the result string.
return result;
// This line returns the resulting hash value as a hexadecimal string.
}
} // namespace SPHINXHash
#endif // HASH_HPP