### Complete Example: File Similarity Detection Source: https://context7.com/ssdeep-project/ssdeep/llms.txt A comprehensive example demonstrating buffer hashing, file hashing, modification detection, and similarity comparison. This example requires compilation with specific flags and links against the ssdeep library. ```c #include #include #include #include "fuzzy.h" #define SIZE 0x50000 // ~320KB int main(void) { // Allocate buffers unsigned char *original = malloc(SIZE); unsigned char *modified = malloc(SIZE); char *hash_original = malloc(FUZZY_MAX_RESULT); char *hash_modified = malloc(FUZZY_MAX_RESULT); char *hash_file = malloc(FUZZY_MAX_RESULT); if (!original || !modified || !hash_original || !hash_modified || !hash_file) { fprintf(stderr, "Memory allocation failed\n"); return EXIT_FAILURE; } // Generate test data srand(42); for (int i = 0; i < SIZE; i++) { original[i] = rand() % 256; } // Hash original buffer if (fuzzy_hash_buf(original, SIZE, hash_original) != 0) { fprintf(stderr, "Failed to hash original buffer\n"); return EXIT_FAILURE; } printf("Original hash: %s\n", hash_original); // Write to file and hash the file FILE *fp = fopen("test_data.bin", "wb"); fwrite(original, 1, SIZE, fp); fclose(fp); if (fuzzy_hash_filename("test_data.bin", hash_file) != 0) { fprintf(stderr, "Failed to hash file\n"); return EXIT_FAILURE; } printf("File hash: %s\n", hash_file); // Verify buffer and file hashes match int score = fuzzy_compare(hash_original, hash_file); printf("Buffer vs File: %d%% match\n\n", score); // Create modified version (change ~0.1% of data) memcpy(modified, original, SIZE); for (int i = 0x1000; i < 0x1100; i++) { modified[i] ^= 0xFF; // Flip all bits in this region } // Hash modified buffer if (fuzzy_hash_buf(modified, SIZE, hash_modified) != 0) { fprintf(stderr, "Failed to hash modified buffer\n"); return EXIT_FAILURE; } printf("Modified hash: %s\n", hash_modified); // Compare original and modified score = fuzzy_compare(hash_original, hash_modified); printf("Original vs Modified: %d%% match\n", score); if (score >= 90) { printf("Result: Files are nearly identical (minor changes detected)\n"); } else if (score >= 50) { printf("Result: Files share significant common content\n"); } else if (score > 0) { printf("Result: Files have some similarity\n"); } else { printf("Result: Files are completely different\n"); } // Cleanup free(original); free(modified); free(hash_original); free(hash_modified); free(hash_file); remove("test_data.bin"); return EXIT_SUCCESS; } // Compile: gcc -Wall -I/usr/local/include -L/usr/local/lib example.c -lfuzzy -o example // Output: // Original hash: 6144:abc123... // File hash: 6144:abc123... // Buffer vs File: 100% match // // Modified hash: 6144:abc124... // Original vs Modified: 97% match // Result: Files are nearly identical (minor changes detected) ``` -------------------------------- ### ssdeep Clustering Output Example Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Example output format for ssdeep clustering, showing identified clusters and the files belonging to each cluster. ```text # Output: # *** Cluster 1 *** # /path/to/variant1.exe # /path/to/variant2.exe # /path/to/variant3.exe ``` -------------------------------- ### ssdeep Comparison Output Example Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Example output format when comparing files, showing the paths of matched files and their similarity score. ```text # Output: # /path/to/file1.exe matches /path/to/file2.exe (97) ``` -------------------------------- ### ssdeep Output Format Example Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Illustrates the standard output format for ssdeep when hashing files, showing block size, hash components, and filename. ```text # Output: # ssdeep,1.1--blocksize:hash:hash,filename # 3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr9v,"document.pdf" ``` -------------------------------- ### ssdeep CSV Output Example Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Example of CSV formatted output when matching files against a known hash database, showing the unknown file, known file, and similarity score. ```text # Output (CSV mode): # "suspicious.exe","known_malware.exe",95 ``` -------------------------------- ### Stateful Hashing API for Large Files Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Use the stateful API (fuzzy_new, fuzzy_update, fuzzy_digest, fuzzy_free) for incremental hashing of large files or streaming data in chunks. This example demonstrates hashing a file chunk by chunk. ```c #include #include #include "fuzzy.h" #define CHUNK_SIZE 65536 int hash_large_file(const char *filename) { FILE *fp = fopen(filename, "rb"); if (!fp) return -1; // Create fuzzy state struct fuzzy_state *state = fuzzy_new(); if (!state) { fclose(fp); return -1; } // Optional: Set total length for optimization fseek(fp, 0, SEEK_END); long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); fuzzy_set_total_input_length(state, file_size); // Process file in chunks unsigned char buffer[CHUNK_SIZE]; size_t bytes_read; while ((bytes_read = fread(buffer, 1, CHUNK_SIZE, fp)) > 0) { if (fuzzy_update(state, buffer, bytes_read) != 0) { fprintf(stderr, "Error during update\n"); fuzzy_free(state); fclose(fp); return -1; } } fclose(fp); // Get final hash char result[FUZZY_MAX_RESULT]; int status = fuzzy_digest(state, result, 0); // Can call fuzzy_digest multiple times - state unchanged // Use FUZZY_FLAG_ELIMSEQ to eliminate repeated sequences // Use FUZZY_FLAG_NOTRUNC to not truncate second hash component fuzzy_free(state); if (status != 0) return -1; printf("%s %s\n", result, filename); return 0; } int main(int argc, char **argv) { if (argc < 2) return 1; return hash_large_file(argv[1]); } ``` -------------------------------- ### fuzzy_hash_stream Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Computes the fuzzy hash from a stream starting at the current position to EOF. This function is designed for non-seekable streams like pipes. ```APIDOC ## fuzzy_hash_stream ### Description Compute the fuzzy hash from a stream starting at current position to EOF. Unlike fuzzy_hash_file, this function never seeks and works with non-seekable streams like pipes. ### Method Not applicable (C function) ### Endpoint Not applicable (C function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include "fuzzy.h" int main(void) { // Hash data from stdin (pipe or redirect) char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_stream(stdin, result); if (status != 0) { fprintf(stderr, "Error hashing stream\n"); return 1; } printf("%s\n", result); return 0; } // Usage: cat largefile.bin | ./hash_stream ``` ### Response #### Success Response (0) - **result** (char array) - The computed fuzzy hash signature. #### Response Example ``` 96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m ``` ``` -------------------------------- ### Compute Fuzzy Hash from Stream Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Use fuzzy_hash_stream to compute a fuzzy hash from a stream, working with non-seekable streams like pipes. This example hashes data from stdin. ```c #include #include "fuzzy.h" int main(void) { // Hash data from stdin (pipe or redirect) char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_stream(stdin, result); if (status != 0) { fprintf(stderr, "Error hashing stream\n"); return 1; } printf("%s\n", result); return 0; } // Usage: cat largefile.bin | ./hash_stream ``` -------------------------------- ### ssdeep Command Line - Match Against Known Hashes Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Load a file of known signatures and match new files against them. Essential for malware detection and forensic investigations. ```APIDOC ## ssdeep Command Line - Match Against Known Hashes ### Description Load a file of known signatures and match new files against them. Essential for malware detection and forensic investigations. ### Method Command Line Tool ### Endpoint N/A (Command Line Utility) ### Parameters #### Command Line Arguments - **-m** (hash_file) - Required - Load a file of known signatures. - **-v** - Optional - Verbose output. - **-k** (hash_file1) - Optional - Compare hash file 1 against hash file 2. - **-x** (hash_file2) - Optional - Compare hash file 2 against hash file 1. - **-c** - Optional - Output in CSV format. ### Request Example ```bash # Create a database of known file hashes ssdeep -r /known/good/files > known_hashes.txt # Match unknown files against known hashes ssdeep -m known_hashes.txt suspicious_file.exe # Match multiple files with verbose output ssdeep -v -m known_hashes.txt /evidence/drive/* # Compare hash files against each other ssdeep -k known_malware.txt -x collected_hashes.txt # CSV output for easy parsing ssdeep -c -m known_hashes.txt *.dll ``` ### Response Example ``` # Output (CSV mode): # "suspicious.exe","known_malware.exe",95 ``` ``` -------------------------------- ### ssdeep Command Line - Hash Files Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute fuzzy hash signatures for one or more files. The output format includes the block size, two hash components, and the filename. ```APIDOC ## ssdeep Command Line - Hash Files ### Description Compute fuzzy hash signatures for one or more files. The output format includes the block size, two hash components, and the filename. ### Method Command Line Tool ### Endpoint N/A (Command Line Utility) ### Parameters #### Command Line Arguments - **-r** - Optional - Hash directories recursively. - **-l** - Optional - Display relative paths. - **-b** - Optional - Display bare filenames (no path). ### Request Example ```bash # Hash a single file ssdeep document.pdf # Hash multiple files ssdeep file1.txt file2.txt file3.dat # Hash all files in directory recursively ssdeep -r /path/to/directory # Hash with relative paths displayed ssdeep -l ./documents/*.txt # Hash with bare filenames (no path) ssdeep -b /full/path/to/file.bin ``` ### Response Example ``` # Output: # ssdeep,1.1--blocksize:hash:hash,filename # 3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr9v,"document.pdf" ``` ``` -------------------------------- ### Create Known Hashes Database with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Generate a file containing fuzzy hash signatures for a collection of known files, typically used for creating a reference database. ```bash # Create a database of known file hashes ssdeep -r /known/good/files > known_hashes.txt ``` -------------------------------- ### ssdeep Command Line - Compare Files Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Find similar files by comparing computed hashes against known signatures. The `-d` flag enables directory comparison mode. ```APIDOC ## ssdeep Command Line - Compare Files ### Description Find similar files by comparing computed hashes against known signatures. The `-d` flag enables directory comparison mode. ### Method Command Line Tool ### Endpoint N/A (Command Line Utility) ### Parameters #### Command Line Arguments - **-d** - Optional - Enable directory comparison mode. - **-t** (threshold) - Optional - Set the match threshold (0-100). - **-r** - Optional - Compare directories recursively. - **-a** - Optional - Display all matches. ### Request Example ```bash # Compare files in a directory to find similar ones ssdeep -d *.exe # Compare with explicit match threshold (0-100) ssdeep -t 50 -d suspicious_files/ # Compare recursively with all matches displayed ssdeep -d -r -a ./malware_samples/ ``` ### Response Example ``` # Output: # /path/to/file1.exe matches /path/to/file2.exe (97) ``` ``` -------------------------------- ### Recursively Hash Files with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute fuzzy hash signatures for all files within a directory and its subdirectories. ```bash # Hash all files in directory recursively ssdeep -r /path/to/directory ``` -------------------------------- ### Compare Files with Match Threshold using ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compare files in a directory for similarity, setting an explicit match threshold between 0 and 100. ```bash # Compare with explicit match threshold (0-100) ssdeep -t 50 -d suspicious_files/ ``` -------------------------------- ### Create and Use a Cloned Fuzzy State Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Demonstrates how to create an independent copy of a fuzzy_state using fuzzy_clone for parallel processing or checkpointing. The cloned state can be used independently. ```c #include #include "fuzzy.h" int main(void) { struct fuzzy_state *state = fuzzy_new(); if (!state) return 1; // Process common prefix const unsigned char prefix[] = "Common header data for all variants"; fuzzy_update(state, prefix, sizeof(prefix) - 1); // Clone state to process different suffixes struct fuzzy_state *state_clone = fuzzy_clone(state); if (!state_clone) { fuzzy_free(state); return 1; } // Process different data on each state const unsigned char suffix1[] = " - Variant A content"; const unsigned char suffix2[] = " - Variant B content"; fuzzy_update(state, suffix1, sizeof(suffix1) - 1); fuzzy_update(state_clone, suffix2, sizeof(suffix2) - 1); // Get both hashes char result1[FUZZY_MAX_RESULT], result2[FUZZY_MAX_RESULT]; fuzzy_digest(state, result1, 0); fuzzy_digest(state_clone, result2, 0); printf("Variant A: %s\n", result1); printf("Variant B: %s\n", result2); // Compare the variants int score = fuzzy_compare(result1, result2); printf("Similarity: %d%%\n", score); fuzzy_free(state); fuzzy_free(state_clone); return 0; } ``` -------------------------------- ### Hash Files with Bare Filenames using ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute fuzzy hash signatures and display only the bare filenames, omitting any path information. ```bash # Hash with bare filenames (no path) ssdeep -b /full/path/to/file.bin ``` -------------------------------- ### Match Hashes in CSV Format with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Match unknown files against a known hash database and output the results in CSV format for easy parsing. ```bash # CSV output for easy parsing ssdeep -c -m known_hashes.txt *.dll ``` -------------------------------- ### Match Unknown Files Against Known Hashes with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compare a suspicious file against a database of known fuzzy hashes to detect potential matches. ```bash # Match unknown files against known hashes ssdeep -m known_hashes.txt suspicious_file.exe ``` -------------------------------- ### Stateful Hashing API (fuzzy_new, fuzzy_update, fuzzy_digest, fuzzy_free) Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Provides a stateful API for incremental hashing, suitable for streaming data or processing large files in chunks without loading the entire content into memory. ```APIDOC ## Stateful Hashing API ### Description For streaming data or processing large files in chunks, use the stateful API. This allows incremental hashing without loading entire files into memory. ### Methods - **fuzzy_new**: Initializes a new fuzzy hashing state. - **fuzzy_update**: Updates the fuzzy hashing state with a chunk of data. - **fuzzy_digest**: Computes the final fuzzy hash signature from the current state. - **fuzzy_free**: Releases the resources associated with the fuzzy hashing state. ### Endpoint Not applicable (C functions) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include #include "fuzzy.h" #define CHUNK_SIZE 65536 int hash_large_file(const char *filename) { FILE *fp = fopen(filename, "rb"); if (!fp) return -1; // Create fuzzy state struct fuzzy_state *state = fuzzy_new(); if (!state) { fclose(fp); return -1; } // Optional: Set total length for optimization fseek(fp, 0, SEEK_END); long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); fuzzy_set_total_input_length(state, file_size); // Process file in chunks unsigned char buffer[CHUNK_SIZE]; size_t bytes_read; while ((bytes_read = fread(buffer, 1, CHUNK_SIZE, fp)) > 0) { if (fuzzy_update(state, buffer, bytes_read) != 0) { fprintf(stderr, "Error during update\n"); fuzzy_free(state); fclose(fp); return -1; } } fclose(fp); // Get final hash char result[FUZZY_MAX_RESULT]; int status = fuzzy_digest(state, result, 0); // Can call fuzzy_digest multiple times - state unchanged // Use FUZZY_FLAG_ELIMSEQ to eliminate repeated sequences // Use FUZZY_FLAG_NOTRUNC to not truncate second hash component fuzzy_free(state); if (status != 0) return -1; printf("%s %s\n", result, filename); return 0; } int main(int argc, char **argv) { if (argc < 2) return 1; return hash_large_file(argv[1]); } ``` ### Response #### Success Response (0) - **result** (char array) - The computed fuzzy hash signature for the processed data. #### Response Example ``` 96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m largefile.bin ``` ``` -------------------------------- ### Compare Hash Files with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compare two files containing fuzzy hashes against each other to find similarities between the sets of signatures. ```bash # Compare hash files against each other ssdeep -k known_malware.txt -x collected_hashes.txt ``` -------------------------------- ### ssdeep Command Line - Cluster Similar Files Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Group similar files together using the clustering feature. Useful for organizing large collections of potentially related files. ```APIDOC ## ssdeep Command Line - Cluster Similar Files ### Description Group similar files together using the clustering feature. Useful for organizing large collections of potentially related files. ### Method Command Line Tool ### Endpoint N/A (Command Line Utility) ### Parameters #### Command Line Arguments - **-g** - Optional - Enable clustering mode. - **-r** - Optional - Process directories recursively. - **-t** (threshold) - Optional - Set the similarity threshold (0-100). ### Request Example ```bash # Find and group similar files ssdeep -g -r /investigation/files/ # Cluster with threshold ssdeep -g -t 70 -r ./documents/ ``` ### Response Example ``` # Output: # *** Cluster 1 *** # /path/to/variant1.exe # /path/to/variant2.exe # /path/to/variant3.exe ``` ``` -------------------------------- ### Compare Files in Directory with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compare all executable files within a directory to identify similar ones using their fuzzy hashes. ```bash # Compare files in a directory to find similar ones ssdeep -d *.exe ``` -------------------------------- ### Hash Files with Relative Paths using ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute fuzzy hash signatures for files specified using relative paths, useful for pattern matching within a directory structure. ```bash # Hash with relative paths displayed ssdeep -l ./documents/*.txt ``` -------------------------------- ### Hash Multiple Files with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute fuzzy hash signatures for multiple files. This command processes a list of files provided as arguments. ```bash # Hash multiple files ssdeep file1.txt file2.txt file3.dat ``` -------------------------------- ### Match Multiple Files Verbose with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Match multiple unknown files against a known hash database with verbose output enabled to show detailed comparison results. ```bash # Match multiple files with verbose output ssdeep -v -m known_hashes.txt /evidence/drive/* ``` -------------------------------- ### Hash Single File with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute a fuzzy hash signature for a single file. The output includes block size, two hash components, and the filename. ```bash # Hash a single file ssdeep document.pdf ``` -------------------------------- ### Cluster Similar Files Recursively with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Find and group similar files within a directory and its subdirectories based on their fuzzy hashes. ```bash # Find and group similar files ssdeep -g -r /investigation/files/ ``` -------------------------------- ### C Library API - fuzzy_hash_filename Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute the fuzzy hash of a file given its path. Handles file opening and reading internally, avoiding Win32 file handle issues. ```APIDOC ## C Library API - fuzzy_hash_filename ### Description Compute the fuzzy hash of a file given its path. Handles file opening and reading internally, avoiding Win32 file handle issues. ### Method C Function ### Endpoint N/A (C Library Function) ### Parameters #### Function Parameters - **filename** (*const char* *) - Required - Path to the file to hash. - **result** (*char*) - Required - Buffer to store the computed fuzzy hash. Must be at least FUZZY_MAX_RESULT bytes. ### Return Value - **0** on success. - **-1** on error. ### Request Example ```c #include #include "fuzzy.h" int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_filename(argv[1], result); if (status != 0) { fprintf(stderr, "Error hashing file: %s\n", argv[1]); return 1; } printf("%s %s\n", result, argv[1]); return 0; } ``` ### Response Example ``` # Output: # 96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m /path/to/file.bin ``` ``` -------------------------------- ### Cluster Files with Threshold using ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Cluster files based on fuzzy hashes, applying a similarity threshold to group only highly related files. ```bash # Cluster with threshold ssdeep -g -t 70 -r ./documents/ ``` -------------------------------- ### Recursive Directory Comparison with ssdeep Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Perform a recursive comparison of files within a directory, displaying all matches found. ```bash # Compare recursively with all matches displayed ssdeep -d -r -a ./malware_samples/ ``` -------------------------------- ### C Library API - fuzzy_hash_buf Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compute the fuzzy hash of a memory buffer. This is the most efficient method when data is already loaded in memory. ```APIDOC ## C Library API - fuzzy_hash_buf ### Description Compute the fuzzy hash of a memory buffer. This is the most efficient method when data is already loaded in memory. ### Method C Function ### Endpoint N/A (C Library Function) ### Parameters #### Function Parameters - **data** (*const unsigned char*) - Required - Pointer to the buffer containing the data to hash. - **length** (*size_t*) - Required - The length of the data buffer. - **result** (*char*) - Required - Buffer to store the computed fuzzy hash. Must be at least FUZZY_MAX_RESULT bytes. ### Return Value - **0** on success. - **-1** on error. ### Request Example ```c #include #include #include #include "fuzzy.h" int main(void) { const char *data = "This is sample content to be hashed using ssdeep fuzzy hashing algorithm"; char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_buf( (const unsigned char *)data, strlen(data), result ); if (status != 0) { fprintf(stderr, "Error computing fuzzy hash\n"); return 1; } printf("Fuzzy hash: %s\n", result); return 0; } ``` ### Response Example ``` # Output: # Fuzzy hash: 3:hMCEpn:hn ``` ``` -------------------------------- ### Compute Fuzzy Hash of a Memory Buffer with C Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Computes the fuzzy hash of data directly from a memory buffer using the `fuzzy_hash_buf` function. This is efficient for in-memory data. ```c #include #include #include #include "fuzzy.h" int main(void) { const char *data = "This is sample content to be hashed using ssdeep fuzzy hashing algorithm"; char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_buf( (const unsigned char *)data, strlen(data), result ); if (status != 0) { fprintf(stderr, "Error computing fuzzy hash\n"); return 1; } printf("Fuzzy hash: %s\n", result); // Output: 3:hMCEpn:hn return 0; } ``` -------------------------------- ### Compute Fuzzy Hash from File Handle Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Use fuzzy_hash_file to compute a fuzzy hash from an open file handle. Ensure the file is opened in binary mode. The file position is restored after hashing. ```c #include #include "fuzzy.h" int main(void) { FILE *handle = fopen("document.pdf", "rb"); if (handle == NULL) { perror("Failed to open file"); return 1; } char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_file(handle, result); fclose(handle); if (status != 0) { fprintf(stderr, "Error computing hash\n"); return 1; } printf("Hash: %s\n", result); // File position is restored to original after hashing return 0; } ``` -------------------------------- ### Compute Fuzzy Hash of a File with C Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Computes the fuzzy hash of a file using its path with the `fuzzy_hash_filename` function. This function handles file operations internally. ```c #include #include "fuzzy.h" int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_filename(argv[1], result); if (status != 0) { fprintf(stderr, "Error hashing file: %s\n", argv[1]); return 1; } printf("%s %s\n", result, argv[1]); // Output: 96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m /path/to/file.bin return 0; } ``` -------------------------------- ### Compare Fuzzy Hashes Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Use fuzzy_compare to compare two fuzzy hash signatures, returning a similarity score from 0 to 100. A score of -1 indicates an error. ```c #include #include "fuzzy.h" int main(void) { // Hash signatures from two similar files const char *sig1 = "96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m"; const char *sig2 = "96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m"; // Hash signature from a different file const char *sig3 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr9v"; int score1 = fuzzy_compare(sig1, sig2); int score2 = fuzzy_compare(sig1, sig3); if (score1 == -1 || score2 == -1) { fprintf(stderr, "Error comparing signatures\n"); return 1; } printf("Identical files score: %d\n", score1); // Output: 100 printf("Different files score: %d\n", score2); // Output: 0 // Practical threshold for similarity detection if (score1 >= 70) { printf("Files are likely related or derived from each other\n"); } return 0; } ``` -------------------------------- ### fuzzy_compare Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Compares two fuzzy hash signatures and returns a similarity score between 0 and 100. A score of 0 indicates no match, while 100 indicates identical content. ```APIDOC ## fuzzy_compare ### Description Compare two fuzzy hash signatures and return a similarity score from 0 to 100. A score of 0 indicates no match; 100 indicates identical content. ### Method Not applicable (C function) ### Endpoint Not applicable (C function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include "fuzzy.h" int main(void) { // Hash signatures from two similar files const char *sig1 = "96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m"; const char *sig2 = "96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m"; // Hash signature from a different file const char *sig3 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr9v"; int score1 = fuzzy_compare(sig1, sig2); int score2 = fuzzy_compare(sig1, sig3); if (score1 == -1 || score2 == -1) { fprintf(stderr, "Error comparing signatures\n"); return 1; } printf("Identical files score: %d\n", score1); // Output: 100 printf("Different files score: %d\n", score2); // Output: 0 // Practical threshold for similarity detection if (score1 >= 70) { printf("Files are likely related or derived from each other\n"); } return 0; } ``` ### Response #### Success Response (0 to 100) - **score** (int) - The similarity score between the two fuzzy hashes. #### Response Example ``` Identical files score: 100 Different files score: 0 ``` ``` -------------------------------- ### fuzzy_hash_file Source: https://context7.com/ssdeep-project/ssdeep/llms.txt Computes the fuzzy hash from an open file handle. This function is suitable when the file is already open or when fine-grained control over file operations is needed. The file position is restored after hashing. ```APIDOC ## fuzzy_hash_file ### Description Compute the fuzzy hash from an open file handle. Useful when the file is already open or when you need control over file operations. ### Method Not applicable (C function) ### Endpoint Not applicable (C function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c #include #include "fuzzy.h" int main(void) { FILE *handle = fopen("document.pdf", "rb"); if (handle == NULL) { perror("Failed to open file"); return 1; } char result[FUZZY_MAX_RESULT]; int status = fuzzy_hash_file(handle, result); fclose(handle); if (status != 0) { fprintf(stderr, "Error computing hash\n"); return 1; } printf("Hash: %s\n", result); // File position is restored to original after hashing return 0; } ``` ### Response #### Success Response (0) - **result** (char array) - The computed fuzzy hash signature. #### Response Example ``` Hash: 96:U7GjfnLtl3uhGakMzMMszef0fXfj0fom:5jtluhGakvMMsef0fn0m ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.