Skip to content

Instantly share code, notes, and snippets.

@MechMK1
Created February 19, 2020 13:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MechMK1/d95ece5ca66ccc884267d18268ea7191 to your computer and use it in GitHub Desktop.
Save MechMK1/d95ece5ca66ccc884267d18268ea7191 to your computer and use it in GitHub Desktop.
A simple tool to calculate the number of bits differing between the SHA-256 hashes of two strings
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <https://unlicense.org>
*/
/*
* Checks the number of bits differing between two string inputs
* Usage: bitChecker Foo Bar
*
* Compile using the following:
* gcc bitChecker.c -o bitChecker -lcrypto
*/
#include <stdio.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <string.h>
// An array which quickly determines the number of bits in one byte. This trades off higher memory usage for faster execution speed.
// Just kidding, I just copied this shit from google because I was too lazy to write int count_bits(byte b) myself.
int bit_count[] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
// Prints the hexadecimal representation of a SHA-256 hash
void print_sha256(unsigned char *hash)
{
int i = 0;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
printf("%02x", hash[i]);
}
printf("\n");
}
// Calculates a SHA-256 hash from a string
void sha256_string(char *string, unsigned char * output)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(output, &sha256);
}
int main(int argc, char* argv[])
{
int i = 0;
int totalBits = 0;
unsigned char hash1[SHA256_DIGEST_LENGTH];
unsigned char hash2[SHA256_DIGEST_LENGTH];
if(argc < 3)
{
fprintf(stderr, "Usage: %s STRING1 STRING2\n", argv[0]);
exit(1);
}
sha256_string(argv[1], hash1);
sha256_string(argv[2], hash2);
printf("SHA-256 hash of '%s':\n", argv[1]);
print_sha256(hash1);
printf("SHA-256 hash of '%s':\n", argv[2]);
print_sha256(hash2);
// The magic happens here
// Iterates 256 times to go through every bit.
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
// Calculating the XOR between both hashes returns the bits where both hashes differ
unsigned char xorRes = hash1[i] ^ hash2[i];
// We use the lookup table from above to determine how many bits differ for a given byte.
totalBits += bit_count[xorRes];
}
printf("\n");
printf("Total differing bits: %d\n", totalBits);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment