mirror of
https://github.com/rvtr/GodMode9i.git
synced 2025-11-02 00:11:07 -04:00
Implement calculating SHA1 hash of files (#86)
* implement calculating SHA1 hash of a file * fix bug where incorrect SHA1 hash would be calculated
This commit is contained in:
parent
faabd8305d
commit
9e00e5394e
@ -9,6 +9,7 @@
|
||||
#include "file_browse.h"
|
||||
|
||||
#define copyBufSize 0x8000
|
||||
#define shaChunkSize 0x10000
|
||||
|
||||
u32 copyBuf[copyBufSize];
|
||||
|
||||
@ -68,6 +69,39 @@ off_t getFileSize(const char *fileName)
|
||||
return fsize;
|
||||
}
|
||||
|
||||
bool calculateSHA1(const char *fileName, u8 *sha1)
|
||||
{
|
||||
off_t fsize = getFileSize(fileName);
|
||||
u8 *buf = (u8*) malloc(shaChunkSize);
|
||||
if (!buf) {
|
||||
iprintf("Could not allocate buffer\n");
|
||||
return false;
|
||||
}
|
||||
FILE* fp = fopen(fileName, "rb");
|
||||
if (!fp) {
|
||||
iprintf("Could not open file for reading\n");
|
||||
free(buf);
|
||||
return false;
|
||||
}
|
||||
memset(sha1, 0, 20);
|
||||
swiSHA1context_t ctx;
|
||||
ctx.sha_block=0; //this is weird but it has to be done
|
||||
swiSHA1Init(&ctx);
|
||||
while (true) {
|
||||
size_t ret = fread(buf, 1, shaChunkSize, fp);
|
||||
if (!ret) break;
|
||||
swiSHA1Update(&ctx, buf, ret);
|
||||
scanKeys();
|
||||
int keys = keysHeld();
|
||||
if (keys & KEY_START) return false;
|
||||
iprintf("\x1b[1;A");
|
||||
iprintf("%ld/%lld bytes\n", ftell(fp), fsize);
|
||||
}
|
||||
swiSHA1Final(sha1, &ctx);
|
||||
free(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
void dirCopy(DirEntry* entry, int i, const char *destinationPath, const char *sourcePath) {
|
||||
std::vector<DirEntry> dirContents;
|
||||
dirContents.clear();
|
||||
|
||||
@ -23,6 +23,7 @@ extern void printBytes(int bytes);
|
||||
extern void printBytesAlign(int bytes);
|
||||
|
||||
extern off_t getFileSize(const char *fileName);
|
||||
extern bool calculateSHA1(const char *fileName, u8 *sha1);
|
||||
extern int fcopy(const char *sourcePath, const char *destinationPath);
|
||||
void changeFileAttribs(DirEntry* entry);
|
||||
|
||||
|
||||
@ -242,8 +242,12 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
|
||||
assignedOp[++maxCursors] = FileOperation::copyFatOut;
|
||||
printf(" Copy to fat:/gm9i/out\n");
|
||||
}
|
||||
printf("\n");
|
||||
printf("(<A> select, <B> cancel)");
|
||||
// The bios SHA1 functions are only availible on the DSi
|
||||
// https://problemkaputt.de/gbatek.htm#biossha1functionsdsionly
|
||||
if (isDSiMode()) {
|
||||
assignedOp[++maxCursors] = FileOperation::calculateSHA1;
|
||||
printf(" Calculate SHA1 hash\n");
|
||||
}
|
||||
consoleSelect(&bottomConsole);
|
||||
printf ("\x1B[47m"); // Print foreground white color
|
||||
while (true) {
|
||||
@ -375,6 +379,29 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
|
||||
currentDrive = 6;
|
||||
}
|
||||
break;
|
||||
} case FileOperation::calculateSHA1: {
|
||||
iprintf("\x1b[2J");
|
||||
iprintf("Calculating SHA1 hash of:\n%s\n", entry->name.c_str());
|
||||
iprintf("Press <START> to cancel\n\n");
|
||||
u8 sha1[20] = {0};
|
||||
bool ret = calculateSHA1(strcat(getcwd(path, PATH_MAX), entry->name.c_str()), sha1);
|
||||
if (!ret) break;
|
||||
iprintf("SHA1 hash is: ");
|
||||
for (int i = 0; i < 19; ++i) iprintf("%02X", sha1[i]);
|
||||
consoleSelect(&topConsole);
|
||||
iprintf ("\x1B[30m"); // Print black color
|
||||
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
|
||||
int pressed;
|
||||
do {
|
||||
// Move to right side of screen
|
||||
iprintf ("\x1b[0;26H");
|
||||
// Print time
|
||||
iprintf (" %s" ,RetTime().c_str());
|
||||
scanKeys();
|
||||
pressed = keysDownRepeat();
|
||||
swiWaitForVBlank();
|
||||
} while (!(pressed & (KEY_A | KEY_Y | KEY_B | KEY_X)));
|
||||
break;
|
||||
} case FileOperation::none: {
|
||||
break;
|
||||
}
|
||||
|
||||
@ -43,6 +43,7 @@ enum class FileOperation {
|
||||
showInfo,
|
||||
copySdOut,
|
||||
copyFatOut,
|
||||
calculateSHA1,
|
||||
};
|
||||
|
||||
bool extension(const std::string &filename, const std::vector<std::string> &extensions);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user