├── .vscode
├── launch.json
├── settings.json
└── tasks.json
├── Blockchain.txt
├── LICENSE
├── README.md
├── lib
├── blockchain.hpp
└── sha256.h
├── main.cpp
├── main.exe
└── src
├── blockchain.cpp
├── img
└── chain.png
└── sha256.cpp
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": []
7 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "files.associations": {
3 | "atomic": "cpp",
4 | "bit": "cpp",
5 | "cctype": "cpp",
6 | "clocale": "cpp",
7 | "cmath": "cpp",
8 | "compare": "cpp",
9 | "concepts": "cpp",
10 | "cstddef": "cpp",
11 | "cstdint": "cpp",
12 | "cstdio": "cpp",
13 | "cstdlib": "cpp",
14 | "cstring": "cpp",
15 | "ctime": "cpp",
16 | "cwchar": "cpp",
17 | "exception": "cpp",
18 | "initializer_list": "cpp",
19 | "ios": "cpp",
20 | "iosfwd": "cpp",
21 | "iostream": "cpp",
22 | "istream": "cpp",
23 | "iterator": "cpp",
24 | "limits": "cpp",
25 | "memory": "cpp",
26 | "new": "cpp",
27 | "ostream": "cpp",
28 | "stdexcept": "cpp",
29 | "streambuf": "cpp",
30 | "string": "cpp",
31 | "system_error": "cpp",
32 | "tuple": "cpp",
33 | "type_traits": "cpp",
34 | "typeinfo": "cpp",
35 | "utility": "cpp",
36 | "xfacet": "cpp",
37 | "xiosbase": "cpp",
38 | "xlocale": "cpp",
39 | "xlocinfo": "cpp",
40 | "xlocnum": "cpp",
41 | "xmemory": "cpp",
42 | "xstddef": "cpp",
43 | "xstring": "cpp",
44 | "xtr1common": "cpp",
45 | "xutility": "cpp",
46 | "vector": "cpp",
47 | "chrono": "cpp",
48 | "sstream": "cpp"
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "tasks": [
3 | {
4 | "type": "cppbuild",
5 | "label": "C/C++: g++.exe build active file",
6 | "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
7 | "args": [
8 | "-fdiagnostics-color=always",
9 | "-g",
10 | "${file}",
11 | "-o",
12 | "${fileDirname}\\${fileBasenameNoExtension}.exe",
13 | "${fileDirname}\\src\\blockchain.cpp",
14 | "${fileDirname}\\src\\sha256.cpp"
15 | ],
16 | "options": {
17 | "cwd": "${fileDirname}"
18 | },
19 | "problemMatcher": ["$gcc"],
20 | "group": {
21 | "kind": "build",
22 | "isDefault": true
23 | },
24 | "detail": "Task generated by Debugger."
25 | }
26 | ],
27 | "version": "2.0.0"
28 | }
29 |
--------------------------------------------------------------------------------
/Blockchain.txt:
--------------------------------------------------------------------------------
1 |
2 | -------------------------------------
3 |
4 | Index: 1
5 | Time-Stamp: 1702977589
6 | Nonce: 108688
7 | Data: This is Genesis Block
8 | Prev. Hash: 0000000000000000000000000000000000000000000000000000000000000000
9 | Current Hash: 0000cdb392b9272e4ce544c6edf4e6c8b1fb29bc3a080775215bc54c36c5975c
10 |
11 | -------------------------------------
12 |
13 | -------------------------------------
14 |
15 | Index: 2
16 | Time-Stamp: 1702977590
17 | Nonce: 227579
18 | Data: This is 2th block and some data ...
19 | Prev. Hash: 0000cdb392b9272e4ce544c6edf4e6c8b1fb29bc3a080775215bc54c36c5975c
20 | Current Hash: 000056111ed6a15878ecacee1624f50588d88eabfa76eed2488b6e4d24ca9623
21 |
22 | -------------------------------------
23 |
24 | -------------------------------------
25 |
26 | Index: 3
27 | Time-Stamp: 1702977591
28 | Nonce: 17595
29 | Data: This is 3th block and some data ...
30 | Prev. Hash: 000056111ed6a15878ecacee1624f50588d88eabfa76eed2488b6e4d24ca9623
31 | Current Hash: 0000882e5bc2e19596c224777b24edc427098abb0df9c4f70dbf69121f145cb7
32 |
33 | -------------------------------------
34 |
35 | -------------------------------------
36 |
37 | Index: 4
38 | Time-Stamp: 1702977591
39 | Nonce: 376
40 | Data: This is 4th block and some data ...
41 | Prev. Hash: 0000882e5bc2e19596c224777b24edc427098abb0df9c4f70dbf69121f145cb7
42 | Current Hash: 000008dea4578ecd68698c5d9369fdc9423b8cd5325ad10979cdd427b75625ef
43 |
44 | -------------------------------------
45 |
46 | -------------------------------------
47 |
48 | Index: 5
49 | Time-Stamp: 1702977591
50 | Nonce: 227630
51 | Data: This is 5th block and some data ...
52 | Prev. Hash: 000008dea4578ecd68698c5d9369fdc9423b8cd5325ad10979cdd427b75625ef
53 | Current Hash: 000000468f7159fe2047daf30aa16141519ebb62fe849d26365dc188589db55f
54 |
55 | -------------------------------------
56 |
57 | -------------------------------------
58 |
59 | Index: 6
60 | Time-Stamp: 1702977592
61 | Nonce: 72974
62 | Data: This is 6th block and some data ...
63 | Prev. Hash: 000000468f7159fe2047daf30aa16141519ebb62fe849d26365dc188589db55f
64 | Current Hash: 00007c136a89c52a3097c71bee7bd69999c804f960fb26a37db06489774a2c26
65 |
66 | -------------------------------------
67 |
68 | -------------------------------------
69 |
70 | Index: 7
71 | Time-Stamp: 1702977593
72 | Nonce: 157503
73 | Data: This is 7th block and some data ...
74 | Prev. Hash: 00007c136a89c52a3097c71bee7bd69999c804f960fb26a37db06489774a2c26
75 | Current Hash: 00007bd1773c60e91dd667403173ff80b65edeb8f3090909394fba7e10016e01
76 |
77 | -------------------------------------
78 |
79 | -------------------------------------
80 |
81 | Index: 8
82 | Time-Stamp: 1702977594
83 | Nonce: 59182
84 | Data: This is 8th block and some data ...
85 | Prev. Hash: 00007bd1773c60e91dd667403173ff80b65edeb8f3090909394fba7e10016e01
86 | Current Hash: 0000fcd78fb2cfe94a9dfa5b038472d0ff975944f82f60c1ed1a8be81e02a836
87 |
88 | -------------------------------------
89 |
90 | -------------------------------------
91 |
92 | Index: 9
93 | Time-Stamp: 1702977594
94 | Nonce: 22178
95 | Data: This is 9th block and some data ...
96 | Prev. Hash: 0000fcd78fb2cfe94a9dfa5b038472d0ff975944f82f60c1ed1a8be81e02a836
97 | Current Hash: 0000fd51382f7667f7f801ce4080f46b1353fc25e8b1fc31cf1e48037730b1c1
98 |
99 | -------------------------------------
100 |
101 | -------------------------------------
102 |
103 | Index: 10
104 | Time-Stamp: 1702977594
105 | Nonce: 26944
106 | Data: This is 10th block and some data ...
107 | Prev. Hash: 0000fd51382f7667f7f801ce4080f46b1353fc25e8b1fc31cf1e48037730b1c1
108 | Current Hash: 00008c2da975a5a362cd7fa7a7f4dfcd4b23efe3b56b94972f28cf6bd487e8a9
109 |
110 | -------------------------------------
111 |
112 | -------------------------------------
113 |
114 | Index: 11
115 | Time-Stamp: 1702977595
116 | Nonce: 13455
117 | Data: This is 11th block and some data ...
118 | Prev. Hash: 00008c2da975a5a362cd7fa7a7f4dfcd4b23efe3b56b94972f28cf6bd487e8a9
119 | Current Hash: 000068a2b0adb96a0bdc95a5e71596fc9b88c0bf5fcd4dbcbf088a7e9dd58c36
120 |
121 | -------------------------------------
122 |
123 | -------------------------------------
124 |
125 | Index: 12
126 | Time-Stamp: 1702977595
127 | Nonce: 47350
128 | Data: This is 12th block and some data ...
129 | Prev. Hash: 000068a2b0adb96a0bdc95a5e71596fc9b88c0bf5fcd4dbcbf088a7e9dd58c36
130 | Current Hash: 000024d038fe52c4fddcf75f5d559cd2f45981dd62949dcbb173553e420ba7eb
131 |
132 | -------------------------------------
133 |
134 | -------------------------------------
135 |
136 | Index: 13
137 | Time-Stamp: 1702977595
138 | Nonce: 155236
139 | Data: This is 13th block and some data ...
140 | Prev. Hash: 000024d038fe52c4fddcf75f5d559cd2f45981dd62949dcbb173553e420ba7eb
141 | Current Hash: 00009336211a96c05fe5e417124dd5193f2b7dfe49c502005b42ec1be504ad1c
142 |
143 | -------------------------------------
144 |
145 | -------------------------------------
146 |
147 | Index: 14
148 | Time-Stamp: 1702977596
149 | Nonce: 23792
150 | Data: This is 14th block and some data ...
151 | Prev. Hash: 00009336211a96c05fe5e417124dd5193f2b7dfe49c502005b42ec1be504ad1c
152 | Current Hash: 000002154354d74853b70f14a440a0d2254cf0500ac5a5bb1845cbf66b0e517b
153 |
154 | -------------------------------------
155 |
156 | -------------------------------------
157 |
158 | Index: 15
159 | Time-Stamp: 1702977596
160 | Nonce: 158945
161 | Data: This is 15th block and some data ...
162 | Prev. Hash: 000002154354d74853b70f14a440a0d2254cf0500ac5a5bb1845cbf66b0e517b
163 | Current Hash: 00003bbbed25c050b9c4c64a8998f727b9a65fad154dc26351324fbef575ba91
164 |
165 | -------------------------------------
166 |
167 | -------------------------------------
168 |
169 | Index: 16
170 | Time-Stamp: 1702977597
171 | Nonce: 271425
172 | Data: This is 16th block and some data ...
173 | Prev. Hash: 00003bbbed25c050b9c4c64a8998f727b9a65fad154dc26351324fbef575ba91
174 | Current Hash: 0000af2637e4a2ce8faf5ab1a3ed95685f6ea316733c1f5e87fee26fb765069c
175 |
176 | -------------------------------------
177 |
178 | -------------------------------------
179 |
180 | Index: 17
181 | Time-Stamp: 1702977598
182 | Nonce: 181711
183 | Data: This is 17th block and some data ...
184 | Prev. Hash: 0000af2637e4a2ce8faf5ab1a3ed95685f6ea316733c1f5e87fee26fb765069c
185 | Current Hash: 00000ea8a482010340c9cec712bf633bdd85f18ed89d5c537574269fb1eb6dd5
186 |
187 | -------------------------------------
188 |
189 | -------------------------------------
190 |
191 | Index: 18
192 | Time-Stamp: 1702977599
193 | Nonce: 51016
194 | Data: This is 18th block and some data ...
195 | Prev. Hash: 00000ea8a482010340c9cec712bf633bdd85f18ed89d5c537574269fb1eb6dd5
196 | Current Hash: 0000a1334d088bc804b2c4c24e488a9e669df223e6afd9761d7a5133f3813fe4
197 |
198 | -------------------------------------
199 |
200 | -------------------------------------
201 |
202 | Index: 19
203 | Time-Stamp: 1702977599
204 | Nonce: 233904
205 | Data: This is 19th block and some data ...
206 | Prev. Hash: 0000a1334d088bc804b2c4c24e488a9e669df223e6afd9761d7a5133f3813fe4
207 | Current Hash: 000029de6bc8a7def41d63ccbcfb8803730528c0a622d28f7665e58d01551ae6
208 |
209 | -------------------------------------
210 |
211 | -------------------------------------
212 |
213 | Index: 20
214 | Time-Stamp: 1702977601
215 | Nonce: 141454
216 | Data: This is 20th block and some data ...
217 | Prev. Hash: 000029de6bc8a7def41d63ccbcfb8803730528c0a622d28f7665e58d01551ae6
218 | Current Hash: 00008b26fbb6c71d4597b2936fcf25a2992e5f6c6651d7aae742a2048aa0da8f
219 |
220 | -------------------------------------
221 |
222 | -------------------------------------
223 |
224 | Index: 21
225 | Time-Stamp: 1702977601
226 | Nonce: 57727
227 | Data: This is 21th block and some data ...
228 | Prev. Hash: 00008b26fbb6c71d4597b2936fcf25a2992e5f6c6651d7aae742a2048aa0da8f
229 | Current Hash: 00006f03933d6386b7ef25ec7ac62c96ea892c7b9f4c0078d58055e315d0292c
230 |
231 | -------------------------------------
232 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Ali İhsan Taş
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | **_
CPP | Blockcahin Demo _**
2 |
3 | **This app provides a simple explanation of blockchain.**
4 |
5 | This application essentially consists of;
6 |
7 | A blockchain network containing index, timestamps, data, prev.hash and hash values has been created. Users are able to mine new blocks containing the desired information. Lastly, a text file can be created from the blockchain.
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | The content of the Blockcahin Demo application;
18 |
19 | - Fundemantel C++
20 | - Class
21 | - Vector
22 | - File Operation
23 |
--------------------------------------------------------------------------------
/lib/blockchain.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __BLOCKCHAIN_HPP__
2 | #define __BLOCKCHAIN_HPP__
3 |
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include "sha256.h"
11 | #include
12 |
13 |
14 | using namespace std;
15 |
16 | class Blockchain{
17 |
18 | private:
19 |
20 | typedef struct Block{
21 |
22 | uint64_t index;
23 | uint64_t nonce;
24 |
25 | time_t time_stamp;
26 |
27 | string data;
28 | string prev_hash;
29 | string current_hash;
30 |
31 | }Block_st;
32 |
33 |
34 | Block_st block;
35 |
36 | void generate_genesis_block();
37 | string hash(string _data);
38 | void proof_of_work(Block_st *_block);
39 | string block_2_string(Block_st *_block);
40 | void clear_current_block();
41 | string uint64_to_string(uint64_t _number);
42 | bool check_hash_is_available(string _hash);
43 |
44 |
45 | public:
46 |
47 | Blockchain();
48 |
49 | void Mine(string _data);
50 |
51 | void Get_BlockChain();
52 |
53 | bool Check_Chain_Is_Valid();
54 |
55 | void Sleep();
56 |
57 | vector Chain;
58 |
59 |
60 | };
61 |
62 |
63 | #endif
--------------------------------------------------------------------------------
/lib/sha256.h:
--------------------------------------------------------------------------------
1 | #ifndef SHA256_H
2 | #define SHA256_H
3 | #include
4 |
5 | // Refence : http://www.zedwood.com/article/cpp-sha256-function
6 |
7 | class SHA256
8 | {
9 | protected:
10 | typedef unsigned char uint8;
11 | typedef unsigned int uint32;
12 | typedef unsigned long long uint64;
13 |
14 | const static uint32 sha256_k[];
15 | static const unsigned int SHA224_256_BLOCK_SIZE = (512/8);
16 | public:
17 | void init();
18 | void update(const unsigned char *message, unsigned int len);
19 | void final(unsigned char *digest);
20 | static const unsigned int DIGEST_SIZE = ( 256 / 8);
21 |
22 | protected:
23 | void transform(const unsigned char *message, unsigned int block_nb);
24 | unsigned int m_tot_len;
25 | unsigned int m_len;
26 | unsigned char m_block[2*SHA224_256_BLOCK_SIZE];
27 | uint32 m_h[8];
28 | };
29 |
30 | std::string sha256(std::string input);
31 |
32 | #define SHA2_SHFR(x, n) (x >> n)
33 | #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
34 | #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
35 | #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
36 | #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
37 | #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
38 | #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
39 | #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
40 | #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
41 | #define SHA2_UNPACK32(x, str) \
42 | { \
43 | *((str) + 3) = (uint8) ((x) ); \
44 | *((str) + 2) = (uint8) ((x) >> 8); \
45 | *((str) + 1) = (uint8) ((x) >> 16); \
46 | *((str) + 0) = (uint8) ((x) >> 24); \
47 | }
48 | #define SHA2_PACK32(str, x) \
49 | { \
50 | *(x) = ((uint32) *((str) + 3) ) \
51 | | ((uint32) *((str) + 2) << 8) \
52 | | ((uint32) *((str) + 1) << 16) \
53 | | ((uint32) *((str) + 0) << 24); \
54 | }
55 | #endif
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include "lib/blockchain.hpp"
2 |
3 | using namespace std;
4 |
5 | int main(){
6 |
7 | Blockchain blockchain;
8 |
9 | // Blockchain Mine
10 | for(int i = 0; i<20; i++){
11 | blockchain.Mine("This is " + to_string(i+2) + "th block and some data ...");
12 | blockchain.Sleep();
13 | }
14 |
15 | // Get Blockchain (to txt file)
16 | blockchain.Get_BlockChain();
17 |
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/main.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/universe1216/blockchain-cpp/4ce56eb527df2b058c4041e48704c5d80845e9af/main.exe
--------------------------------------------------------------------------------
/src/blockchain.cpp:
--------------------------------------------------------------------------------
1 | #include "../lib/blockchain.hpp"
2 | #include
3 |
4 | /////////////////////////////
5 | // Public Functions
6 |
7 | // Constructure
8 | Blockchain::Blockchain(){
9 | this->generate_genesis_block();
10 | }
11 |
12 |
13 | // Mine Block
14 | void Blockchain::Mine(string _data){
15 |
16 | this->block.prev_hash = this->Chain.at(this->block.index-1).current_hash;
17 | this->block.index++;
18 | this->block.time_stamp = time(0);
19 | this->block.data = _data;
20 | this->proof_of_work(&this->block);
21 | this->Chain.push_back(this->block);
22 |
23 | }
24 |
25 | // Check Chain is valid
26 | bool Blockchain::Check_Chain_Is_Valid(){
27 |
28 | int _length = this->Chain.size();
29 | for(int i = 0; i<_length; i++){
30 |
31 | string _data = this->block_2_string(&this->Chain.at(i));
32 | uint64_t _nonce = this->Chain.at(i).nonce;
33 | string _hash = this->hash(_data + uint64_to_string(_nonce));
34 |
35 | if(i >= 1){
36 | if(!(this->Chain.at(i).prev_hash == this->Chain.at(i-1).current_hash)){
37 | return false;
38 | }
39 | }
40 |
41 | if(!this->check_hash_is_available(_hash)){
42 | return false;
43 | }
44 |
45 | }
46 |
47 |
48 | return true;
49 | }
50 |
51 | // Get Chain (to txt file)
52 |
53 | void Blockchain::Get_BlockChain(){
54 |
55 |
56 | // Create and open
57 | ofstream ChainFile("Blockchain.txt");
58 |
59 | string _str = "";
60 |
61 | if(this->Check_Chain_Is_Valid()){
62 |
63 | _str += "Chain is Valid";
64 |
65 | for(int i = 0; iChain.size(); i++){
66 |
67 | _str = "\n-------------------------------------\n";
68 | _str += "\nIndex: " + uint64_to_string(this->Chain.at(i).index);
69 | _str += "\nTime-Stamp: ";
70 | _str += uint64_to_string(this->Chain.at(i).time_stamp);
71 | _str += "\n";
72 | _str += "Nonce: " + uint64_to_string(this->Chain.at(i).nonce);
73 | _str += "\nData: " + this->Chain.at(i).data;
74 | _str += "\nPrev. Hash: " + this->Chain.at(i).prev_hash;
75 | _str += "\nCurrent Hash: " + this->Chain.at(i).current_hash + "\n";
76 | _str += "\n-------------------------------------\n";
77 |
78 | // Write to the file
79 | ChainFile << _str;
80 |
81 | }
82 | }
83 |
84 | else{
85 | _str += "Chain is not Valid";
86 | ChainFile << _str;
87 | }
88 |
89 |
90 |
91 | ChainFile.close();
92 | }
93 |
94 |
95 | // sleep
96 | void Blockchain::Sleep(){
97 | for(int i = 0; i<100000; i++){
98 |
99 | }
100 |
101 | }
102 | // Public Functions
103 | /////////////////////////////
104 |
105 |
106 | /////////////////////////////
107 | // Private Functions
108 |
109 | // Genesis Block
110 | void Blockchain::generate_genesis_block(){
111 |
112 | this->block.index = 1;
113 | this->block.time_stamp = time(0);
114 | this->block.data = "This is Genesis Block";
115 | this->block.prev_hash = "0000000000000000000000000000000000000000000000000000000000000000";
116 | this->proof_of_work(&this->block);
117 | this->Chain.push_back(this->block);
118 | }
119 |
120 | // Calculate Nonce and Hash
121 | void Blockchain::proof_of_work(Block_st *_block){
122 |
123 | // Get block full data for calculate nonce and hash
124 | string _data = this->block_2_string(_block);
125 | uint64_t _nonce = 0;
126 |
127 | string _hash = "FFFF";
128 | // Find the nonce value for available hash
129 | while (!this->check_hash_is_available(_hash))
130 | {
131 | _nonce++;
132 | _hash = this->hash(_data + uint64_to_string(_nonce));
133 | }
134 |
135 | // Assign the correct Value
136 | _block->nonce = _nonce;
137 | _block->current_hash = _hash;
138 |
139 | }
140 |
141 | // Convert block to string
142 | string Blockchain::block_2_string(Block_st *_block){
143 |
144 | // Full block data
145 | string _data;
146 |
147 | // Add index to data
148 | _data += uint64_to_string(_block->index);
149 |
150 | // Add timestamp to data
151 | _data += ctime(&_block->time_stamp);
152 |
153 | // Add block data
154 | _data += _block->data;
155 |
156 | // Add prev. Has
157 | _data += _block->prev_hash;
158 |
159 | return _data;
160 | }
161 |
162 | // Clear Current Block
163 | void Blockchain::clear_current_block(){
164 |
165 | this->block.index = -1;
166 | this->block.time_stamp = -1;
167 | this->block.data = "-1";
168 | this->block.prev_hash = "-1";
169 | this->block.current_hash = "-1";
170 | this->block.nonce = -1;
171 |
172 | }
173 |
174 | // uint64_t to string converter
175 | string Blockchain::uint64_to_string(uint64_t _number){
176 |
177 | ostringstream _str;
178 | _str << _number;
179 | return (_str.str());
180 |
181 | }
182 |
183 | // Check Hash
184 | bool Blockchain::check_hash_is_available(string _hash){
185 |
186 | for(uint8_t i = 0; i<4; i++ ){
187 | if(_hash[i] != '0')
188 | return false;
189 | }
190 |
191 | return true;
192 | }
193 |
194 | // SHA 256
195 | string Blockchain::hash(string _data){
196 | return sha256(_data);
197 | }
198 |
199 |
200 |
201 |
202 | // Private Functions
203 | /////////////////////////////
204 |
--------------------------------------------------------------------------------
/src/img/chain.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/universe1216/blockchain-cpp/4ce56eb527df2b058c4041e48704c5d80845e9af/src/img/chain.png
--------------------------------------------------------------------------------
/src/sha256.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "../lib/sha256.h"
4 |
5 | const unsigned int SHA256::sha256_k[64] = //UL = uint32
6 | {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
7 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
8 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
9 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
10 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
11 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
12 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
13 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
14 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
15 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
16 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
17 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
18 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
19 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
20 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
21 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
22 |
23 | void SHA256::transform(const unsigned char *message, unsigned int block_nb)
24 | {
25 | uint32 w[64];
26 | uint32 wv[8];
27 | uint32 t1, t2;
28 | const unsigned char *sub_block;
29 | int i;
30 | int j;
31 | for (i = 0; i < (int) block_nb; i++) {
32 | sub_block = message + (i << 6);
33 | for (j = 0; j < 16; j++) {
34 | SHA2_PACK32(&sub_block[j << 2], &w[j]);
35 | }
36 | for (j = 16; j < 64; j++) {
37 | w[j] = SHA256_F4(w[j - 2]) + w[j - 7] + SHA256_F3(w[j - 15]) + w[j - 16];
38 | }
39 | for (j = 0; j < 8; j++) {
40 | wv[j] = m_h[j];
41 | }
42 | for (j = 0; j < 64; j++) {
43 | t1 = wv[7] + SHA256_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6])
44 | + sha256_k[j] + w[j];
45 | t2 = SHA256_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]);
46 | wv[7] = wv[6];
47 | wv[6] = wv[5];
48 | wv[5] = wv[4];
49 | wv[4] = wv[3] + t1;
50 | wv[3] = wv[2];
51 | wv[2] = wv[1];
52 | wv[1] = wv[0];
53 | wv[0] = t1 + t2;
54 | }
55 | for (j = 0; j < 8; j++) {
56 | m_h[j] += wv[j];
57 | }
58 | }
59 | }
60 |
61 | void SHA256::init()
62 | {
63 | m_h[0] = 0x6a09e667;
64 | m_h[1] = 0xbb67ae85;
65 | m_h[2] = 0x3c6ef372;
66 | m_h[3] = 0xa54ff53a;
67 | m_h[4] = 0x510e527f;
68 | m_h[5] = 0x9b05688c;
69 | m_h[6] = 0x1f83d9ab;
70 | m_h[7] = 0x5be0cd19;
71 | m_len = 0;
72 | m_tot_len = 0;
73 | }
74 |
75 | void SHA256::update(const unsigned char *message, unsigned int len)
76 | {
77 | unsigned int block_nb;
78 | unsigned int new_len, rem_len, tmp_len;
79 | const unsigned char *shifted_message;
80 | tmp_len = SHA224_256_BLOCK_SIZE - m_len;
81 | rem_len = len < tmp_len ? len : tmp_len;
82 | memcpy(&m_block[m_len], message, rem_len);
83 | if (m_len + len < SHA224_256_BLOCK_SIZE) {
84 | m_len += len;
85 | return;
86 | }
87 | new_len = len - rem_len;
88 | block_nb = new_len / SHA224_256_BLOCK_SIZE;
89 | shifted_message = message + rem_len;
90 | transform(m_block, 1);
91 | transform(shifted_message, block_nb);
92 | rem_len = new_len % SHA224_256_BLOCK_SIZE;
93 | memcpy(m_block, &shifted_message[block_nb << 6], rem_len);
94 | m_len = rem_len;
95 | m_tot_len += (block_nb + 1) << 6;
96 | }
97 |
98 | void SHA256::final(unsigned char *digest)
99 | {
100 | unsigned int block_nb;
101 | unsigned int pm_len;
102 | unsigned int len_b;
103 | int i;
104 | block_nb = (1 + ((SHA224_256_BLOCK_SIZE - 9)
105 | < (m_len % SHA224_256_BLOCK_SIZE)));
106 | len_b = (m_tot_len + m_len) << 3;
107 | pm_len = block_nb << 6;
108 | memset(m_block + m_len, 0, pm_len - m_len);
109 | m_block[m_len] = 0x80;
110 | SHA2_UNPACK32(len_b, m_block + pm_len - 4);
111 | transform(m_block, block_nb);
112 | for (i = 0 ; i < 8; i++) {
113 | SHA2_UNPACK32(m_h[i], &digest[i << 2]);
114 | }
115 | }
116 |
117 | std::string sha256(std::string input)
118 | {
119 | unsigned char digest[SHA256::DIGEST_SIZE];
120 | memset(digest,0,SHA256::DIGEST_SIZE);
121 |
122 | SHA256 ctx = SHA256();
123 | ctx.init();
124 | ctx.update( (unsigned char*)input.c_str(), input.length());
125 | ctx.final(digest);
126 |
127 | char buf[2*SHA256::DIGEST_SIZE+1];
128 | buf[2*SHA256::DIGEST_SIZE] = 0;
129 | for (int i = 0; i < SHA256::DIGEST_SIZE; i++)
130 | sprintf(buf+i*2, "%02x", digest[i]);
131 | return std::string(buf);
132 | }
--------------------------------------------------------------------------------