Skip to content

Instantly share code, notes, and snippets.

@zabacad
Last active November 6, 2019 04:13
Show Gist options
  • Save zabacad/b075fe083b089f3e1aee178ac4a8a828 to your computer and use it in GitHub Desktop.
Save zabacad/b075fe083b089f3e1aee178ac4a8a828 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define SHA1_BLOCK 64
int main(int argc, char *argv[])
{
int a, b, c, d, e;
int h0, h1, h2, h3, h4;
int i;
int file_handle;
unsigned char *sha_data;
int *w;
int got;
printf("Hello, world!\n");
if (argc < 2)
{
fprintf(stderr, "Wrong number of arguments.\n");
return 1;
}
sha_data = (unsigned char *)malloc(SHA1_BLOCK);
if (sha_data == NULL)
{
fprintf(stderr, "Failed to allocate memory for SHA data.\n");
return 1;
}
file_handle = open(argv[1], O_RDONLY);
if (file_handle < 0)
{
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
return 1;
}
if (read(file_handle, sha_data, SHA1_BLOCK) != SHA1_BLOCK)
{
fprintf(stderr, "Could not read %dB from file: %s\n", SHA1_BLOCK, strerror(errno));
return 1;
}
close(file_handle);
w = (int *)malloc(80 * sizeof(int));
if (w == NULL)
{
fprintf(stderr, "Failed to allocate memory for SHA words.\n");
return 1;
}
for (i = 0; i < 16; i++)
{
/* Convert big-endian to native */
w[i] = sha_data[i * 4 + 3] * 16777216 + \
sha_data[i * 4 + 2] * 65536 + \
sha_data[i * 4 + 1] * 256 + \
sha_data[i * 4];
}
for (i = 16; i < 80; i++)
{
w[i] = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]; /*TODO left rotate 1 whole thing*/
}
for (i = 0; i < 80; i++)
{
printf("w[%02d] = 0x%08x", i, w[i]);
((i + 1) % 4) ? printf(" ") : printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment