
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
970
|
Chapter 17: Security
The attractiveness of the decorator pattern is that you can add any number of con-
crete decorator classes that derive from either
ReaderDecorator or WriterDecorator. If
you need to use a different hashing algorithm, or even a quick and dirty hash algo-
rithm, you can subclass the
ReaderDecorator or WriterDecorator classes and add
functionality to use these new algorithms. Now you have more choices of how to
decorate these classes.
See Also
See the “StringWriter Class” and “StringReader Class” topics in the MSDN docu-
mentation.
17.7 A Better Random Number Generator
Problem
You need a random number with which to generate items such as a sequence of ses-
sion keys. The random number must be as unpredictable as possible so that the like-
lihood of predicting the sequence of keys is as low as possible.
Solution
Use the class System.Security.Cryptography.RNGCryptoServiceProvider.
The
RNGCryptoServiceProvider is used to populate a random byte array using the
GetBytes method that is then printed out as a string in the following example:
public static void BetterRandomString( )
{
// Create a stronger hash code using RNGCryptoServiceProvider.
byte[] random = new byte[64];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider( );
// Populate with random bytes.
rng.GetBytes(random); ...