fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* ------------------------------
  6.   Simple Hash Function (Pedagogical)
  7.   NOT cryptographically secure
  8. -------------------------------- */
  9. unsigned int simple_hash(const char *data)
  10. {
  11. unsigned int hash = 0;
  12. while (*data)
  13. {
  14. hash = hash * 31 + *data;
  15. data++;
  16. }
  17. return hash;
  18. }
  19.  
  20. /* ------------------------------
  21.   Block Structure with Hash Pointer
  22. -------------------------------- */
  23. typedef struct Block
  24. {
  25. char data[100];
  26. unsigned int previous_hash; // hash pointer part
  27. struct Block *previous; // normal pointer part
  28. } Block;
  29.  
  30. /* ------------------------------
  31.   Create New Block
  32. -------------------------------- */
  33. Block *create_block(const char *data, Block *previous_block)
  34. {
  35. Block *new_block = (Block *)malloc(sizeof(Block));
  36. strcpy(new_block->data, data);
  37. new_block->previous = previous_block;
  38.  
  39. if (previous_block == NULL)
  40. new_block->previous_hash = 0; // Genesis block
  41. else
  42. new_block->previous_hash = simple_hash(previous_block->data);
  43.  
  44. return new_block;
  45. }
  46.  
  47. /* ------------------------------
  48.   Verify Integrity of Chain
  49. -------------------------------- */
  50. int verify_chain(Block *current)
  51. {
  52. while (current->previous != NULL)
  53. {
  54. unsigned int recalculated_hash =
  55. simple_hash(current->previous->data);
  56.  
  57. if (recalculated_hash != current->previous_hash)
  58. return 0; // Tampering detected
  59.  
  60. current = current->previous;
  61. }
  62. return 1; // Chain is valid
  63. }
  64.  
  65. /* ------------------------------
  66.   Print Blockchain
  67. -------------------------------- */
  68. void print_chain(Block *current)
  69. {
  70. while (current != NULL)
  71. {
  72. printf("Block Data: %s\n", current->data);
  73. printf("Previous Hash: %u\n", current->previous_hash);
  74. printf("-------------------------\n");
  75.  
  76. current = current->previous;
  77. }
  78. }
  79.  
  80. /* ------------------------------
  81.   Main Demonstration
  82. -------------------------------- */
  83. int main()
  84. {
  85. Block *genesis = create_block("Genesis Block", NULL);
  86. Block *block2 = create_block("Second Block", genesis);
  87. Block *block3 = create_block("Third Block", block2);
  88.  
  89. printf("=== Original Chain ===\n");
  90. print_chain(block3);
  91.  
  92. if (verify_chain(block3))
  93. printf("Chain is VALID\n");
  94. else
  95. printf("Chain is INVALID\n");
  96.  
  97. printf("\n--- Tampering with Block 2 ---\n");
  98. strcpy(block2->data, "Hacked Data");
  99.  
  100. if (verify_chain(block3))
  101. printf("Chain is VALID\n");
  102. else
  103. printf("Chain is INVALID (Tampering Detected)\n");
  104.  
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
=== Original Chain ===
Block Data: Third Block
Previous Hash: 2248563169
-------------------------
Block Data: Second Block
Previous Hash: 3198240725
-------------------------
Block Data: Genesis Block
Previous Hash: 0
-------------------------
Chain is VALID

--- Tampering with Block 2 ---
Chain is INVALID (Tampering Detected)