char* input = calloc(1, 1000);
fgets(input, 1000, stdin);
char* hash = calloc(1, 32);
SHA256((const char*)input, 1000, hash);
Since SHA256 distills randomness, we know that hash points to 32 bytes with as much randomness in it as in the initial string the user types, which can be up to 1000 bytes. Since it is generally safe to assume that every byte the user types has one bit of entropy in it, if the user hits 256 keys before hitting enter, then hash points to 256 bits of randomness.
int x = 32 / sizeof(unsigned int*);
while(x-- > 0){
printf("...%u...", *(unsigned int*)&hash[sizeof(unsigned int) * x]);
}
this goes through the output hash 4 bytes at a time and prints the 4 bytes as an unsigned int. Since the hash value is 256 bits of randomness, each of the printed numbers are random and up to 32 bits. If you want to make more than sha256 bytesize / sizeof(unsigned int) random numbers with the same seed material you would do something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#define sha256_bytesize 32
#define input_bytesize_max 1000
int main()
{
printf("Pound randomly on your keyboard for a while and then hit enter: \n\n");
char* input = calloc(1, input_bytesize_max);
fgets(input, input_bytesize_max, stdin);
char* hash = calloc(1, sha256_bytesize);
SHA256((const char*)input, input_bytesize_max, hash);
int numbers_to_generate = 1024; // must be evenly divisible by 8
int cycles = numbers_to_generate / (sha256_bytesize / sizeof(unsigned int));
int x;
for(x = sha256_bytesize / sizeof(unsigned int); cycles; cycles--){
while(x-- > 0){
printf("%u, ", *(unsigned int*)&hash[sizeof(unsigned int) * x]);
}
SHA256_CTX digest_ctx;
SHA256_Init(&digest_ctx);
SHA256_Update(&digest_ctx, input, input_bytesize_max);
SHA256_Update(&digest_ctx, hash, sha256_bytesize);
SHA256_Final((unsigned char*)hash, &digest_ctx);
x = sha256_bytesize / sizeof(unsigned int);
}
return 1;
}
note that the original input is hashed to determine the original hash, and then from there the input is hashed together with the previous hash in order to determine the next hash. Without knowing the original input, which is the seed, an attacker can not determine what numbers will come next by knowing any of the given numbers.
Pound randomly on your keyboard for a while and then hit enter:
ewofkpewofkfkfkewpfkpoew
1131825061, 3688026824, 1231966063, 3835540840, 3895683430, 100125131, 2080584810, 1174908134, 3434929624, 3875039201, 3071522454, 4143845028, 3458178688, 1989285470, 1125142825, 676834196, 3650598165, 3459411485, 1406189030, 1376089312, 1399940066, 2028894875, 2571302588, 1895924890, 2177742134, 3327946134, 2223606966, 1002549878, 3489905312, 3744919418, 1058878650, 866029914, 395102763, 2740964949, 1034838232, 2554586916, 2048139009, 955301508, 2762861201, 1015224392, 700357896, 3078575593, 2610097327, 4011768463, 3911089829, 976734916, 1770142116, 913487976, 4126269859, 1586109218, 2362801860, 632276841, 4008883232, 611869321, 3564339646, 849225957, 2368641573, 730657830, 3915227568, 1905431881, 943347584, 2537902018, 925828280, 2416726404, 2085729931, 2839515556, 2840179316, 1041815396, 1071756885, 2971405229, 4288722269, 724981196, 2501557562, 3580583715, 3407327544, 2673202105, 1476378478, 522994926, 2423416914, 783203073, 3496945928, 1877228586, 2240303800, 1139669603, 1158863945, 3753521370, 4096674841, 2876740655, 4007404669, 3562863030, 2596617445, 2445288485, 3233054462, 1247761355, 3704164861, 3170364380, 3836729434, 2725146589, 4147929128, 3087740256, 1936047366, 4253737861, 252338802, 2218407517, 4003946582, 2324847090, 3082065373, 2938476361, 1855091543, 3181203504, 278679784, 3831819330, 3726952201, 4230906935, 1567772450, 2353106759, 2017912504, 2898707071, 927712183, 2748683864, 2034867947, 2286301538, 37316200, 146697774, 419575480, 3023900409, 1554208096, 2255379805, 2129178774, 1832306274, 3709693727, 1158945100, 3885577929, 108382223, 1681468813, 2222855059, 3538985238, 1781661, 325685693, 2574073320, 3682393197, 1935375562, 1824054295, 4255299601, 1374818890, 2432445517, 3320675812, 58168969, 1687243179, 2972579585, 41892015, 1131564771, 453564015, 2669395313, 543130798, 2944727200, 195685940, 1296584579, 3843488452, 184299526, 4130237899, 801814728, 1792487551, 648181115, 23257902, 571372193, 1001157529, 2791336529, 2971566889, 923682043, 3506482788, 3747088898, 900758611, 2709379818, 3266513084, 2020571485, 4224204788, 464648919, 210190141, 86858892, 3893707481, 343477454, 393472892, 2023636984, 1657329957, 3653942738, 798089328, 2934929443, 3168866288, 2010130405, 1132391558, 2846745634, 3641298431, 45068674, 4259134821, 3964381122, 1921911720, 3920526807, 1134800417, 1297631980, 202942715, 279485134, 311081715, 773855100, 3453975190, 769087683, 2604270473, 2635005511, 540049818, 3437818205, 3010282740, 1153327421, 2182921009, 3661448462, 1122928384, 3834425893, 1399432444, 2712126042, 3164621221, 3069351572, 281146602, 4128225209, 1698301003, 1169935036, 4141370673, 1338751159, 1468291099, 476242480, 4038722735, 456455532, 1873586460, 1066800148, 730158053, 439367008, 701573547, 2011690664, 3069062954, 504176327, 1949327869, 990165602, 2009068904, 1378914809, 2838828185, 3202093389, 1503479913, 1067524315, 3924953662, 2361424352, 1203789083, 278972846, 495474496, 3147856700, 1419929674, 2005992688, 298715854, 1230067326, 1820965332, 874744817, 1313337212, 2301344273, 2291389508, 3379599601, 2494210340, 4179941271, 2947071300, 1410664041, 852214619, 2091126356, 2340712558, 3371302046, 1638197237, 1988377879, 2843068184, 839910806, 3292734381, 2810624105, 1419489147, 3922079063, 1897039338, 575954582, 1980164931, 3019008623, 3334616146, 750479126, 1215510368, 574716984, 4156439648, 2672883897, 783651620, 995147558, 199794639, 1267660435, 2980399644, 1074630743, 3271238980, 2755642229, 409873928, 4151378132, 1711776621, 2668995087, 3078689802, 459755588, 1516623802, 3797715371, 2788089906, 2683535398, 2530337901, 4239447367, 3152018247, 911951204, 2159368386, 1120897222, 2935288821, 207926990, 1782127795, 3394933846, 2979939893, 3622123052, 2034789030, 2679150463, 2897795785, 87475486, 3994960773, 4131982448, 1884737313, 3504562954, 2560447894, 4112690194, 3318861311, 976451147, 1011861312, 2666536440, 3310435290, 3818496971, 2529605336, 1781567681, 1071862382, 709475180, 1393781558, 1927649894, 3106591565, 4048318678, 457889737, 4261246113, 2301456581, 1978726878, 40467572, 2678448096, 3940009970, 4152892592, 1195989556, 1490238979, 2933864626, 916963898, 1719008729, 4188873015, 1097339721, 2683727951, 2928864981, 1339696191, 3235732567, 2372639035, 1255613943, 2222450843, 3596715244, 1952805296, 3149719875, 3778488406, 3053723161, 894411363, 2311314654, 3342993995, 3788124544, 1764066626, 514424945, 135875307, 4200939669, 3852912621, 586166270, 1886516394, 592720731, 3915707150, 954526429, 4128085329, 3707671186, 2349835295, 1648919880, 2668617841, 3644165730, 952609991, 2358545089, 3450973756, 4006885596, 3136060004, 1000265955, 3728269305, 1728174558, 1947551414, 2025618422, 2174200413, 383732023, 1141303053, 2572426234, 3312726418, 1610926562, 2315720138, 2246548015, 3725991361, 3247741733, 807584781, 2973704356, 4046820802, 4276638001, 2502534087, 1987390684, 1024248478, 816451332, 2837656358, 1620496390, 1206616750, 4088876987, 2538011675, 1089443033, 4130105975, 1333065656, 597485786, 2970368116, 3379020716, 3693217128, 101700833, 2880003371, 3363899816, 2755469039, 572839881, 3713233411, 2799793260, 644708209, 729858670, 1023928487, 904411366, 1368678657, 1415153704, 1263528891, 2986888444, 412525905, 3776776372, 2007618083, 1023488133, 1360396902, 3085551208, 368780118, 472271455, 3368210622, 1486467650, 748038476, 2903780231, 926337708, 1913556323, 1769239640, 467775424, 884993483, 1880753682, 2678255172, 3115835871, 279085005, 1854137915, 3825887488, 2880145003, 4201376872, 996139613, 412950211, 3007160973, 2001193329, 1676883123, 164652730, 270667872, 2469936608, 656766275, 2225654456, 1466172415, 97558823, 3968500559, 2331929664, 387472071, 2776938806, 3133954, 3513609806, 1395751084, 3930376128, 58770381, 3543810575, 1555294880, 626559765, 3588798871, 620328231, 1868112306, 2065875284, 3024830291, 3516001110, 2065367968, 527997395, 699989490, 2122121007, 2474587956, 4264263019, 3108597240, 3716893556, 355206280, 4104366162, 1589387964, 991634136, 3258618709, 1939879522, 3943776049, 2310155274, 3394254311, 3058600296, 1694289090, 1350080016, 798367306, 4198928713, 3542558036, 3811717929, 438169574, 1624691559, 260745736, 1652629032, 1652265240, 713870369, 2868834775, 1451803791, 832979628, 2445086111, 3115520481, 1233633660, 2193585414, 2356264214, 1433628448, 1473526895, 1383053461, 3742702439, 309498954, 1927510607, 901502325, 2451078913, 1870117541, 1391022224, 1352089645, 2199761074, 3512828386, 4212129343, 3604566834, 2594348588, 3304255545, 2979465200, 3146868888, 2093601261, 601219291, 3154375867, 340827160, 138986795, 1611448895, 1830093050, 2356172326, 982079876, 2288335605, 1113125225, 3677868545, 3466453176, 1361204694, 159400349, 2677765674, 605714313, 3978227473, 3910292465, 4269661722, 159477487, 2665247188, 735150667, 789962098, 3892613157, 3630297165, 1387960633, 3483225739, 1209851284, 2356254422, 3386099698, 2037875474, 3763486769, 1984654262, 2221621758, 724779491, 3819447583, 1271605392, 4265058615, 43704700, 1906590300, 3537098683, 4190771444, 1779648299, 930942468, 2150555788, 3213078468, 2910886634, 3148695195, 3052957441, 573212002, 2258351227, 2913384306, 1632377868, 455885593, 4246220245, 2311233557, 3750072750, 3228307058, 825875749, 3000233838, 1746868973, 1168865672, 3348311454, 244319473, 2812813011, 22996684, 1635003376, 2392205837, 1382560930, 139888668, 2705243006, 3074408187, 2828550378, 3542558275, 43575779, 3385210032, 4091335036, 4182436998, 4159234675, 1208316006, 1844246200, 2535141270, 1101221750, 2367125474, 3529762512, 3563098040, 1459017302, 3304256957, 2367191695, 796560594, 958022359, 1170073188, 3897246010, 3560902677, 3478910850, 271093843, 547051433, 2236278945, 1539185203, 1880996255, 1425516773, 3056780989, 2836182936, 1798746482, 1333812486, 621213866, 2029554771, 4133584493, 1382812499, 3361468537, 3109562757, 3227199462, 3302729769, 1918794384, 1756969763, 1124566920, 1788140616, 2707459645, 4010666223, 2849734079, 4095286962, 1423376786, 2986606412, 3431489129, 565983202, 296383102, 3730050922, 242937688, 1386136462, 2070118049, 2700155719, 780737552, 3834461315, 1234973494, 3639979156, 281512671, 686339895, 3117180650, 3631078139, 3136793149, 2224183609, 1553157158, 495427809, 1440134354, 1897669267, 1822898799, 3667308203, 3550373038, 1806618457, 824096499, 916518988, 1375402600, 489583658, 34986332, 832626016, 1057095869, 399133961, 98139021, 4132711966, 2474824075, 2141450054, 806198962, 3553179879, 3810740226, 140069611, 3032816587, 3858261923, 226975308, 309794698, 1849459980, 774524394, 2384353573, 2605943766, 2789989015, 3487277111, 409038223, 3972173437, 1183545483, 3833899189, 868290187, 2129739952, 2735405174, 3578844194, 199772641, 476305441, 1945207422, 1047871028, 1772768521, 3259156476, 488663387, 3740319219, 3047318117, 3226812043, 2563434464, 2461664186, 2239949802, 1066519062, 994547509, 3869272, 3626920295, 1614634858, 3025445964, 485655704, 4187634557, 1059086460, 1391515319, 584093806, 352238135, 1466597256, 3319545112, 3548364901, 190114565, 3928573893, 3750021476, 3713931069, 1783831176, 3255583976, 2096470220, 4012412121, 524861224, 3854831713, 214360217, 1847763838, 3780377675, 2374201239, 942925124, 2063219874, 3346760477, 2467139008, 1125799132, 1094890806, 533518308, 2301805316, 558520212, 1628896753, 2966209252, 3214760426, 4213109246, 12663941, 4199851087, 2539889474, 946250346, 2704244124, 3411492275, 3490381440, 1825684, 1653354648, 1747579554, 3701169410, 1727418872, 429020278, 3450866749, 465004734, 1861867216, 1866874331, 3269910787, 3871402315, 2683416023, 2549787181, 761291181, 2948598568, 1666041973, 2755207629, 617441615, 122867073, 2122181977, 3584318163, 1004458475, 2765029722, 3999002898, 1633545143, 1912842402, 1843318491, 2814139584, 2201700300, 1618905503, 1942976337, 3656922810, 526725357, 3701480012, 996428986, 431055458, 2872103545, 3668284300, 826763806, 3587639843, 3652337444, 1289805244, 3762961529, 39101766, 1521828428, 1896940177, 3652712873, 339425432, 71528270, 3243575421, 994706306, 2607244426, 832707414, 1875448807, 2776470608, 4237012680, 183296027, 1766117880, 3589481863, 3091583589, 1379593361, 945065049, 3938551910, 2006427855, 892858433, 2035054014, 2134824116, 2324044766, 114634662, 2642336105, 3646394982, 1401013143, 1380120844, 3903196376, 824047721, 1629644109, 1970315500, 4040512079, 1128691294, 1106628811, 3246874665, 228991640, 3042266555, 994697508, 1978908948, 6659385, 2379273963, 317913210, 4125798411, 2968397034, 2847561279, 917989675, 768034685, 3914209855, 323687538, 126724290, 3243338145, 1211515296, 3025336532, 1607338720, 3751097049, 3041455699, 841715105, 2105657049, 466048154, 1077950036, 1425994784, 1181560686, 3819897593, 2944658783, 17704962, 4189717388, 300850250, 379234294, 3316845498, 588433034, 1334035997, 1405006497, 1777187009, 2148083835, 3474559801, 4256439249, 15352263, 2331132546, 2984307187, 847283779, 440435752, 134034903, 3177939388, 2429067152, 394863738, 182610078, 1268353682, 3122459933, 3342181794, 3533338887, 2486559530, 1989024990, 2947911781, 1333161597, 3698190411, 1287530203, 1575871295, 1230688809, 3016327265, 2969566583, 1918621877, 663201141, 3217233565, 865654647, 3725662001, 562493874, 3532567360, 3055374410, 196874216, 570726472, 2697994287, 2971268675, 3698284644, 2172308107, 167078127, 3271576499, 3017836962, 3680715643, 3057619064, 3396693870, 4112478828, 1581249777, 1681796512, 2250344043, 3241122766, 2547918020, 144619826, 2342225903, 2649173592, 1274740361, 2577013794, 188619481, 2899313883, 3376424600, 2113243967, 1662241018, 3390798238, 1539159943, 2054257230, 3321793510, 2134818401, 478547292, 1937269648, 2176201861, 1232911078, 1730672703, 517486047, 648971569, 366218610, 1742763735, 2803068129, 4164450963, 3641163288, 1969974907, 3726459121, 2146936566, 668283836, 1141288765, 1781560955, 2847764909, 1123447968, 2195479446, 4227370755, 247087828, 3556321491, 4027490605, 1392301047, 2139104139, 2837763224, 4256764867, 1366461607, 73316475, 2533963066, 2638526465, 47518757