bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc116.c (5803B)


      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 /* Compile any C program into a .EXE or a .out file. Obtain SHA256 checksum of the file.
      9 */
     10 /* Let Us C, Chap- 23 (Security Programming), Qn No.: D(b) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <stdint.h>
     20 
     21 /* * Self-contained SHA-256 Implementation
     22  * Based on FIPS 180-2 reference logic.
     23  */
     24 
     25 // --- SHA256 Implementation Start ---
     26 typedef struct {
     27     uint8_t data[64];
     28     uint32_t datalen;
     29     uint64_t bitlen;
     30     uint32_t state[8];
     31 } SHA256_CTX;
     32 
     33 #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
     34 #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
     35 #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
     36 #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
     37 #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
     38 #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
     39 #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
     40 
     41 static const uint32_t K[64] = {
     42     0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
     43     0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
     44     0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
     45     0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
     46     0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
     47     0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
     48     0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
     49     0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
     50 };
     51 
     52 void sha256_transform(SHA256_CTX *ctx, const uint8_t data[]) {
     53     uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
     54 
     55     for (i = 0, j = 0; i < 16; ++i, j += 4)
     56         m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
     57     for (; i < 64; ++i)
     58         m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
     59 
     60     a = ctx->state[0]; b = ctx->state[1]; c = ctx->state[2]; d = ctx->state[3];
     61     e = ctx->state[4]; f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7];
     62 
     63     for (i = 0; i < 64; ++i) {
     64         t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i];
     65         t2 = EP0(a) + MAJ(a, b, c);
     66         h = g; g = f; f = e; e = d + t1;
     67         d = c; c = b; b = a; a = t1 + t2;
     68     }
     69 
     70     ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d;
     71     ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h;
     72 }
     73 
     74 void sha256_init(SHA256_CTX *ctx) {
     75     ctx->datalen = 0;
     76     ctx->bitlen = 0;
     77     ctx->state[0] = 0x6a09e667; ctx->state[1] = 0xbb67ae85;
     78     ctx->state[2] = 0x3c6ef372; ctx->state[3] = 0xa54ff53a;
     79     ctx->state[4] = 0x510e527f; ctx->state[5] = 0x9b05688c;
     80     ctx->state[6] = 0x1f83d9ab; ctx->state[7] = 0x5be0cd19;
     81 }
     82 
     83 void sha256_update(SHA256_CTX *ctx, const uint8_t data[], size_t len) {
     84     for (size_t i = 0; i < len; ++i) {
     85         ctx->data[ctx->datalen] = data[i];
     86         ctx->datalen++;
     87         if (ctx->datalen == 64) {
     88             sha256_transform(ctx, ctx->data);
     89             ctx->bitlen += 512;
     90             ctx->datalen = 0;
     91         }
     92     }
     93 }
     94 
     95 void sha256_final(SHA256_CTX *ctx, uint8_t hash[]) {
     96     uint32_t i = ctx->datalen;
     97     if (ctx->datalen < 56) {
     98         ctx->data[i++] = 0x80;
     99         while (i < 56) ctx->data[i++] = 0x00;
    100     } else {
    101         ctx->data[i++] = 0x80;
    102         while (i < 64) ctx->data[i++] = 0x00;
    103         sha256_transform(ctx, ctx->data);
    104         memset(ctx->data, 0, 56);
    105     }
    106     
    107     ctx->bitlen += ctx->datalen * 8;
    108     ctx->data[63] = ctx->bitlen;
    109     ctx->data[62] = ctx->bitlen >> 8;
    110     ctx->data[61] = ctx->bitlen >> 16;
    111     ctx->data[60] = ctx->bitlen >> 24;
    112     ctx->data[59] = ctx->bitlen >> 32;
    113     ctx->data[58] = ctx->bitlen >> 40;
    114     ctx->data[57] = ctx->bitlen >> 48;
    115     ctx->data[56] = ctx->bitlen >> 56;
    116     sha256_transform(ctx, ctx->data);
    117 
    118     for (i = 0; i < 4; ++i) {
    119         hash[i]      = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
    120         hash[i + 4]  = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
    121         hash[i + 8]  = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
    122         hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
    123         hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
    124         hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
    125         hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
    126         hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
    127     }
    128 }
    129 // --- SHA256 Implementation End ---
    130 
    131 int main(int argc, char *argv[]) {
    132     FILE *fp;
    133     char filename[100];
    134     uint8_t buffer[1024];
    135     size_t bytesRead;
    136     SHA256_CTX ctx;
    137     uint8_t hash[32];
    138     int i;
    139 
    140     // Get filename
    141     if (argc < 2) {
    142         printf("Enter filename to checksum (e.g., this_program.exe): ");
    143         scanf("%s", filename);
    144     } else {
    145         strcpy(filename, argv[1]);
    146     }
    147 
    148     fp = fopen(filename, "rb"); // Open in binary mode
    149     if (fp == NULL) {
    150         printf("Error: Could not open file '%s'\n", filename);
    151         return 1;
    152     }
    153 
    154     sha256_init(&ctx);
    155 
    156     while ((bytesRead = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
    157         sha256_update(&ctx, buffer, bytesRead);
    158     }
    159 
    160     sha256_final(&ctx, hash);
    161     fclose(fp);
    162 
    163     printf("SHA256 Checksum of '%s':\n", filename);
    164     for (i = 0; i < 32; i++) {
    165         printf("%02x", hash[i]);
    166     }
    167     printf("\n");
    168 
    169     return 0;
    170 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror