January 2004
Beginner to intermediate
864 pages
22h 18m
English
You have some text that will be sent across a network to another machine for processing. It is critical that you are able to verify that this text remains intact and unmodified when it arrives at its destination.
Calculate a hash value from
this string and append it to the string before it is sent to its
destination. Once the destination receives the string, it can remove
the hash value and determine whether the string is the same one that
was initially sent. The CreateStringHash method
takes a string as input, adds a hash value to the
end of it, and returns the new
string:
public class HashOps { public static string CreateStringHash(string unHashedString) { byte[] encodedUnHashedString = Encoding.Unicode.GetBytes(unHashedString); SHA256Managed hashingObj = new SHA256Managed( ); byte[] hashCode = hashingObj.ComputeHash(encodedUnHashedString); string hashBase64 = Convert.ToBase64String(hashCode); string stringWithHash = unHashedString + hashBase64; hashingObj.Clear( ); return (stringWithHash); } public static bool TestReceivedStringHash(string stringWithHash, out string originalStr) { // Code to quickly test the handling of a tampered string //stringWithHash = stringWithHash.Replace('a', 'b'); if (stringWithHash.Length < 45) { originalStr = null; return (true); } string hashCodeString = stringWithHash.Substring(stringWithHash.Length - 44); string unHashedString = stringWithHash.Substring(0, stringWithHash.Length ...Read now
Unlock full access