luc115.c (7240B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Obtain MD5 checksum of the following strings and check whether they are same: 9 "Six slippery snails slid slowly seaward." 10 "Six silppery snails slid slowly seaward." 11 */ 12 /* Let Us C, Chap- 23 (Security Programming), Qn No.: D(a) */ 13 14 /* This file is auto-generated by a bot. */ 15 /* This code is not compiled; it is for reference only. */ 16 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <stdint.h> 22 23 /* * Self-contained MD5 Implementation 24 * Based on the public domain reference implementation (RFC 1321) logic. 25 */ 26 27 // --- MD5 Implementation Start --- 28 typedef struct { 29 uint64_t size; // Size of input in bytes 30 uint32_t buffer[4]; // Current accumulation of hash 31 uint8_t input[64]; // Input to be processed 32 } MD5Context; 33 34 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 35 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 36 #define H(x, y, z) ((x) ^ (y) ^ (z)) 37 #define I(x, y, z) ((y) ^ ((x) | (~z))) 38 39 #define ROTL(x, s) (((x) << (s)) | ((x) >> (32 - (s)))) 40 41 #define STEP(f, a, b, c, d, x, t, s) \ 42 (a) += f((b), (c), (d)) + (x) + (t); \ 43 (a) = ROTL((a), (s)); \ 44 (a) += (b); 45 46 void md5Transform(uint32_t state[4], const uint8_t block[64]) { 47 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 48 int i, j; 49 50 // Decode block into x 51 for (i = 0, j = 0; j < 64; i++, j += 4) 52 x[i] = ((uint32_t)block[j]) | (((uint32_t)block[j + 1]) << 8) | 53 (((uint32_t)block[j + 2]) << 16) | (((uint32_t)block[j + 3]) << 24); 54 55 // Round 1 56 STEP(F, a, b, c, d, x[0], 0xd76aa478, 7); 57 STEP(F, d, a, b, c, x[1], 0xe8c7b756, 12); 58 STEP(F, c, d, a, b, x[2], 0x242070db, 17); 59 STEP(F, b, c, d, a, x[3], 0xc1bdceee, 22); 60 STEP(F, a, b, c, d, x[4], 0xf57c0faf, 7); 61 STEP(F, d, a, b, c, x[5], 0x4787c62a, 12); 62 STEP(F, c, d, a, b, x[6], 0xa8304613, 17); 63 STEP(F, b, c, d, a, x[7], 0xfd469501, 22); 64 STEP(F, a, b, c, d, x[8], 0x698098d8, 7); 65 STEP(F, d, a, b, c, x[9], 0x8b44f7af, 12); 66 STEP(F, c, d, a, b, x[10], 0xffff5bb1, 17); 67 STEP(F, b, c, d, a, x[11], 0x895cd7be, 22); 68 STEP(F, a, b, c, d, x[12], 0x6b901122, 7); 69 STEP(F, d, a, b, c, x[13], 0xfd987193, 12); 70 STEP(F, c, d, a, b, x[14], 0xa679438e, 17); 71 STEP(F, b, c, d, a, x[15], 0x49b40821, 22); 72 73 // Round 2 74 STEP(G, a, b, c, d, x[1], 0xf61e2562, 5); 75 STEP(G, d, a, b, c, x[6], 0xc040b340, 9); 76 STEP(G, c, d, a, b, x[11], 0x265e5a51, 14); 77 STEP(G, b, c, d, a, x[0], 0xe9b6c7aa, 20); 78 STEP(G, a, b, c, d, x[5], 0xd62f105d, 5); 79 STEP(G, d, a, b, c, x[10], 0x02441453, 9); 80 STEP(G, c, d, a, b, x[15], 0xd8a1e681, 14); 81 STEP(G, b, c, d, a, x[4], 0xe7d3fbc8, 20); 82 STEP(G, a, b, c, d, x[9], 0x21e1cde6, 5); 83 STEP(G, d, a, b, c, x[14], 0xc33707d6, 9); 84 STEP(G, c, d, a, b, x[3], 0xf4d50d87, 14); 85 STEP(G, b, c, d, a, x[8], 0x455a14ed, 20); 86 STEP(G, a, b, c, d, x[13], 0xa9e3e905, 5); 87 STEP(G, d, a, b, c, x[2], 0xfcefa3f8, 9); 88 STEP(G, c, d, a, b, x[7], 0x676f02d9, 14); 89 STEP(G, b, c, d, a, x[12], 0x8d2a4c8a, 20); 90 91 // Round 3 92 STEP(H, a, b, c, d, x[5], 0xfffa3942, 4); 93 STEP(H, d, a, b, c, x[8], 0x8771f681, 11); 94 STEP(H, c, d, a, b, x[11], 0x6d9d6122, 16); 95 STEP(H, b, c, d, a, x[14], 0xfde5380c, 23); 96 STEP(H, a, b, c, d, x[1], 0xa4beea44, 4); 97 STEP(H, d, a, b, c, x[4], 0x4bdecfa9, 11); 98 STEP(H, c, d, a, b, x[7], 0xf6bb4b60, 16); 99 STEP(H, b, c, d, a, x[10], 0xbebfbc70, 23); 100 STEP(H, a, b, c, d, x[13], 0x289b7ec6, 4); 101 STEP(H, d, a, b, c, x[0], 0xeaa127fa, 11); 102 STEP(H, c, d, a, b, x[3], 0xd4ef3085, 16); 103 STEP(H, b, c, d, a, x[6], 0x04881d05, 23); 104 STEP(H, a, b, c, d, x[9], 0xd9d4d039, 4); 105 STEP(H, d, a, b, c, x[12], 0xe6db99e5, 11); 106 STEP(H, c, d, a, b, x[15], 0x1fa27cf8, 16); 107 STEP(H, b, c, d, a, x[2], 0xc4ac5665, 23); 108 109 // Round 4 110 STEP(I, a, b, c, d, x[0], 0xf4292244, 6); 111 STEP(I, d, a, b, c, x[7], 0x432aff97, 10); 112 STEP(I, c, d, a, b, x[14], 0xab9423a7, 15); 113 STEP(I, b, c, d, a, x[5], 0xfc93a039, 21); 114 STEP(I, a, b, c, d, x[12], 0x655b59c3, 6); 115 STEP(I, d, a, b, c, x[3], 0x8f0ccc92, 10); 116 STEP(I, c, d, a, b, x[10], 0xffeff47d, 15); 117 STEP(I, b, c, d, a, x[1], 0x85845dd1, 21); 118 STEP(I, a, b, c, d, x[8], 0x6fa87e4f, 6); 119 STEP(I, d, a, b, c, x[15], 0xfe2ce6e0, 10); 120 STEP(I, c, d, a, b, x[6], 0xa3014314, 15); 121 STEP(I, b, c, d, a, x[13], 0x4e0811a1, 21); 122 STEP(I, a, b, c, d, x[4], 0xf7537e82, 6); 123 STEP(I, d, a, b, c, x[11], 0xbd3af235, 10); 124 STEP(I, c, d, a, b, x[2], 0x2ad7d2bb, 15); 125 STEP(I, b, c, d, a, x[9], 0xeb86d391, 21); 126 127 state[0] += a; 128 state[1] += b; 129 state[2] += c; 130 state[3] += d; 131 } 132 133 void md5Init(MD5Context *ctx) { 134 ctx->size = 0; 135 ctx->buffer[0] = 0x67452301; 136 ctx->buffer[1] = 0xefcdab89; 137 ctx->buffer[2] = 0x98badcfe; 138 ctx->buffer[3] = 0x10325476; 139 } 140 141 void md5Update(MD5Context *ctx, const uint8_t *input, size_t inputLen) { 142 size_t i, index, partLen; 143 index = (size_t)((ctx->size >> 3) & 0x3f); 144 ctx->size += (uint64_t)inputLen * 8; 145 partLen = 64 - index; 146 147 if (inputLen >= partLen) { 148 memcpy(&ctx->input[index], input, partLen); 149 md5Transform(ctx->buffer, ctx->input); 150 for (i = partLen; i + 63 < inputLen; i += 64) 151 md5Transform(ctx->buffer, &input[i]); 152 index = 0; 153 } else { 154 i = 0; 155 } 156 memcpy(&ctx->input[index], &input[i], inputLen - i); 157 } 158 159 void md5Final(uint8_t digest[16], MD5Context *ctx) { 160 uint8_t bits[8]; 161 size_t index, padLen; 162 static const uint8_t PADDING[64] = {0x80}; // Remaining zeros implicit 163 164 // Save number of bits 165 for (int i = 0; i < 8; i++) 166 bits[i] = (uint8_t)((ctx->size >> (i * 8)) & 0xff); 167 168 index = (size_t)((ctx->size >> 3) & 0x3f); 169 padLen = (index < 56) ? (56 - index) : (120 - index); 170 md5Update(ctx, PADDING, padLen); 171 md5Update(ctx, bits, 8); 172 173 // Encode state into digest 174 for (int i = 0; i < 16; i++) 175 digest[i] = (uint8_t)((ctx->buffer[i >> 2] >> ((i & 3) * 8)) & 0xff); 176 } 177 // --- MD5 Implementation End --- 178 179 void print_hash(uint8_t *digest) { 180 for (int i = 0; i < 16; i++) printf("%02x", digest[i]); 181 printf("\n"); 182 } 183 184 int main() { 185 char str1[] = "Six slippery snails slid slowly seaward."; 186 char str2[] = "Six silppery snails slid slowly seaward."; // Typo 'silppery' 187 188 uint8_t hash1[16], hash2[16]; 189 MD5Context ctx; 190 191 printf("String 1: %s\n", str1); 192 md5Init(&ctx); 193 md5Update(&ctx, (uint8_t*)str1, strlen(str1)); 194 md5Final(hash1, &ctx); 195 printf("MD5: "); 196 print_hash(hash1); 197 198 printf("\nString 2: %s\n", str2); 199 md5Init(&ctx); 200 md5Update(&ctx, (uint8_t*)str2, strlen(str2)); 201 md5Final(hash2, &ctx); 202 printf("MD5: "); 203 print_hash(hash2); 204 205 // Compare 206 int same = 1; 207 for(int i=0; i<16; i++) if(hash1[i] != hash2[i]) same = 0; 208 209 printf("\nConclusion: The MD5 checksums are %s.\n", same ? "IDENTICAL" : "DIFFERENT"); 210 if (!same) printf("(Even a small change in input creates a completely different hash)\n"); 211 212 return 0; 213 }