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 74 75 76 77 78 79 80 81 82 83 |
#include <iostream> #include <vector> #include <string> #include <sstream> #include <iomanip> #include <openssl/sha.h> // For SHA256 hashing using namespace std; // Function to calculate SHA256 hash of a string string sha256(const string& input) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, input.c_str(), input.size()); SHA256_Final(hash, &sha256); stringstream ss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { ss << hex << setw(2) << setfill('0') << (int)hash[i]; } return ss.str(); } // Block class class Block { public: string previousHash; string hash; string data; long timestamp; Block(string data, string previousHash = "") : data(data), previousHash(previousHash), timestamp(time(nullptr)) { hash = calculateHash(); } string calculateHash() const { stringstream ss; ss << timestamp << previousHash << data; return sha256(ss.str()); } }; // Blockchain class class Blockchain { private: vector<Block> chain; public: Blockchain() { // Create the genesis block chain.push_back(Block("Genesis Block")); } void addBlock(const Block& newBlock) { chain.push_back(newBlock); } void printChain() const { for (const auto& block : chain) { cout << "Block Index: " << (&block - &chain[0]) << endl; cout << "Timestamp: " << block.timestamp << endl; cout << "Data: " << block.data << endl; cout << "Hash: " << block.hash << endl; cout << "Previous Hash: " << block.previousHash << endl; cout << "------------------------" << endl; } } }; int main() { Blockchain blockchain; blockchain.addBlock(Block("Block 1 Data", blockchain.chain.back().hash)); blockchain.addBlock(Block("Block 2 Data", blockchain.chain.back().hash)); blockchain.addBlock(Block("Block 3 Data", blockchain.chain.back().hash)); cout << "Blockchain contents:" << endl; blockchain.printChain(); return 0; } |
Explanation
- Headers:
<iostream>
: For input and output operations.<vector>
: For using thestd::vector
container.<string>
: For using thestd::string
class.<sstream>
: For string stream operations.<iomanip>
: For formatting output.<openssl/sha.h>
: For SHA256 hashing. (You need OpenSSL library installed.)
- Function
sha256
:- Parameters:
const string& input
: The input string to hash.
- Functionality:
- Computes the SHA256 hash of the input string.
- Returns the hash as a hexadecimal string.
- Parameters:
- Block Class:
- Attributes:
previousHash
: The hash of the previous block.hash
: The hash of the current block.data
: The data stored in the block.timestamp
: The time when the block was created.
- Constructor:
- Initializes the block with data and the hash of the previous block.
- Methods:
string calculateHash() const
: Computes the hash of the block using SHA256.
- Attributes:
- Blockchain Class:
- Attributes:
chain
: A vector ofBlock
objects representing the blockchain.
- Methods:
Blockchain()
: Creates the genesis block and initializes the blockchain.void addBlock(const Block& newBlock)
: Adds a new block to the blockchain.void printChain() const
: Prints the details of each block in the blockchain.
- Attributes:
- Main Function:
- Initialization:
- Creates a
Blockchain
object.
- Creates a
- Adding Blocks:
- Adds several blocks to the blockchain with sample data.
- Display:
- Prints the contents of the blockchain.
- Initialization:
Notes:
- Simple Blockchain: This example provides a basic implementation of a blockchain with hashing and linking of blocks. In real-world scenarios, blockchains involve more complex operations and security measures.
- SHA256 Hashing: Uses the SHA256 algorithm for cryptographic hashing. You need the OpenSSL library installed to use the
SHA256
functions. - Genesis Block: The first block in the blockchain is called the genesis block. It is hardcoded in this example.