mscorlib(4.0.0.0) API with additions
DSA.cs
1 using System.IO;
3 using System.Security.Util;
4 using System.Text;
5 
7 {
9  [ComVisible(true)]
10  public abstract class DSA : AsymmetricAlgorithm
11  {
14  public new static DSA Create()
15  {
16  return Create("System.Security.Cryptography.DSA");
17  }
18 
22  public new static DSA Create(string algName)
23  {
24  return (DSA)CryptoConfig.CreateFromName(algName);
25  }
26 
30  public abstract byte[] CreateSignature(byte[] rgbHash);
31 
37  public abstract bool VerifySignature(byte[] rgbHash, byte[] rgbSignature);
38 
46  protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
47  {
48  throw DerivedClassMustOverride();
49  }
50 
56  protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
57  {
58  throw DerivedClassMustOverride();
59  }
60 
69  public byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm)
70  {
71  if (data == null)
72  {
73  throw new ArgumentNullException("data");
74  }
75  return SignData(data, 0, data.Length, hashAlgorithm);
76  }
77 
92  public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
93  {
94  if (data == null)
95  {
96  throw new ArgumentNullException("data");
97  }
98  if (offset < 0 || offset > data.Length)
99  {
100  throw new ArgumentOutOfRangeException("offset");
101  }
102  if (count < 0 || count > data.Length - offset)
103  {
104  throw new ArgumentOutOfRangeException("count");
105  }
106  if (string.IsNullOrEmpty(hashAlgorithm.Name))
107  {
108  throw HashAlgorithmNameNullOrEmpty();
109  }
110  byte[] rgbHash = HashData(data, offset, count, hashAlgorithm);
111  return CreateSignature(rgbHash);
112  }
113 
122  public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm)
123  {
124  if (data == null)
125  {
126  throw new ArgumentNullException("data");
127  }
128  if (string.IsNullOrEmpty(hashAlgorithm.Name))
129  {
130  throw HashAlgorithmNameNullOrEmpty();
131  }
132  byte[] rgbHash = HashData(data, hashAlgorithm);
133  return CreateSignature(rgbHash);
134  }
135 
147  public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm)
148  {
149  if (data == null)
150  {
151  throw new ArgumentNullException("data");
152  }
153  return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
154  }
155 
173  public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm)
174  {
175  if (data == null)
176  {
177  throw new ArgumentNullException("data");
178  }
179  if (offset < 0 || offset > data.Length)
180  {
181  throw new ArgumentOutOfRangeException("offset");
182  }
183  if (count < 0 || count > data.Length - offset)
184  {
185  throw new ArgumentOutOfRangeException("count");
186  }
187  if (signature == null)
188  {
189  throw new ArgumentNullException("signature");
190  }
191  if (string.IsNullOrEmpty(hashAlgorithm.Name))
192  {
193  throw HashAlgorithmNameNullOrEmpty();
194  }
195  byte[] rgbHash = HashData(data, offset, count, hashAlgorithm);
196  return VerifySignature(rgbHash, signature);
197  }
198 
210  public virtual bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm)
211  {
212  if (data == null)
213  {
214  throw new ArgumentNullException("data");
215  }
216  if (signature == null)
217  {
218  throw new ArgumentNullException("signature");
219  }
220  if (string.IsNullOrEmpty(hashAlgorithm.Name))
221  {
222  throw HashAlgorithmNameNullOrEmpty();
223  }
224  byte[] rgbHash = HashData(data, hashAlgorithm);
225  return VerifySignature(rgbHash, signature);
226  }
227 
232  public override void FromXmlString(string xmlString)
233  {
234  if (xmlString == null)
235  {
236  throw new ArgumentNullException("xmlString");
237  }
238  DSAParameters parameters = default(DSAParameters);
239  Parser parser = new Parser(xmlString);
240  SecurityElement topElement = parser.GetTopElement();
241  string text = topElement.SearchForTextOfLocalName("P");
242  if (text == null)
243  {
244  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "P"));
245  }
246  parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text));
247  string text2 = topElement.SearchForTextOfLocalName("Q");
248  if (text2 == null)
249  {
250  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Q"));
251  }
252  parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text2));
253  string text3 = topElement.SearchForTextOfLocalName("G");
254  if (text3 == null)
255  {
256  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "G"));
257  }
258  parameters.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text3));
259  string text4 = topElement.SearchForTextOfLocalName("Y");
260  if (text4 == null)
261  {
262  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Y"));
263  }
264  parameters.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text4));
265  string text5 = topElement.SearchForTextOfLocalName("J");
266  if (text5 != null)
267  {
268  parameters.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text5));
269  }
270  string text6 = topElement.SearchForTextOfLocalName("X");
271  if (text6 != null)
272  {
273  parameters.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text6));
274  }
275  string text7 = topElement.SearchForTextOfLocalName("Seed");
276  string text8 = topElement.SearchForTextOfLocalName("PgenCounter");
277  if (text7 != null && text8 != null)
278  {
279  parameters.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text7));
280  parameters.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(text8)));
281  }
282  else if (text7 != null || text8 != null)
283  {
284  if (text7 == null)
285  {
286  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Seed"));
287  }
288  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "PgenCounter"));
289  }
290  ImportParameters(parameters);
291  }
292 
297  public override string ToXmlString(bool includePrivateParameters)
298  {
299  DSAParameters dSAParameters = ExportParameters(includePrivateParameters);
300  StringBuilder stringBuilder = new StringBuilder();
301  stringBuilder.Append("<DSAKeyValue>");
302  stringBuilder.Append("<P>" + Convert.ToBase64String(dSAParameters.P) + "</P>");
303  stringBuilder.Append("<Q>" + Convert.ToBase64String(dSAParameters.Q) + "</Q>");
304  stringBuilder.Append("<G>" + Convert.ToBase64String(dSAParameters.G) + "</G>");
305  stringBuilder.Append("<Y>" + Convert.ToBase64String(dSAParameters.Y) + "</Y>");
306  if (dSAParameters.J != null)
307  {
308  stringBuilder.Append("<J>" + Convert.ToBase64String(dSAParameters.J) + "</J>");
309  }
310  if (dSAParameters.Seed != null)
311  {
312  stringBuilder.Append("<Seed>" + Convert.ToBase64String(dSAParameters.Seed) + "</Seed>");
313  stringBuilder.Append("<PgenCounter>" + Convert.ToBase64String(Utils.ConvertIntToByteArray(dSAParameters.Counter)) + "</PgenCounter>");
314  }
315  if (includePrivateParameters)
316  {
317  stringBuilder.Append("<X>" + Convert.ToBase64String(dSAParameters.X) + "</X>");
318  }
319  stringBuilder.Append("</DSAKeyValue>");
320  return stringBuilder.ToString();
321  }
322 
327  public abstract DSAParameters ExportParameters(bool includePrivateParameters);
328 
331  public abstract void ImportParameters(DSAParameters parameters);
332 
333  private static Exception DerivedClassMustOverride()
334  {
335  return new NotImplementedException(Environment.GetResourceString("NotSupported_SubclassOverride"));
336  }
337 
338  internal static Exception HashAlgorithmNameNullOrEmpty()
339  {
340  return new ArgumentException(Environment.GetResourceString("Cryptography_HashAlgorithmNameNullOrEmpty"), "hashAlgorithm");
341  }
342  }
343 }
The exception that is thrown when an error occurs during a cryptographic operation.
Converts a base data type to another base data type.
Definition: Convert.cs:10
virtual bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm)
Verifies that a digital signature is valid by calculating the hash value of the specified stream usin...
Definition: DSA.cs:210
static new DSA Create(string algName)
Creates the specified cryptographic object used to perform the asymmetric algorithm.
Definition: DSA.cs:22
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
byte [] J
Specifies the J parameter for the T:System.Security.Cryptography.DSA algorithm.
override string ToXmlString(bool includePrivateParameters)
Creates and returns an XML string representation of the current T:System.Security....
Definition: DSA.cs:297
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
abstract void ImportParameters(DSAParameters parameters)
When overridden in a derived class, imports the specified T:System.Security.Cryptography....
string Name
Gets the underlying string representation of the algorithm name.
byte [] P
Specifies the P parameter for the T:System.Security.Cryptography.DSA algorithm.
byte [] X
Specifies the X parameter for the T:System.Security.Cryptography.DSA algorithm.
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static new DSA Create()
Creates the default cryptographic object used to perform the asymmetric algorithm.
Definition: DSA.cs:14
byte [] Q
Specifies the Q parameter for the T:System.Security.Cryptography.DSA algorithm.
byte [] SignData(byte[] data, HashAlgorithmName hashAlgorithm)
Computes the hash value of the specified byte array using the specified hash algorithm and signs the ...
Definition: DSA.cs:69
Specifies the name of a cryptographic hash algorithm.
virtual byte [] HashData(Stream data, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified binary stream by using a s...
Definition: DSA.cs:56
static string ToBase64String(byte[] inArray)
Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded ...
Definition: Convert.cs:4413
byte [] Seed
Specifies the seed for the T:System.Security.Cryptography.DSA algorithm.
Represents the abstract base class from which all implementations of the Digital Signature Algorithm ...
Definition: DSA.cs:10
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
abstract DSAParameters ExportParameters(bool includePrivateParameters)
When overridden in a derived class, exports the T:System.Security.Cryptography.DSAParameters.
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Accesses the cryptography configuration information.
Definition: CryptoConfig.cs:17
Represents the XML object model for encoding security objects. This class cannot be inherited.
byte [] Y
Specifies the Y parameter for the T:System.Security.Cryptography.DSA algorithm.
Represents the abstract base class from which all implementations of asymmetric algorithms must inher...
virtual byte [] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified portion of a byte array by...
Definition: DSA.cs:46
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
byte [] G
Specifies the G parameter for the T:System.Security.Cryptography.DSA algorithm.
The exception that is thrown when one of the arguments provided to a method is not valid.
bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm)
Verifies that a digital signature is valid by calculating the hash value of the specified data using ...
Definition: DSA.cs:147
static unsafe byte [] FromBase64String(string s)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit un...
Definition: Convert.cs:4692
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
abstract bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
When overridden in a derived class, verifies the T:System.Security.Cryptography.DSA signature for the...
virtual byte [] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Computes the hash value of a portion of the specified byte array using the specified hash algorithm a...
Definition: DSA.cs:92
virtual byte [] SignData(Stream data, HashAlgorithmName hashAlgorithm)
Computes the hash value of the specified stream using the specified hash algorithm and signs the resu...
Definition: DSA.cs:122
virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm)
Verifies that a digital signature is valid by calculating the hash value of the data in a portion of ...
Definition: DSA.cs:173
override void FromXmlString(string xmlString)
Reconstructs a T:System.Security.Cryptography.DSA object from an XML string.
Definition: DSA.cs:232
int Counter
Specifies the counter for the T:System.Security.Cryptography.DSA algorithm.
abstract byte [] CreateSignature(byte[] rgbHash)
When overridden in a derived class, creates the T:System.Security.Cryptography.DSA signature for the ...
The exception that is thrown when a requested method or operation is not implemented.
Contains the typical parameters for the T:System.Security.Cryptography.DSA algorithm.
Definition: DSAParameters.cs:8
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16