2³² Addition
2 min readMay 13, 2021
Yes! I can use triple backticks
Sample 2000000000 + 3000000000 = 705032704
Started sha256 implementation. Felt really dumb because my code is redundant.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct shaContents *SHA256;struct shaContents
{
int *hashWordH;
int *messageScheduleW;
int *temporaryVariables;
};void CharToBinHelper(char character, int *messageHolder, int startIndex)
{
int index = startIndex;
int number = character;
while(number > 0)
{
int remainder = number % 2;
number /= 2;
//printf("(%d %d)", remainder, index-1);
messageHolder[index-1] = remainder;
index--;
}
}void StringToBin(char *message, int messageLength, int *messageHolder)
{
int startIndex = 1;
for(int i = 0; i < messageLength; i++)
{
CharToBinHelper(message[i],messageHolder, startIndex * 8);
startIndex++;
}
}void DecimalToBinary(int *binHolder, unsigned int number)
{
int index = 63;
while(number > 0)
{
int remainder = number % 2;
number /= 2;
binHolder[index] = remainder;
index--;
}
}void messageProcessor(char *initialMessage, int initialLength)
{
int processed[512] = {0};
int currentBits = initialLength;
processed[initialLength] = 1;
currentBits++;
int numberLength[64] = {0};
if(currentBits > 512)
{
}
else
{
int paddingZeros = 448 - currentBits;
DecimalToBinary(numberLength, paddingZeros);
for(int i = 0; i < 63; i++)
printf("%d", numberLength[i]);
}
}SHA256 InitializeShaContents()
{
SHA256 sha256;
sha256 = malloc(sizeof(*sha256));
sha256->hashWordH = malloc(sizeof(int) * 8);
sha256->messageScheduleW = calloc(64, sizeof(int));
sha256->temporaryVariables = calloc(2, sizeof(int));
sha256->hashWordH[0] = 0x6a09e667;
sha256->hashWordH[1] = 0xbb67ae85;
sha256->hashWordH[2] = 0x3c6ef372;
sha256->hashWordH[3] = 0xa54ff53a;
sha256->hashWordH[4] = 0x510e527f;
sha256->hashWordH[5] = 0x9605688c;
sha256->hashWordH[6] = 0x1f83d9ab;
sha256->hashWordH[7] = 0x5be0cd19;return sha256;
}void DestroyHash(SHA256 sha256)
{
free(sha256->hashWordH);
free(sha256->messageScheduleW);
free(sha256->temporaryVariables);
free(sha256);
}int main()
{
char *string = "abc";
int mess[24] = {0};
StringToBin(string, 3, mess);
for(int i = 0; i < 24; i++)
printf("%d", mess[i]);
//int message[64] = {0};
return 0;
}
Simple addition
//gcc mod32.c -o mod32.o && ./mod32.oint Mod232Addition(int one, int two)
{
return(one+two);
}int main()
{
printf("%d\n", Mod232Addition(2000000000, 3000000000));
return 0;
}