int calc_sha256 (void) { const int bufSize = 256; int bytesRead = 0; unsigned char hash[SHA256_DIGEST_LENGTH]; unsigned char* buffer; if (!raw_file) return -1; buffer = malloc(bufSize); if(!buffer) return -1; SHA256_CTX sha256; SHA256_Init(&sha256); while((bytesRead = fread(buffer, 1, bufSize, raw_file))) { SHA256_Update(&sha256, buffer, bytesRead); } SHA256_Final(hash, &sha256); sha256_hash_string(hash, calc_hash); free(buffer); fseek(raw_file, 0L, SEEK_SET); return 0; }
static int sha256_file (char *path, char outputBuffer[65]) { int bytesRead; unsigned char *buffer; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; FILE *file = fopen (path, "rb"); if (!file) { return -534; } SHA256_Init (&sha256); buffer = malloc (BUFSIZE); bytesRead = 0; if (!buffer) { return ENOMEM; } while ((bytesRead = fread (buffer, 1, BUFSIZE, file))) { SHA256_Update (&sha256, buffer, bytesRead); } SHA256_Final (hash, &sha256); sha256_hash_string (hash, outputBuffer); fclose (file); free (buffer); return 0; }
cell_t dgst_sha256(IPluginContext *pContext, const cell_t *params) { char *path; char buffer[PLATFORM_MAX_PATH]; // Get input file to calculate pContext->LocalToString(params[3], &path); unsigned char hash[SHA256_DIGEST_LENGTH]; char output[65]; // Open file and calculate hash smutils->BuildPath(Path_Game, buffer, PLATFORM_MAX_PATH, path); if(calc_sha256(buffer, hash) == -1) return -1; // Convert digest into human readable hex string sha256_hash_string(hash, output); // Set return buffer pContext->StringToLocalUTF8(params[1], params[2], output, NULL); return 0; }