Add sha1_calculate (takes a filename path)
This commit is contained in:
parent
7bfa01e2bc
commit
19304cf56a
46
hash.c
46
hash.c
|
@ -56,6 +56,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include <retro_miscellaneous.h>
|
#include <retro_miscellaneous.h>
|
||||||
#include <retro_endianness.h>
|
#include <retro_endianness.h>
|
||||||
|
@ -277,7 +278,7 @@ uint32_t crc32_calculate(const uint8_t *data, size_t length)
|
||||||
/* Define the circular shift macro */
|
/* Define the circular shift macro */
|
||||||
#define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF) | ((word) >> (32-(bits))))
|
#define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF) | ((word) >> (32-(bits))))
|
||||||
|
|
||||||
void SHA1Reset(SHA1Context *context)
|
static void SHA1Reset(SHA1Context *context)
|
||||||
{
|
{
|
||||||
context->Length_Low = 0;
|
context->Length_Low = 0;
|
||||||
context->Length_High = 0;
|
context->Length_High = 0;
|
||||||
|
@ -424,7 +425,7 @@ static void SHA1PadMessage(SHA1Context *context)
|
||||||
SHA1ProcessMessageBlock(context);
|
SHA1ProcessMessageBlock(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SHA1Result(SHA1Context *context)
|
static int SHA1Result(SHA1Context *context)
|
||||||
{
|
{
|
||||||
if (context->Corrupted)
|
if (context->Corrupted)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -438,7 +439,7 @@ int SHA1Result(SHA1Context *context)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHA1Input( SHA1Context *context,
|
static void SHA1Input( SHA1Context *context,
|
||||||
const unsigned char *message_array,
|
const unsigned char *message_array,
|
||||||
unsigned length)
|
unsigned length)
|
||||||
{
|
{
|
||||||
|
@ -474,3 +475,42 @@ void SHA1Input( SHA1Context *context,
|
||||||
message_array++;
|
message_array++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sha1_calculate(const char *path, char *result)
|
||||||
|
{
|
||||||
|
unsigned char buff[4096];
|
||||||
|
SHA1Context sha;
|
||||||
|
int rv = 1;
|
||||||
|
int fd = open(path, O_RDONLY);
|
||||||
|
|
||||||
|
if (fd < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
SHA1Reset(&sha);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
rv = read(fd, buff, 4096);
|
||||||
|
if (rv < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
SHA1Input(&sha, buff, rv);
|
||||||
|
}while(rv);
|
||||||
|
|
||||||
|
if (!SHA1Result(&sha))
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
sprintf(result, "%08X%08X%08X%08X%08X",
|
||||||
|
sha.Message_Digest[0],
|
||||||
|
sha.Message_Digest[1],
|
||||||
|
sha.Message_Digest[2],
|
||||||
|
sha.Message_Digest[3], sha.Message_Digest[4]);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
error:
|
||||||
|
if (fd >= 0)
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
4
hash.h
4
hash.h
|
@ -85,9 +85,7 @@ typedef struct SHA1Context
|
||||||
int Corrupted; /* Is the message digest corruped? */
|
int Corrupted; /* Is the message digest corruped? */
|
||||||
} SHA1Context;
|
} SHA1Context;
|
||||||
|
|
||||||
void SHA1Reset(SHA1Context *);
|
int sha1_calculate(const char *path, char *result);
|
||||||
int SHA1Result(SHA1Context *);
|
|
||||||
void SHA1Input( SHA1Context *, const unsigned char *, unsigned);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue