library/src/utility/BP-StringUtilities.cc
BP_BOOL BP_PasswordMatchesSaltedPlaintext(char *plaintext_password, char *hash_algo, char *hash, char *salt)
{

	// ensure all parts are non-null
	if(!plaintext_password)
		return BP_FALSE;
	if(!hash_algo)
		return BP_FALSE;
	if(!hash)
		return BP_FALSE;
	if(!salt)
		return BP_FALSE;

	// run reasonable string checks on all parts
	if(!BP_StringIsReasonablePrintableString(plaintext_password, bpstrlen(plaintext_password), BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(hash_algo, bpstrlen(hash_algo), BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(hash,      bpstrlen(hash),      BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(salt,      bpstrlen(salt),      BP_FALSE, BP_FALSE))
		return BP_FALSE;


	// set combined plaintext and salt
	char * combined_plaintext_and_salt = BP_JoinStrings(salt, plaintext_password, NULL);
	if(!combined_plaintext_and_salt)
		return BP_FALSE;

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%% Create Hash %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// hashed value
	char *hashed_value = NULL;

	// create hashes based on the hash algorithm
	if(BP_StringsMatchAsReasonableStrings(hash_algo, "sha1"))
	{
		hashed_value = BP_GenSHA1(combined_plaintext_and_salt, bpstrlen(combined_plaintext_and_salt));
	}
	else if(BP_StringsMatchAsReasonableStrings(hash_algo, "sha256"))
	{
		hashed_value = BP_GenSHA256(combined_plaintext_and_salt, bpstrlen(combined_plaintext_and_salt));
	}
	else
	{

		// zero out the plaintext/salt
		memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));

		// destroy the combined plaintext and salt
		bpfree(combined_plaintext_and_salt);
		bpfree(hashed_value);

		// return indicating that a match was made
		return BP_FALSE;

	}


	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%% Check Hash %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	printf("\n HASHED VALUE: %s", hashed_value);
	printf("\n HASH: %s", hash);
	printf("\n PASSCOMBINED: %s", combined_plaintext_and_salt);

	if(BP_StringsMatchAsReasonableStrings(hashed_value, hash))
	{

		// zero out the plaintext/salt
		memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));

		// destroy the combined plaintext and salt
		bpfree(combined_plaintext_and_salt);
		bpfree(hashed_value);

		// return indicating that a match was made
		return BP_TRUE;

	}


	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%% Cleanup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	// zero out the plaintext/salt
	memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));

	// destroy the combined plaintext and salt
	bpfree(combined_plaintext_and_salt);
	bpfree(hashed_value);

	// return indicating no match
	return BP_FALSE;

}
// Returns true if the salted password matches the plaintext provided
Returns true if the salted password matches the plaintext provided
Returns true if the salted password matches the plaintext provided
BP_BOOL BP_PasswordMatchesSaltedPlaintext(char *plaintext_password, char *hash_algo, char *hash, char *salt)
{
	if(!plaintext_password)
		return BP_FALSE;
	if(!hash_algo)
		return BP_FALSE;
	if(!hash)
		return BP_FALSE;
	if(!salt)
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(plaintext_password, bpstrlen(plaintext_password), BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(hash_algo, bpstrlen(hash_algo), BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(hash,      bpstrlen(hash),      BP_FALSE, BP_FALSE))
		return BP_FALSE;
	if(!BP_StringIsReasonablePrintableString(salt,      bpstrlen(salt),      BP_FALSE, BP_FALSE))
		return BP_FALSE;
	char * combined_plaintext_and_salt = BP_JoinStrings(salt, plaintext_password, NULL);
	if(!combined_plaintext_and_salt)
		return BP_FALSE;
	char *hashed_value = NULL;
	if(BP_StringsMatchAsReasonableStrings(hash_algo, "sha1"))
	{
		hashed_value = BP_GenSHA1(combined_plaintext_and_salt, bpstrlen(combined_plaintext_and_salt));
	}
	else if(BP_StringsMatchAsReasonableStrings(hash_algo, "sha256"))
	{
		hashed_value = BP_GenSHA256(combined_plaintext_and_salt, bpstrlen(combined_plaintext_and_salt));
	}
	else
	{
		memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));
		bpfree(combined_plaintext_and_salt);
		bpfree(hashed_value);
		return BP_FALSE;
	}
	printf("\n HASHED VALUE: %s", hashed_value);
	printf("\n HASH: %s", hash);
	printf("\n PASSCOMBINED: %s", combined_plaintext_and_salt);
	if(BP_StringsMatchAsReasonableStrings(hashed_value, hash))
	{
		memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));
		bpfree(combined_plaintext_and_salt);
		bpfree(hashed_value);
		return BP_TRUE;
	}
	memset(combined_plaintext_and_salt, 0x00, bpstrlen(combined_plaintext_and_salt));
	bpfree(combined_plaintext_and_salt);
	bpfree(hashed_value);
	return BP_FALSE;
}
BP_BOOL BP_PasswordMatchesSaltedPlaintext ( char * plaintext_password , char * hash_algo , char * hash , char * salt ) { if ( ! plaintext_password ) return BP_FALSE ; if ( ! hash_algo ) return BP_FALSE ; if ( ! hash ) return BP_FALSE ; if ( ! salt ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( plaintext_password , bpstrlen ( plaintext_password ) , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( hash_algo , bpstrlen ( hash_algo ) , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( hash , bpstrlen ( hash ) , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; if ( ! BP_StringIsReasonablePrintableString ( salt , bpstrlen ( salt ) , BP_FALSE , BP_FALSE ) ) return BP_FALSE ; char * combined_plaintext_and_salt = BP_JoinStrings ( salt , plaintext_password , NULL ) ; if ( ! combined_plaintext_and_salt ) return BP_FALSE ; char * hashed_value = NULL ; if ( BP_StringsMatchAsReasonableStrings ( hash_algo , " " ) ) { hashed_value = BP_GenSHA1 ( combined_plaintext_and_salt , bpstrlen ( combined_plaintext_and_salt ) ) ; } else if ( BP_StringsMatchAsReasonableStrings ( hash_algo , " " ) ) { hashed_value = BP_GenSHA256 ( combined_plaintext_and_salt , bpstrlen ( combined_plaintext_and_salt ) ) ; } else { memset ( combined_plaintext_and_salt , 0x00 , bpstrlen ( combined_plaintext_and_salt ) ) ; bpfree ( combined_plaintext_and_salt ) ; bpfree ( hashed_value ) ; return BP_FALSE ; } printf ( " \n " , hashed_value ) ; printf ( " \n " , hash ) ; printf ( " \n " , combined_plaintext_and_salt ) ; if ( BP_StringsMatchAsReasonableStrings ( hashed_value , hash ) ) { memset ( combined_plaintext_and_salt , 0x00 , bpstrlen ( combined_plaintext_and_salt ) ) ; bpfree ( combined_plaintext_and_salt ) ; bpfree ( hashed_value ) ; return BP_TRUE ; } memset ( combined_plaintext_and_salt , 0x00 , bpstrlen ( combined_plaintext_and_salt ) ) ; bpfree ( combined_plaintext_and_salt ) ; bpfree ( hashed_value ) ; return BP_FALSE ; }
Returns true if the salted password matches the plaintext provided
Returns true if the salted password matches the plaintext provided
// ensure all parts are non-null // run reasonable string checks on all parts // set combined plaintext and salt // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Create Hash %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // hashed value // create hashes based on the hash algorithm // zero out the plaintext/salt // destroy the combined plaintext and salt // return indicating that a match was made // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check Hash %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // zero out the plaintext/salt // destroy the combined plaintext and salt // return indicating that a match was made // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Cleanup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // zero out the plaintext/salt // destroy the combined plaintext and salt // return indicating no match