Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MaskGenerationMethod Class
Collapse the table of content
Expand the table of content

MaskGenerationMethod Class

Represents the abstract class from which all mask generator algorithms must derive.


Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

public abstract class MaskGenerationMethod

The MaskGenerationMethod type exposes the following members.

Protected methodMaskGenerationMethodInitializes a new instance of the MaskGenerationMethod class.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGenerateMaskWhen overridden in a derived class, generates a mask with the specified length using the specified random seed.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Derived classes of MaskGenerationMethod compute masks that are used in key exchange algorithms such as Optimal Asymmetric Encryption Padding (OAEP).

This class is used by implementations of key exchange algorithms only for mask generation. Application code does not use this class directly.

The following code example demonstrates how to derive from the MaskGenerationMethod class.

using System;
using System.Security.Cryptography;

namespace Contoso
    class MaskGenerator : System.Security.Cryptography.MaskGenerationMethod
        private String HashNameValue;

        // Initialize a mask to encrypt using the SHA1 algorithm.
        public MaskGenerator() 
            HashNameValue = "SHA1";

        // Create a mask with the specified seed.
        override public byte[] GenerateMask(byte[] seed, int maskLength)
            HashAlgorithm hash;
            byte[] rgbCounter = new byte[4];
            byte[] targetRgb = new byte[maskLength];
            uint counter = 0;

            for (int inc = 0; inc < targetRgb.Length; )
                ConvertIntToByteArray(counter++, ref rgbCounter);
                hash = (HashAlgorithm)

                byte[] temp = new byte[4 + seed.Length];
                Buffer.BlockCopy(rgbCounter, 0, temp, 0, 4);
                Buffer.BlockCopy(seed, 0, temp, 4, seed.Length);

                if (targetRgb.Length - inc > hash.HashSize/8) 
                        targetRgb.Length - inc);
                inc += hash.Hash.Length;
            return targetRgb;

        // Convert the specified integer to the byte array.
        private void ConvertIntToByteArray(
            uint sourceInt,
            ref byte[] targetBytes)
            uint remainder;
            int inc = 0;

            // Clear the array prior to filling it.
            Array.Clear(targetBytes, 0, targetBytes.Length);

            while (sourceInt > 0) 
                remainder = sourceInt % 256;
                targetBytes[3 - inc] = (byte) remainder;
                sourceInt = (sourceInt - remainder)/256;
// This class demonstrates how to create the MaskGenerator class 
// and call its GenerateMask member.
    class MaskGeneratorImpl
      public static void Main(string[] args)
          byte[] seed = new byte[] {0x01, 0x02, 0x03, 0x04};
          int length = 16;
          MaskGenerator maskGenerator = new MaskGenerator();
          byte[] mask = maskGenerator.GenerateMask(seed, length);
          Console.WriteLine("Generated the following mask:");

          Console.WriteLine("This sample completed successfully; " +
                "press Enter to exit.");
// This sample produces the following output:
// Generated the following mask:
// ?"TFd(?~OtO?
// This sample completed successfully; press Enter to exit.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

© 2015 Microsoft