// // System.Security.Cryptography KeySizes Class implementation // // Authors: // Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu) // Ben Maurer (bmaurer@users.sf.net) // Sebastien Pouliot (spouliot@motus.com) // // Copyright 2001 by Matthew S. Ford. // namespace System.Security.Cryptography { /// /// This class represents valid ranges of key sizes for ciphers. It is also used to represent block sizes in the same fashion for block ciphers. /// #if NET_1_0 public class KeySizes { #else public sealed class KeySizes { #endif private int _maxSize; private int _minSize; private int _skipSize; /// /// Creates a new KeySizes object. /// /// The minimum size key allowed for this cipher in bits. /// The maximum size key allowed for this cipher in bits. /// The jump/skip between the valid key sizes in bits. public KeySizes (int minSize, int maxSize, int skipSize) { _maxSize = maxSize; _minSize = minSize; _skipSize = skipSize; } /// /// Returns the maximum valid key size in bits; /// public int MaxSize { get { return _maxSize; } } /// /// Returns the minimum valid key size in bits; /// public int MinSize { get { return _minSize; } } /// /// Returns the skip between valid key sizes in bits; /// public int SkipSize { get { return _skipSize; } } /// /// Checks if a key of keySize bits is valid, according to this /// rule. /// /// The keysize to check. /// True if the key size is legal, else false. internal bool IsLegal (int keySize) { int ks = keySize - MinSize; bool result = ((ks >= 0) && (keySize <= MaxSize)); return ((SkipSize == 0) ? result : (result && (ks % SkipSize == 0))); } internal static bool IsLegalKeySize (KeySizes[] legalKeys, int size) { foreach (KeySizes legalKeySize in legalKeys) { if (legalKeySize.IsLegal (size)) return true; } return false; } } }