example_hash.c (8506B)
1 /* 2 * Unified Hash API Example 3 * Demonstrates usage of SHA-256, SHA-512, SHA-1, MD5, and CRC32 4 */ 5 6 #include "hash.h" 7 #include <stdio.h> 8 #include <string.h> 9 10 int main(void) { 11 printf("╔═══════════════════════════════════════════╗\n"); 12 printf("║ Unified Hash API - Comprehensive Demo ║\n"); 13 printf("╚═══════════════════════════════════════════╝\n\n"); 14 15 // Test data 16 const char *test_strings[] = { 17 "", 18 "abc", 19 "The quick brown fox jumps over the lazy dog", 20 "Hello, World!" 21 }; 22 23 // Test all algorithms 24 for (int i = 0; i < 4; i++) { 25 const char *str = test_strings[i]; 26 printf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); 27 printf("Input: \"%s\"\n", str); 28 printf("Length: %zu bytes\n", strlen(str)); 29 printf("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); 30 31 // SHA-256 32 HashResult sha256_result = hash_compute(HASH_SHA256, (uint8_t*)str, strlen(str)); 33 hash_print("SHA-256", &sha256_result); 34 hash_free(&sha256_result); 35 36 // SHA-512 37 HashResult sha512_result = hash_compute(HASH_SHA512, (uint8_t*)str, strlen(str)); 38 hash_print("SHA-512", &sha512_result); 39 hash_free(&sha512_result); 40 41 // SHA-1 42 HashResult sha1_result = hash_compute(HASH_SHA1, (uint8_t*)str, strlen(str)); 43 hash_print("SHA-1 ", &sha1_result); 44 hash_free(&sha1_result); 45 46 // MD5 47 HashResult md5_result = hash_compute(HASH_MD5, (uint8_t*)str, strlen(str)); 48 hash_print("MD5 ", &md5_result); 49 hash_free(&md5_result); 50 51 // CRC32 52 HashResult crc32_result = hash_compute(HASH_CRC32, (uint8_t*)str, strlen(str)); 53 hash_print("CRC32 ", &crc32_result); 54 hash_free(&crc32_result); 55 56 printf("\n"); 57 } 58 59 // Demonstrate macro usage 60 printf("═════════════════════════════════════════════\n"); 61 printf(" Macro Usage Example\n"); 62 printf("═════════════════════════════════════════════\n\n"); 63 64 const char *password = "MySecretPassword123!"; 65 printf("Hashing password: \"%s\"\n\n", password); 66 67 HashResult h1 = HASH(HASH_SHA256, password); 68 hash_print("SHA-256", &h1); 69 70 HashResult h2 = hash_sha512_str(password); 71 hash_print("SHA-512", &h2); 72 73 hash_free(&h1); 74 hash_free(&h2); 75 76 // Demonstrate hex conversion 77 printf("\n═════════════════════════════════════════════\n"); 78 printf(" Hex String Conversion\n"); 79 printf("═════════════════════════════════════════════\n\n"); 80 81 HashResult h3 = hash_md5_str("test"); 82 char *hex = hash_to_hex(&h3); 83 if (hex) { 84 printf("MD5 of \"test\" as string: %s\n", hex); 85 free(hex); 86 } 87 hash_free(&h3); 88 89 // Demonstrate hash comparison 90 printf("\n═════════════════════════════════════════════\n"); 91 printf(" Hash Comparison\n"); 92 printf("═════════════════════════════════════════════\n\n"); 93 94 HashResult h4 = hash_sha256_str("hello"); 95 HashResult h5 = hash_sha256_str("hello"); 96 HashResult h6 = hash_sha256_str("world"); 97 98 printf("Comparing hashes:\n"); 99 printf(" SHA-256(\"hello\") == SHA-256(\"hello\"): %s\n", 100 hash_equals(&h4, &h5) ? "✓ Equal" : "✗ Not equal"); 101 printf(" SHA-256(\"hello\") == SHA-256(\"world\"): %s\n", 102 hash_equals(&h4, &h6) ? "✓ Equal" : "✗ Not equal"); 103 104 hash_free(&h4); 105 hash_free(&h5); 106 hash_free(&h6); 107 108 // Security warnings 109 printf("\n═════════════════════════════════════════════\n"); 110 printf(" Security Notes\n"); 111 printf("═════════════════════════════════════════════\n\n"); 112 113 printf("✓ SHA-256: Recommended for most cryptographic uses\n"); 114 printf("✓ SHA-512: Recommended for maximum security\n"); 115 printf("⚠ SHA-1: Cryptographically broken, use only for compatibility\n"); 116 printf("⚠ MD5: Cryptographically broken, use only for checksums\n"); 117 printf("✓ CRC32: Good for error detection, NOT for security\n"); 118 119 printf("\n═════════════════════════════════════════════\n"); 120 printf(" Performance Tips\n"); 121 printf("═════════════════════════════════════════════\n\n"); 122 123 printf("• SHA-256 uses hardware acceleration (SHA extensions) when available\n"); 124 printf("• CRC32 uses hardware acceleration (SSE 4.2) when available\n"); 125 printf("• SHA-512 is optimized for 64-bit processors\n"); 126 printf("• Use CRC32 for checksums, SHA-256/512 for security\n"); 127 128 printf("\n═════════════════════════════════════════════\n"); 129 printf(" Common Use Cases\n"); 130 printf("═════════════════════════════════════════════\n\n"); 131 132 // File integrity check simulation 133 const char *file_data = "This is the content of a file..."; 134 printf("Use Case 1: File Integrity Verification\n"); 135 printf("----------------------------------------\n"); 136 HashResult file_hash = hash_sha256_str(file_data); 137 printf("Original file hash: "); 138 char *file_hex = hash_to_hex(&file_hash); 139 if (file_hex) { 140 printf("%s\n", file_hex); 141 free(file_hex); 142 } 143 printf("→ Store this hash to verify file integrity later\n\n"); 144 hash_free(&file_hash); 145 146 // Password storage simulation (simplified) 147 printf("Use Case 2: Password Hashing (simplified)\n"); 148 printf("------------------------------------------\n"); 149 const char *user_password = "user_password_123"; 150 HashResult pwd_hash = hash_sha512_str(user_password); 151 printf("Password hash to store: "); 152 char *pwd_hex = hash_to_hex(&pwd_hash); 153 if (pwd_hex) { 154 printf("%s\n", pwd_hex); 155 printf("→ Never store plaintext passwords!\n"); 156 printf("→ In production, use proper password hashing (bcrypt, argon2)\n\n"); 157 free(pwd_hex); 158 } 159 hash_free(&pwd_hash); 160 161 // Data deduplication simulation 162 printf("Use Case 3: Data Deduplication\n"); 163 printf("--------------------------------\n"); 164 const char *chunk1 = "Data chunk 1"; 165 const char *chunk2 = "Data chunk 2"; 166 const char *chunk3 = "Data chunk 1"; // Duplicate of chunk1 167 168 HashResult c1 = hash_sha256_str(chunk1); 169 HashResult c2 = hash_sha256_str(chunk2); 170 HashResult c3 = hash_sha256_str(chunk3); 171 172 printf("Chunk 1 hash: "); 173 char *c1_hex = hash_to_hex(&c1); 174 if (c1_hex) { printf("%s\n", c1_hex); free(c1_hex); } 175 176 printf("Chunk 2 hash: "); 177 char *c2_hex = hash_to_hex(&c2); 178 if (c2_hex) { printf("%s\n", c2_hex); free(c2_hex); } 179 180 printf("Chunk 3 hash: "); 181 char *c3_hex = hash_to_hex(&c3); 182 if (c3_hex) { printf("%s\n", c3_hex); free(c3_hex); } 183 184 printf("→ Chunk 1 and 3 have same hash (duplicates detected)\n"); 185 186 hash_free(&c1); 187 hash_free(&c2); 188 hash_free(&c3); 189 190 printf("\n═════════════════════════════════════════════\n"); 191 printf(" Demo Complete!\n"); 192 printf("═════════════════════════════════════════════\n"); 193 194 return 0; 195 }