example_easyhashing.c (14677B)
1 /* 2 * EasyHashing API Demo 3 * Simple examples of using the easy hashing interface 4 */ 5 6 #include "EasyHashing.h" 7 #include <stdio.h> 8 #include <string.h> 9 10 int main(void) { 11 printf("╔════════════════════════════════════════════╗\n"); 12 printf("║ EasyHashing API - Simple Demo ║\n"); 13 printf("╚════════════════════════════════════════════╝\n\n"); 14 15 // Example 1: Default hash (SHA-256) 16 printf("══════════════════════════════════════════════\n"); 17 printf("Example 1: Default HASH() macro (SHA-256)\n"); 18 printf("══════════════════════════════════════════════\n"); 19 20 const char *message = "Hello, World!"; 21 printf("Message: \"%s\"\n\n", message); 22 23 HashResult hash1 = HASH((uint8_t*)message, strlen(message)); 24 HASH_PRINT(hash1); 25 HASH_FREE(hash1); 26 27 // Example 2: String convenience 28 printf("\n══════════════════════════════════════════════\n"); 29 printf("Example 2: String Convenience Macros\n"); 30 printf("══════════════════════════════════════════════\n"); 31 32 HashResult hash2 = HASH_STR("Quick hash of a string"); 33 HASH_PRINT(hash2); 34 HASH_FREE(hash2); 35 36 // Example 3: Explicit algorithm selection 37 printf("\n══════════════════════════════════════════════\n"); 38 printf("Example 3: Explicit Algorithm Selection\n"); 39 printf("══════════════════════════════════════════════\n"); 40 41 const char *data = "Test data for hashing"; 42 printf("Data: \"%s\"\n\n", data); 43 44 HashResult sha256_hash = SHA256((uint8_t*)data, strlen(data)); 45 HASH_PRINT(sha256_hash); 46 47 HashResult sha512_hash = SHA512((uint8_t*)data, strlen(data)); 48 HASH_PRINT(sha512_hash); 49 50 HashResult sha1_hash = SHA1((uint8_t*)data, strlen(data)); 51 HASH_PRINT(sha1_hash); 52 53 HashResult md5_hash = MD5((uint8_t*)data, strlen(data)); 54 HASH_PRINT(md5_hash); 55 56 HashResult crc32_hash = CRC32((uint8_t*)data, strlen(data)); 57 HASH_PRINT(crc32_hash); 58 59 HASH_FREE(sha256_hash); 60 HASH_FREE(sha512_hash); 61 HASH_FREE(sha1_hash); 62 HASH_FREE(md5_hash); 63 HASH_FREE(crc32_hash); 64 65 // Example 4: Algorithm-specific string macros 66 printf("\n══════════════════════════════════════════════\n"); 67 printf("Example 4: Algorithm-Specific String Macros\n"); 68 printf("══════════════════════════════════════════════\n"); 69 70 HashResult h1 = SHA256_STR("password123"); 71 HashResult h2 = SHA512_STR("secret"); 72 HashResult h3 = MD5_STR("legacy_data"); 73 HashResult h4 = CRC32_STR("checksum_me"); 74 75 HASH_PRINT(h1); 76 HASH_PRINT(h2); 77 HASH_PRINT(h3); 78 HASH_PRINT(h4); 79 80 HASH_FREE(h1); 81 HASH_FREE(h2); 82 HASH_FREE(h3); 83 HASH_FREE(h4); 84 85 // Example 5: Get hex string 86 printf("\n══════════════════════════════════════════════\n"); 87 printf("Example 5: Convert to Hex String\n"); 88 printf("══════════════════════════════════════════════\n"); 89 90 HashResult h5 = SHA256_STR("example"); 91 char *hex = HASH_HEX(h5); 92 if (hex) { 93 printf("Hash as hex string: %s\n", hex); 94 printf("String length: %zu\n", strlen(hex)); 95 free(hex); 96 } 97 HASH_FREE(h5); 98 99 // Example 6: Compare hashes 100 printf("\n══════════════════════════════════════════════\n"); 101 printf("Example 6: Hash Comparison\n"); 102 printf("══════════════════════════════════════════════\n"); 103 104 HashResult h6 = SHA256_STR("same_data"); 105 HashResult h7 = SHA256_STR("same_data"); 106 HashResult h8 = SHA256_STR("different_data"); 107 108 printf("Comparing hashes:\n"); 109 printf(" SHA256(\"same_data\") == SHA256(\"same_data\"): %s\n", 110 HASH_EQUALS(h6, h7) ? "✓ Match" : "✗ No match"); 111 printf(" SHA256(\"same_data\") == SHA256(\"different_data\"): %s\n", 112 HASH_EQUALS(h6, h8) ? "✓ Match" : "✗ No match"); 113 114 HASH_FREE(h6); 115 HASH_FREE(h7); 116 HASH_FREE(h8); 117 118 // Example 7: Quick hash-and-print (debugging) 119 printf("\n══════════════════════════════════════════════\n"); 120 printf("Example 7: Quick Hash-and-Print (Debug)\n"); 121 printf("══════════════════════════════════════════════\n"); 122 123 HASH_QUICK_STR("Debug hash", "debug_data"); 124 SHA256_QUICK("File checksum", (uint8_t*)"file_content", 12); 125 MD5_QUICK("Legacy hash", (uint8_t*)"old_system", 10); 126 CRC32_QUICK("Packet CRC", (uint8_t*)"packet_data", 11); 127 128 // Example 8: Practical use cases 129 printf("\n══════════════════════════════════════════════\n"); 130 printf("Example 8: Practical Use Cases\n"); 131 printf("══════════════════════════════════════════════\n"); 132 133 // File integrity check (simulated) 134 printf("\n[Use Case 1: File Integrity]\n"); 135 const char *file_content = "Important file content..."; 136 HashResult file_hash = SHA256((uint8_t*)file_content, strlen(file_content)); 137 printf("Store this hash for later verification:\n"); 138 HASH_PRINT_LABEL("File hash", file_hash); 139 HASH_FREE(file_hash); 140 141 // Password verification (simplified - NOT production code!) 142 printf("\n[Use Case 2: Password Verification (Simplified)]\n"); 143 const char *stored_password_hash = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"; 144 printf("Stored hash: %s\n", stored_password_hash); 145 146 HashResult login_attempt = SHA256_STR("password"); 147 char *login_hash = HASH_HEX(login_attempt); 148 if (login_hash) { 149 printf("Login attempt hash: %s\n", login_hash); 150 printf("Match: %s\n", strcmp(stored_password_hash, login_hash) == 0 ? "✓ Yes" : "✗ No"); 151 printf("Note: Use proper password hashing (bcrypt/argon2) in production!\n"); 152 free(login_hash); 153 } 154 HASH_FREE(login_attempt); 155 156 // Data deduplication 157 printf("\n[Use Case 3: Data Deduplication]\n"); 158 const char *block1 = "Data block 1"; 159 const char *block2 = "Data block 2"; 160 const char *block3 = "Data block 1"; // Duplicate 161 162 HashResult b1 = SHA256((uint8_t*)block1, strlen(block1)); 163 HashResult b2 = SHA256((uint8_t*)block2, strlen(block2)); 164 HashResult b3 = SHA256((uint8_t*)block3, strlen(block3)); 165 166 char *b1_hex = HASH_HEX(b1); 167 char *b2_hex = HASH_HEX(b2); 168 char *b3_hex = HASH_HEX(b3); 169 170 printf("Block 1 hash: %s\n", b1_hex); 171 printf("Block 2 hash: %s\n", b2_hex); 172 printf("Block 3 hash: %s\n", b3_hex); 173 printf("Blocks 1 and 3 are identical (detected via hash)\n"); 174 175 free(b1_hex); 176 free(b2_hex); 177 free(b3_hex); 178 HASH_FREE(b1); 179 HASH_FREE(b2); 180 HASH_FREE(b3); 181 182 // Checksums 183 printf("\n[Use Case 4: Fast Checksums with CRC32]\n"); 184 const char *packet = "Network packet data..."; 185 HashResult packet_crc = CRC32((uint8_t*)packet, strlen(packet)); 186 printf("Packet: \"%s\"\n", packet); 187 HASH_PRINT_LABEL("CRC32 checksum", packet_crc); 188 HASH_FREE(packet_crc); 189 190 // Example 9: Different data types 191 printf("\n══════════════════════════════════════════════\n"); 192 printf("Example 9: Hashing Different Data Types\n"); 193 printf("══════════════════════════════════════════════\n"); 194 195 // Binary data 196 uint8_t binary_data[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE}; 197 HashResult bin_hash = SHA256(binary_data, sizeof(binary_data)); 198 printf("Binary data (8 bytes): "); 199 for (size_t i = 0; i < sizeof(binary_data); i++) { 200 printf("%02x ", binary_data[i]); 201 } 202 printf("\n"); 203 HASH_PRINT_LABEL("SHA-256", bin_hash); 204 HASH_FREE(bin_hash); 205 206 // Integer data 207 int number = 42; 208 HashResult int_hash = SHA256((uint8_t*)&number, sizeof(number)); 209 printf("\nInteger value: %d\n", number); 210 HASH_PRINT_LABEL("SHA-256", int_hash); 211 HASH_FREE(int_hash); 212 213 // Example 10: Base64 encoding 214 printf("\n══════════════════════════════════════════════\n"); 215 printf("Example 10: Base64 Hash Encoding\n"); 216 printf("══════════════════════════════════════════════\n"); 217 218 // Get base64 from hash result 219 printf("\n[Method 1: Hash then convert to base64]\n"); 220 HashResult h_b64 = SHA256_STR("test data"); 221 char *b64_from_hash = HASH_B64(h_b64); 222 printf("SHA-256 hash (hex): "); 223 char *hex_compare = HASH_HEX(h_b64); 224 if (hex_compare) { 225 printf("%s\n", hex_compare); 226 free(hex_compare); 227 } 228 printf("SHA-256 hash (base64): %s\n", b64_from_hash ? b64_from_hash : "(null)"); 229 free(b64_from_hash); 230 HASH_FREE(h_b64); 231 232 // One-shot base64 string hashing 233 printf("\n[Method 2: One-shot string to base64]\n"); 234 char *b64_str1 = HASH_B64_STR("hello world"); 235 char *b64_str2 = SHA512_B64_STR("secure password"); 236 char *b64_str3 = MD5_B64_STR("legacy data"); 237 238 printf("SHA-256 base64 of \"hello world\": %s\n", b64_str1 ? b64_str1 : "(null)"); 239 printf("SHA-512 base64 of \"secure password\": %s\n", b64_str2 ? b64_str2 : "(null)"); 240 printf("MD5 base64 of \"legacy data\": %s\n", b64_str3 ? b64_str3 : "(null)"); 241 242 free(b64_str1); 243 free(b64_str2); 244 free(b64_str3); 245 246 // One-shot base64 data hashing 247 printf("\n[Method 3: One-shot binary data to base64]\n"); 248 uint8_t binary[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F}; // "Hello" 249 char *b64_data1 = HASH_B64_DATA(binary, sizeof(binary)); 250 char *b64_data2 = SHA256_B64_DATA(binary, sizeof(binary)); 251 char *b64_data3 = CRC32_B64_DATA(binary, sizeof(binary)); 252 253 printf("Default (SHA-256) base64: %s\n", b64_data1 ? b64_data1 : "(null)"); 254 printf("SHA-256 base64: %s\n", b64_data2 ? b64_data2 : "(null)"); 255 printf("CRC32 base64: %s\n", b64_data3 ? b64_data3 : "(null)"); 256 257 free(b64_data1); 258 free(b64_data2); 259 free(b64_data3); 260 261 // Practical base64 use case 262 printf("\n[Use Case: API Authentication Token]\n"); 263 const char *api_key = "my_secret_api_key_12345"; 264 char *auth_token = SHA256_B64_STR(api_key); 265 printf("API Key: %s\n", api_key); 266 printf("Auth Token: %s\n", auth_token ? auth_token : "(null)"); 267 printf("→ Base64 is URL-safe and compact for tokens\n"); 268 free(auth_token); 269 270 // Example 11: Cheat sheet 271 printf("\n══════════════════════════════════════════════\n"); 272 printf("Quick Reference Cheat Sheet\n"); 273 printf("══════════════════════════════════════════════\n"); 274 printf("\n"); 275 printf("Default (SHA-256):\n"); 276 printf(" HASH(data, len) - Hash binary data\n"); 277 printf(" HASH_STR(str) - Hash string\n"); 278 printf("\n"); 279 printf("Specific algorithms:\n"); 280 printf(" SHA256(data, len) - SHA-256 hash\n"); 281 printf(" SHA512(data, len) - SHA-512 hash\n"); 282 printf(" SHA1(data, len) - SHA-1 hash (legacy)\n"); 283 printf(" MD5(data, len) - MD5 hash (legacy)\n"); 284 printf(" CRC32(data, len) - CRC32 checksum\n"); 285 printf("\n"); 286 printf("String convenience:\n"); 287 printf(" SHA256_STR(str) - Hash string with SHA-256\n"); 288 printf(" SHA512_STR(str) - Hash string with SHA-512\n"); 289 printf(" MD5_STR(str) - Hash string with MD5\n"); 290 printf(" CRC32_STR(str) - Hash string with CRC32\n"); 291 printf("\n"); 292 printf("Operations:\n"); 293 printf(" HASH_PRINT(result) - Print hash\n"); 294 printf(" HASH_HEX(result) - Get hex string (must free)\n"); 295 printf(" HASH_B64(result) - Get base64 string (must free)\n"); 296 printf(" HASH_EQUALS(r1, r2) - Compare two hashes\n"); 297 printf(" HASH_FREE(result) - Free memory\n"); 298 printf("\n"); 299 printf("Base64 output:\n"); 300 printf(" HASH_B64_STR(str) - Hash string, return base64\n"); 301 printf(" HASH_B64_DATA(data, len) - Hash data, return base64\n"); 302 printf(" SHA256_B64_STR(str) - SHA-256 string to base64\n"); 303 printf(" SHA512_B64_STR(str) - SHA-512 string to base64\n"); 304 printf(" MD5_B64_STR(str) - MD5 string to base64\n"); 305 printf(" CRC32_B64_STR(str) - CRC32 string to base64\n"); 306 printf("\n"); 307 printf("Quick debug:\n"); 308 printf(" HASH_QUICK_STR(label, s) - Hash and print string\n"); 309 printf(" SHA256_QUICK(label, d, l) - SHA-256 and print\n"); 310 printf(" CRC32_QUICK(label, d, l) - CRC32 and print\n"); 311 312 printf("\n╔════════════════════════════════════════════╗\n"); 313 printf("║ Demo Complete! ║\n"); 314 printf("╚════════════════════════════════════════════╝\n"); 315 316 return 0; 317 }