by

Generate Hash Key In C

  1. Pound Key
  2. Generate Hash Key In C 10
26 Jun 2019CPOL

Hash Generator is the FREE universal hash generator tool which automates the generation of 14 different type of hashes or checksums. It support most of the popular hashes including MD5 family, SHA family, BASE64, LM, NTLM, CRC32, ROT13, RIPEMD, ALDER32, HAVAL, WHIRLPOOL etc.

Nov 04, 2019  Hi, Thanks for taking the time to write this implementation. I've learned a lot from it. You could make the void htset( hashtablet.hashtable, char.key, char.value ) function a bit simpler. Currently your logic tries to: Find if the key already exists in the table. Feb 12, 2018  Delete: To delete a node from hash table, calculate the hash index for the key, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key (if found). Please refer Hashing Set 2 (Separate Chaining) for details. We use a list in C which is internally implemented as linked list (Faster insertion.

Introduction

Hashing is the transformation process of value into a usually shorter fixed-length key/value that represents the original value. A few days ago, we had to use hash comparison to sync data between two systems via API (obviously, it wasn't the most efficient way to use API for data syncing, but we had no option to add any change at source end).

Background

What we were doing:

  1. Creating a hash string at our end after object JSON deserialization
  2. Comparing that hash string with an existing DB row by a unique identifier (Primary key)
    1. If no row found by the unique identifier (Primary key), adding a new row to the DB
    2. If the hash string wasn't the same, updating the existing row with new values
  3. And few other sync log processes

Everything was working as expected until we refactored the existing code (changed name of a few models and properties). The hash string was being generated from the entire object (including all the values) rather than considering specific properties. The way we were creating the hash string was actually wrong. Let's check a few hash string examples.

Hash Helper Class

This is the utility class to manage hash related operations.

Consideration

  • Using MD5 hash Hash(byte[] value)
  • Any null value is considered as 'null' string Byte(object value)

Object to Hash String Process

  1. Create bytes of that object Byte(object value)
  2. Create hash bytes from the object bytes Hash(byte[] value)
  3. String from hash bytes String(byte[] hash)

A Combined Hash of Multiple Objects

  1. Create bytes of each object Byte(object value)
  2. Combine or sum the bytes Combine(params byte[][] values)
  3. Create hash bytes from the combine or sum the bytes Hash(byte[] value)
  4. String from hash bytes String(byte[] hash)

Alternatively:

  1. Create combined hash bytes Hash(params object[] values)
  2. String from hash bytes String(byte[] hash)

Methods We Are Going to Use More Frequently

  • Create a hash string of any string HashString(string value, Encoding encoding = null)
  • Create hash/combine hash string of any/group of object HashString(params object[] values)

Hash of Entire Object

The data class or model:

Creating a hash of the model:

Important to Remember

This hash depends on both object structures and assigned values. The generated hash will not be the same even if we assign the same values to the properties, but added some changes like:

  • Class/Model name change
  • Property name change
  • Namespace name change
  • Property Number change (add or remove any property)

to the model. And in a development environment, refactoring can take place any time.

Hash of Data Values

Let's make a hash using only values. Creating an interface IHash.

Using IHash to a model and using hash helper inside the method HashString().

Generate Hash Key In C

This way, the model structure is not taking part in the hash generation process, only specific property values (Name, IsActive, CreatedDateTime) are being considered.

Pound Key

Hash will remain the same until no new value has been set to any of those properties. Any structural change (name change, property add/remove, etc.) to the model will not affect the hash string.

Hash Result

Other Tests

Working fine with null object values:

We will not be able to create the entire People class as it is not using [Serializable]:

BONUS: String Hash

It is quite common to create a password/string hash. So here we have it.

Conclusion

  • If we have to compare considering values or specific values only, then using Hash of Data Values is the best option.
  • But if we need to compare both object structure and values altogether, go for Hash of Entire Object.

References

My first read many years ago

Bytes

Hash Bytes

Combined Bytes

Bytes to String

Limitations

I haven't considered all possible worst scenarios or code may throw unexpected errors for untested inputs. If any, just let me know.

Find Visual Studio 2017 console application sample code as attachment. Aes 256 key generator online.

History

  • 26th June, 2019: Initial version
-->
Key

Definition

Computes the signature for the specified hash value.

Overloads

SignHash(Byte[], String)

Computes the signature for the specified hash value.

SignHash(Byte[], HashAlgorithmName, RSASignaturePadding)

Computes the signature for the specified hash value using the specified padding.

Computes the signature for the specified hash value.

Parameters

rgbHash
Byte[]

The hash value of the data to be signed.

str
String

The hash algorithm identifier (OID) used to create the hash value of the data.

Returns

The RSA signature for the specified hash value.

Exceptions

The rgbHash parameter is null.

The cryptographic service provider (CSP) cannot be acquired.

/generate-a-new-ssh-key-passphrase.html. -or-

There is no private key.

Examples

The following code example encrypts some data, creates a hash of the encrypted data, and then signs hash with a digital signature.

Generate Hash Key In C 10

Remarks

This method creates a digital signature that is verified using the VerifyHash method.

The valid hash algorithms are SHA1 and MD5. The algorithm identifier can be derived from the hash name by using the MapNameToOID method.

Due to collision problems with SHA1 and MD5, Microsoft recommends a security model based on SHA256 or better.

See also

SignHash(Byte[], HashAlgorithmName, RSASignaturePadding)

Computes the signature for the specified hash value using the specified padding.

Parameters

hash
Byte[]

The hash value of the data to be signed.

hashAlgorithm
HashAlgorithmName

The hash algorithm name used to create the hash value of the data.

Returns

The RSA signature for the specified hash value.

Exceptions

hashAlgorithm is null or Empty.

hash is null.

-or-

padding is null.

padding does not equal Pkcs1.

Applies to