
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
964
|
Chapter 17: Security
This method first removes the hash value from the end of the StringWithHash vari-
able. Next, a new hash is calculated using the string portion of the
StringWithHash
variable. These two hash values are compared. If they are the same, the string has
been received, unmodified. Note that if you change the hashing algorithm used, you
must change it in both this method and the
CreateStringHash method. You must also
change the HASH_LENGTH constant in the
TestReceivedStringHash method to an
appropriate size for the new hashing algorithm. This number is the exact length of
the base64 representation of the hash value, which was appended to the string.
See Also
See the “SHA256Managed Class,” “Convert.ToBase64String Method,” and “Convert.
FromBase64String Method” topics in the MSDN documentation.
17.6 Wrapping a String Hash for Ease of Use
Problem
You need to create a class to isolate other developers on your team from the details
of adding a hash to a string, as well as the details of using the hash to verify if the
string has been modified or corrupted.
Solution
The following classes decorate the StringWriter and StringReader classes to handle a
hash added to its contained string. The
WriterDecorator and StringWriterHash
classes allow the StringWriter class to be decorated