Skip to content
Snippets Groups Projects
Commit fea82107 authored by Qifan Tang's avatar Qifan Tang
Browse files

late sub

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #3047 canceled
crack: crack.c sha256.o
gcc -Wall crack.c sha256.o -o crack
sha256.o: sha256.c sha256.h
gcc -c sha256.c
This diff is collapsed.
111111
\ No newline at end of file
crack 0 → 100755
File added
crack.c 0 → 100644
/*************************** HEADER FILES ***************************/
#include <memory.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include "sha256.h"
#define FILE4 "pwd4sha256"
#define FILE6 "pwd6sha256"
#define FILEWORD "common_passwords.txt"
#define MIN 32
#define MAX 127
#define TOTAL 95
#define MAXWORD 12
void hashWord(char * text, BYTE *hashed);
long int fileSize(const char *fileName);
int checkHashed(BYTE * hashed1, BYTE * hashed2);
void hashcpy(BYTE * hashed1, BYTE * hashed2);
void checkWord(char * word, BYTE ** hashed, int pwds);
char *repSub(char *str1, char *str2, char *str3);
int fileguess(BYTE ** hashed, int pwds,int count);
int stupidguess(BYTE ** hashed, int pwds, int count);
int getHashed(char * file, BYTE ** hashed, int lines);
void oneargc();
void twoargc(int count);
void threeargc(char * passwordfile, char * hashedfile);
//convert the plain text to hashed Bytes
void hashWord(char * text, BYTE *hashed){
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_fill(&ctx, text, strlen(text));
sha256_final(&ctx, hashed);
}
//get the size of the file
long int fileSize(const char *fileName){
struct stat st;
if(stat(fileName,&st)==0){
return (st.st_size);
}
return 0;
}
//check two hashed if same return 1, if not return 0
int checkHashed(BYTE * hashed1, BYTE * hashed2){
for(int i = 0; i < SHA256_BLOCK_SIZE; i++){
if(hashed1[i] != hashed2[i]){
return 0;
}
}
return 1;
}
//copying the hashed
void hashcpy(BYTE * hashed1, BYTE * hashed2){
for(int i = 0; i < SHA256_BLOCK_SIZE; i++){
hashed1[i] = hashed2[i];
}
}
//check the word
void checkWord(char * word, BYTE ** hashed, int pwds){
BYTE hash[SHA256_BLOCK_SIZE];
//hash the word
hashWord(word, hash);
//printf("%s\n", word);
//check through the hashed
for(int i = 0; i < pwds; i++){
if(checkHashed(hash, hashed[i])){
printf("%s %d\n", word, i+1);
}
}
}
//this function replace the substring with str3
char *repSub(char *str1, char *str2, char *str3) {
char *result;
char *ins;
char *tmp;
int len_str2 = strlen(str2);
int len_str3 = strlen(str3);
int len_front;
int count;
if (!str1 || !str2)
return NULL;
if (len_str2 == 0)
return NULL;
//if str3 is empty
if (!str3)
str3 = "";
ins = str1;
for (count = 0; tmp = strstr(ins, str2); ++count) {
ins = tmp + len_str2;
}
tmp = result = malloc(strlen(str1) + (len_str3 - len_str2) * count + 1);
if (!result)
return NULL;
while (count--) {
ins = strstr(str1, str2);
len_front = ins - str1;
tmp = strncpy(tmp, str1, len_front) + len_front;
tmp = strcpy(tmp, str3) + len_str3;
str1 += len_front + len_str2;
}
strcpy(tmp, str1);
return result;
}
//add one character
int addOne(BYTE ** hashed, int pwds, int count, char * word){
//printf("addOne\n");
//add one number
char buff[20];
char temp [MAXWORD] = {'\0'};
strcpy(temp, word);
for (int i = 0; i < 10; i++){
if (count == 0){
break;
}
snprintf (buff, sizeof(buff), "%d",i);
strcpy(temp,word);
strcat(temp, buff);
checkWord(temp, hashed, pwds);
count--;
}
return count;
}
//remove one character
int removeOne(BYTE ** hashed, int pwds, int count, char * word){
//printf("removeOne");
char temp [MAXWORD] = {'\0'};
//take out the last character
if (count != 0){
strncpy(temp,word,strlen(word)-1);
checkWord(temp, hashed, pwds);
count--;
}
return count;
}
//try remove the last two because so many end with er or ar
int removeTwo(BYTE ** hashed, int pwds, int count, char *word){
//printf("removeTwo");
char temp [MAXWORD] = {'\0'};
//take out the last two character
if (count != 0){
strncpy(temp,word,strlen(word)-2);
checkWord(temp, hashed, pwds);
count--;
}
return count;
}
//check for birthday only for pwd4
int checkBday(BYTE ** hashed, int pwds, int count){
//check year
//add one number
char buff[20];
char temp [MAXWORD] = {'\0'};
for (int year = 1900; year < 2020; year++){
if (count == 0){
break;
}
snprintf (buff, sizeof(buff), "%d",year);
strcat(temp, buff);
checkWord(temp, hashed, pwds);
bzero(temp,strlen(temp));
count--;
}
//check date
for(int month = 0; month < 13; month++){
if (count == 0){
break;
}
for(int date = 0; date< 32; date++){
if(count ==0){
break;
}
if(month < 10){
if(date < 10){
snprintf (buff, sizeof(buff), "%d%d%d%d",0,month,0,date);
}else{
snprintf (buff, sizeof(buff), "%d%d%d",0,month,date);
}
}else{
if(date < 10){
snprintf (buff, sizeof(buff), "%d%d%d",month,0,date);
}else{
snprintf (buff, sizeof(buff), "%d%d",month,date);
}
}
strcat(temp, buff);
checkWord(temp, hashed, pwds);
bzero(temp,strlen(temp));
count--;
}
}
return count;
}
//check special numbers
int specialNumber(BYTE ** hashed, int pwds, int count){
char buff[20];
char temp [MAXWORD] = {'\0'};
for (int i = 0; i < 10; i++){
if (count == 0){
break;
}
snprintf (buff, sizeof(buff), "%d%d%d%d",i,i,i,i);
strcat(temp, buff);
checkWord(temp, hashed, pwds);
bzero(temp,strlen(temp));
count--;
}
for (int i = 0; i < 10; i++){
if (count == 0){
break;
}
snprintf (buff, sizeof(buff), "%d%d%d%d%d%d",i,i,i,i,i,i);
strcat(temp, buff);
checkWord(temp, hashed, pwds);
bzero(temp,strlen(temp));
count--;
}
return count;
}
//check integers first
int checkInt(BYTE ** hashed, int pwds, int count){
char buff[20];
char temp [MAXWORD] = {'\0'};
//4 pwds
for (int i = 0; i < 10; i ++){
if(count == 0){
break;
}
for(int j = 0; j < 10; j++){
if(count == 0){
break;
}
for(int k = 0; k < 10; k++){
if(count == 0){
break;
}
for(int m = 0; m < 10; m++){
if(count == 0){
break;
}
snprintf(buff,sizeof(buff),"%d%d%d%d",i,j,k,m);
strcat(temp,buff);
checkWord(temp,hashed,pwds);
bzero(temp,strlen(temp));
count--;
}
}
}
}
//6 pwds
for (int i = 0; i < 10; i ++){
if(count == 0){
break;
}
for(int j = 0; j < 10; j++){
if(count == 0){
break;
}
for(int k = 0; k < 10; k++){
if(count == 0){
break;
}
for(int m = 0; m < 10; m++){
if(count == 0){
break;
}
for(int n = 0; n < 10; n++){
if (count == 0){
break;
}
for (int o = 0; o < 10; o++){
if (count == 0){
break;
}
snprintf(buff,sizeof(buff),"%d%d%d%d%d%d",i,j,k,m,n,o);
strcat(temp,buff);
checkWord(temp,hashed,pwds);
bzero(temp,strlen(temp));
count--;
}
}
}
}
}
}
return count;
}
//check if there is a substring
int checkSub(BYTE ** hashed, int pwds, int count, char * word, char * sub, char * rep){
char temp [MAXWORD] = {'\0'};
if (strstr(word, sub)){
strcpy(temp,repSub(word, sub, rep));
count = tryFitting(hashed, pwds, count, temp);
}
return count;
}
//considering the first letter capital or all capital case
int tryCapital(BYTE ** hashed, int pwds, int count, char * word){
char temp [MAXWORD] = {'\0'};
//try capital the first letter
if(count!=0){
if(word[0] >= 'a' && word[0] <= 'z'){
temp[0] = word[0] -32;
strcat(temp,&word[1]);
}else if(word[0] >= 'A' && word[0] <= 'Z'){
temp[0] = word[0] + 32;
strcat(temp,&word[1]);
}
count = tryFitting(hashed, pwds, count, temp);
count = tryReplace(hashed, pwds, count, temp);
}
bzero(temp,strlen(temp));
//try capital last letter
if(count != 0){
int index = strlen(word)-1;
char temp[MAXWORD] = {'\0'};
if(word[index] >= 'a' && word[index] <= 'z'){
strncpy(temp,word, index);
temp[index] = word[index] - 32;
}else if(word[index] >= 'A' && word[index] <= 'Z'){
strncpy(temp,word, index);
temp[index] = word[index] + 32;
}
count = tryFitting(hashed, pwds, count, temp);
count = tryReplace(hashed, pwds, count, temp);
}
//all capital
if(count!=0){
for(int i = 0; i < strlen(word); i++){
if(word[i] >= 'a' && word[i] <= 'z'){
temp[i] = word[i] - 32;
}else{
temp[i] = word[i];
}
}
count = tryFitting(hashed, pwds, count, temp);
count = tryReplace(hashed,pwds,count,temp);
}
bzero(temp,strlen(temp));
//all lower
if(count!=0){
for(int i = 0; i < strlen(word); i++){
if(word[i] >= 'A' && word[i] <= 'Z'){
temp[i] = word[i] + 32;
}else{
temp[i] = word[i];
}
}
count = tryFitting(hashed, pwds, count, temp);
count = tryReplace(hashed,pwds,count,temp);
}
return count;
}
//special replacement
int tryReplace(BYTE ** hashed, int pwds, int count, char * word){
//printf("tryReplace\n");
count = checkSub(hashed, pwds, count, word, "two", "2");
count = checkSub(hashed, pwds, count, word, "for", "4");
count = checkSub(hashed, pwds, count, word, "are", "r");
count = checkSub(hashed, pwds, count, word, "you", "u");
count = checkSub(hashed, pwds, count, word, "four", "4");
count = checkSub(hashed, pwds, count, word, "six", "6");
count = checkSub(hashed, pwds, count, word, "sev", "7");
count = checkSub(hashed, pwds, count, word, "one", "1");
count = checkSub(hashed, pwds, count, word, "ate", "8");
count = checkSub(hashed, pwds, count, word, "at", "@");
count = checkSub(hashed, pwds, count, word, "@", "at");
count = checkSub(hashed, pwds, count, word, "please", "plz");
count = checkSub(hashed, pwds, count, word, "your", "yr");
count = checkSub(hashed, pwds, count, word, "yr", "ur");
count = checkSub(hashed, pwds, count, word, "ur", "yr");
count = checkSub(hashed, pwds, count, word, "gud", "good");
count = checkSub(hashed, pwds, count, word, "good", "gud");
count = checkSub(hashed, pwds, count, word, "to", "2");
count = checkSub(hashed, pwds, count, word, "too", "2");
count = checkSub(hashed, pwds, count, word, "see", "c");
count = checkSub(hashed, pwds, count, word, "wait", "w8");
count = checkSub(hashed, pwds, count, word, "before", "b4");
count = checkSub(hashed, pwds, count, word, "t", "7");
count = checkSub(hashed, pwds, count, word, "T", "7");
count = checkSub(hashed, pwds, count, word, "7", "T");
count = checkSub(hashed, pwds, count, word, "7", "t");
return count;
}
//try to fit the word into 4 or 6
int tryFitting(BYTE ** hashed, int pwds, int count, char * word){
//printf("tryFitting\n");
if(count!=0){
if (strlen(word) == 4){
checkWord(word,hashed,pwds);
count--;
}else if(strlen(word)==6){
checkWord(word,hashed,pwds);
count--;
count = removeTwo(hashed, pwds, count, word);
}else if(strlen(word) == 3){
count = addOne(hashed, pwds, count, word);
}else if(strlen(word) == 5){
count = removeOne(hashed, pwds, count, word);
count = addOne(hashed, pwds, count, word);
}else if(strlen(word) == 7){
count = removeOne(hashed, pwds, count, word);
}else if(strlen(word) == 8){
count = removeTwo(hashed, pwds, count, word);
}
}
return count;
}
//guess from the file with common words provided
int fileguess(BYTE ** hashed, int pwds,int count){
//printf("fileguess");
FILE *fp = fopen (FILEWORD, "r" );
if (fp != NULL) {
char word [MAXWORD] = {'\0'};
while (fgets(word, sizeof(word), fp ) != NULL )
{
if(count == 0){
break;
}
//take the last new line
strtok(word, "\n");
count = tryFitting(hashed, pwds, count, word);
count = tryReplace(hashed, pwds, count, word);
count = tryCapital(hashed, pwds, count, word);
bzero(word, strlen(word));
}
//close the file
fclose(fp);
}
return count;
}
//guess one by one
int stupidguess(BYTE ** hashed, int pwds, int count){
char word [MAXWORD] = {'\0'};
//4 digits
for(int i = MIN; i < MAX; i++){
for (int j = MIN; j < MAX; j++){
for (int k = MIN; k < MAX; k++){
for (int m = MIN; m < MAX; m++){
if(count == 0){
break;
}
word[0] = (char) i;
word[1] = (char) j;
word[2] = (char) k;
word[3] = (char) m;
checkWord(word, hashed, pwds);
count--;
}
}
}
}
//6 digits
char word6 [MAXWORD] = {'\0'};//6 digits
for(int i = MIN; i < MAX; i++){
for (int j = MIN; j < MAX; j++){
for (int k = MIN; k < MAX; k++){
for (int m = MIN; m < MAX; m++){
for (int n = MIN; n < MAX; n++){
for ( int o = MIN; o < MAX; o++){
if (count == 0){
break;
}
word6[0] = (char) i;
word6[1] = (char) j;
word6[2] = (char) k;
word6[3] = (char) m;
word6[4] = (char) n;
word6[5] = (char) o;
checkWord(word6, hashed, pwds);
count--;
}
}
}
}
}
}
return count;
}
//gets the hashed Byte from the file given and store
int getHashed(char * file, BYTE ** hashed, int lines) {
FILE * fp = fopen(file, "rb");
BYTE line[SHA256_BLOCK_SIZE];
int rd;
if(fp != 0){
while((rd = fread(line, 1, SHA256_BLOCK_SIZE, fp)) > 0){
hashcpy(hashed[lines], line);
lines++;
bzero(line, SHA256_BLOCK_SIZE);
}
}
fclose(fp);
return lines;
}
//if there are only one input
void oneargc(){
//printf("oneargc\n");
int count = -1;
//calculate the number of passwords
int pwds = fileSize(FILE4)/SHA256_BLOCK_SIZE + fileSize(FILE6)/SHA256_BLOCK_SIZE;
// get memory for the hashed
BYTE ** hashed = (BYTE **)malloc(sizeof(BYTE *) * pwds);
for(int i = 0; i < pwds; i++) {
hashed[i] = (BYTE *)malloc(sizeof(BYTE *)* SHA256_BLOCK_SIZE);
}
//get the hashed from files
int lines = 0;
lines = getHashed(FILE4, hashed, lines);
lines = getHashed(FILE6, hashed, lines);
//run the file guess first based on the commonly used words
count = fileguess(hashed, pwds,count);
//check for special numbers
count = specialNumber(hashed, pwds, count);
//check for bday
count = checkBday(hashed,pwds,count);
//test integers first
count = checkInt(hashed, pwds, count);
//for the rest, run the stupid guess, takes long time
count = stupidguess(hashed, pwds,count);
// free all
for (int i = 0; i < pwds; i++){
free(hashed[i]);
}
free(hashed);
}
//if there are two inputs
void twoargc(int count){
//calculate the number of passwords
int pwds = fileSize(FILE4)/SHA256_BLOCK_SIZE + fileSize(FILE6)/SHA256_BLOCK_SIZE;
// get memory for the hashed
BYTE ** hashed = (BYTE **)malloc(sizeof(BYTE *) * pwds);
for(int i = 0; i < pwds; i++) {
hashed[i] = (BYTE *)malloc(sizeof(BYTE *)* SHA256_BLOCK_SIZE);
}
//get the hashed from files
int lines = 0;
lines = getHashed(FILE4, hashed, lines);
lines = getHashed(FILE6, hashed, lines);
//run the file guess first based on the commonly used words
count = fileguess(hashed, pwds,count);
//check for special numbers
count = specialNumber(hashed, pwds, count);
//check for bday
count = checkBday(hashed,pwds,count);
//test integers first
count = checkInt(hashed, pwds, count);
//for the rest, run the stupid guess, takes long time
count = stupidguess(hashed, pwds,count);
// free all
for (int i = 0; i < pwds; i++){
free(hashed[i]);
}
free(hashed);
}
//if there are three inputs
void threeargc(char * passwordfile, char * hashedfile){
int pwds = fileSize(hashedfile)/SHA256_BLOCK_SIZE;
//allocate memmory for the hashed
BYTE ** hashed = (BYTE**) malloc (sizeof(BYTE*) * pwds);
for (int i = 0; i < pwds; i ++){
hashed[i] = (BYTE *)malloc (sizeof(BYTE) * SHA256_BLOCK_SIZE);
}
//read the hashed file
int lines = 0;
lines = getHashed(hashedfile, hashed, lines);
//check the passwords
FILE * fp = fopen(passwordfile, "rb");
if( fp != NULL){
char word[12] = {'\0'};
//whenever there is a line
while(fgets(word, MAXWORD, fp) != NULL){
//take the new line
strtok(word,"\n");
checkWord(word, hashed, pwds);
//set to nothing
bzero(word, strlen(word));
}
}
}
int main(int argc, char* argv[]){
if(argc == 1){
oneargc();
}else if(argc == 2){
int count = atoi(argv[1]);
twoargc(count);
}else if(argc == 3){
char * filename1 = argv[1];
char * filename2 = argv[2];
threeargc(filename1, filename2);
}
return 0;
}
File added
File added
sha256.c 0 → 100644
/*********************************************************************
* Filename: sha256.c
* Author: Brad Conte (brad AT bradconte.com)
* Copyright:
* Disclaimer: This code is presented "as is" without any guarantees.
* Details: Implementation of the SHA-256 hashing algorithm.
SHA-256 is one of the three algorithms in the SHA2
specification. The others, SHA-384 and SHA-512, are not
offered in this implementation.
Algorithm specification can be found here:
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
This implementation uses little endian byte order.
*********************************************************************/
/*************************** HEADER FILES ***************************/
#include <stdlib.h>
#include <memory.h>
#include "sha256.h"
/****************************** MACROS ******************************/
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
/**************************** VARIABLES *****************************/
static const WORD k[64] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
};
/*********************** FUNCTION DEFINITIONS ***********************/
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
{
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
for (i = 0, j = 0; i < 16; ++i, j += 4)
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
for ( ; i < 64; ++i)
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
a = ctx->state[0];
b = ctx->state[1];
c = ctx->state[2];
d = ctx->state[3];
e = ctx->state[4];
f = ctx->state[5];
g = ctx->state[6];
h = ctx->state[7];
for (i = 0; i < 64; ++i) {
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
t2 = EP0(a) + MAJ(a,b,c);
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
ctx->state[0] += a;
ctx->state[1] += b;
ctx->state[2] += c;
ctx->state[3] += d;
ctx->state[4] += e;
ctx->state[5] += f;
ctx->state[6] += g;
ctx->state[7] += h;
}
void sha256_init(SHA256_CTX *ctx)
{
ctx->datalen = 0;
ctx->bitlen = 0;
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
}
void sha256_fill(SHA256_CTX *ctx, char text[], size_t len)
{
WORD i;
BYTE dtext[strlen(text)];
for (int j = 0; j < strlen(text); j++){
dtext[j] = text[j];
}
const BYTE *data = dtext;
for (i = 0; i < len; ++i) {
ctx->data[ctx->datalen] = data[i];
ctx->datalen++;
if (ctx->datalen == 64) {
sha256_transform(ctx, ctx->data);
ctx->bitlen += 512;
ctx->datalen = 0;
}
}
}
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
{
WORD i;
i = ctx->datalen;
// Pad whatever data is left in the buffer.
if (ctx->datalen < 56) {
ctx->data[i++] = 0x80;
while (i < 56)
ctx->data[i++] = 0x00;
}
else {
ctx->data[i++] = 0x80;
while (i < 64)
ctx->data[i++] = 0x00;
sha256_transform(ctx, ctx->data);
memset(ctx->data, 0, 56);
}
// Append to the padding the total message's length in bits and transform.
ctx->bitlen += ctx->datalen * 8;
ctx->data[63] = ctx->bitlen;
ctx->data[62] = ctx->bitlen >> 8;
ctx->data[61] = ctx->bitlen >> 16;
ctx->data[60] = ctx->bitlen >> 24;
ctx->data[59] = ctx->bitlen >> 32;
ctx->data[58] = ctx->bitlen >> 40;
ctx->data[57] = ctx->bitlen >> 48;
ctx->data[56] = ctx->bitlen >> 56;
sha256_transform(ctx, ctx->data);
// Since this implementation uses little endian byte ordering and SHA uses big endian,
// reverse all the bytes when copying the final state to the output hash.
for (i = 0; i < 4; ++i) {
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
}
}
sha256.h 0 → 100644
/*********************************************************************
* Filename: sha256.h
* Author: Brad Conte (brad AT bradconte.com)
* Copyright:
* Disclaimer: This code is presented "as is" without any guarantees.
* Details: Defines the API for the corresponding SHA1 implementation.
*********************************************************************/
#ifndef SHA256_H
#define SHA256_H
/*************************** HEADER FILES ***************************/
#include <stddef.h>
/****************************** MACROS ******************************/
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
typedef struct {
BYTE data[64];
WORD datalen;
unsigned long long bitlen;
WORD state[8];
} SHA256_CTX;
/*********************** FUNCTION DECLARATIONS **********************/
void sha256_init(SHA256_CTX *ctx);
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
void sha256_fill(SHA256_CTX *ctx, char text[], size_t len);
void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
void printSha(SHA256_CTX *ctx);
#endif // SHA256_H
sha256.o 0 → 100644
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment