Skip to content

Instantly share code, notes, and snippets.

@dce
Last active December 13, 2015 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dce/4987033 to your computer and use it in GitHub Desktop.
Save dce/4987033 to your computer and use it in GitHub Desktop.
#include <sys/stat.h>
#include "otp.h"
#define WORD_LENGTH 32
#define BLOCK_SIZE 1024
// int fseek ( FILE * stream, long int offset, int origin );
// size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
// unsigned char *SHA256 ( const unsigned char *d, size_t n, unsigned char *md );
int
main(int argc, char *argv[])
{
int i;
char *filename,
*result;
FILE *file;
struct stat st;
long int offset,
iterations;
uint8_t *input,
*hash;
filename = "/Users/dce/Desktop/6 - 1 - Introduction (11 min).mp4";
file = fopen(filename, "rb");
stat(filename, &st);
iterations = st.st_size / BLOCK_SIZE;
offset = st.st_size % BLOCK_SIZE;
input = malloc(BLOCK_SIZE + WORD_LENGTH);
hash = malloc(WORD_LENGTH);
// Compute the initial hash
fseek(file, offset * -1, SEEK_END);
fread(input, 1, offset, file);
SHA256(input, offset, hash);
for (i = 0; i < iterations; i++) {
// Move back one block from end & read a block
fseek(file,
((i + 1) * BLOCK_SIZE + offset) * -1,
SEEK_END);
fread(input, 1, BLOCK_SIZE, file);
// Append previous result to block
memcpy(input + BLOCK_SIZE, hash, WORD_LENGTH);
// Hash block plus previous result
SHA256(input, BLOCK_SIZE + WORD_LENGTH, hash);
}
result = bytes_to_hex(hash, WORD_LENGTH);
printf("%s\n", result);
free(input);
free(hash);
free(result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment