Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Security / Cryptography / dsa.cs / 1 / dsa.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// DSA.cs
//
namespace System.Security.Cryptography {
using System.Text;
using System.Runtime.Serialization;
using System.Security.Util;
using System.Globalization;
// DSAParameters is serializable so that one could pass the public parameters
// across a remote call, but we explicitly make the private key X non-serializable
// so you cannot accidently send it along with the public parameters.
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DSAParameters {
public byte[] P;
public byte[] Q;
public byte[] G;
public byte[] Y;
public byte[] J;
[NonSerialized] public byte[] X;
public byte[] Seed;
public int Counter;
}
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class DSA : AsymmetricAlgorithm
{
//
// Extending this class allows us to know that you are really implementing
// an DSA key. This is required for anybody providing a new DSA key value
// implemention.
//
// The class provides no methods, fields or anything else. Its only purpose is
// as a heirarchy member for identification of the algorithm.
//
protected DSA() { }
//
// public methods
//
new static public DSA Create() {
return Create("System.Security.Cryptography.DSA");
}
new static public DSA Create(String algName) {
return (DSA) CryptoConfig.CreateFromName(algName);
}
abstract public byte[] CreateSignature(byte[] rgbHash);
abstract public bool VerifySignature(byte[] rgbHash, byte[] rgbSignature);
// We can provide a default implementation of FromXmlString because we require
// every DSA implementation to implement ImportParameters
// All we have to do here is parse the XML.
public override void FromXmlString(String xmlString) {
if (xmlString == null) throw new ArgumentNullException("xmlString");
DSAParameters dsaParams = new DSAParameters();
Parser p = new Parser(xmlString);
SecurityElement topElement = p.GetTopElement();
// P is always present
String pString = topElement.SearchForTextOfLocalName("P");
if (pString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","P"));
}
dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
// Q is always present
String qString = topElement.SearchForTextOfLocalName("Q");
if (qString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Q"));
}
dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
// G is always present
String gString = topElement.SearchForTextOfLocalName("G");
if (gString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","G"));
}
dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString));
// Y is always present
String yString = topElement.SearchForTextOfLocalName("Y");
if (yString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Y"));
}
dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));
// J is optional
String jString = topElement.SearchForTextOfLocalName("J");
if (jString != null) dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString));
// X is optional -- private key if present
String xString = topElement.SearchForTextOfLocalName("X");
if (xString != null) dsaParams.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(xString));
// Seed and PgenCounter are optional as a unit -- both present or both absent
String seedString = topElement.SearchForTextOfLocalName("Seed");
String pgenCounterString = topElement.SearchForTextOfLocalName("PgenCounter");
if ((seedString != null) && (pgenCounterString != null)) {
dsaParams.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(seedString));
dsaParams.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(pgenCounterString)));
} else if ((seedString != null) || (pgenCounterString != null)) {
if (seedString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Seed"));
} else {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","PgenCounter"));
}
}
ImportParameters(dsaParams);
}
// We can provide a default implementation of ToXmlString because we require
// every DSA implementation to implement ImportParameters
// If includePrivateParameters is false, this is just an XMLDSIG DSAKeyValue
// clause. If includePrivateParameters is true, then we extend DSAKeyValue with
// the other (private) elements.
public override String ToXmlString(bool includePrivateParameters) {
// From the XMLDSIG spec, RFC 3075, Section 6.4.1, a DSAKeyValue looks like this:
/*
*/
// we extend appropriately for private component X
DSAParameters dsaParams = this.ExportParameters(includePrivateParameters);
StringBuilder sb = new StringBuilder();
sb.Append("");
// Add P, Q, G and Y
sb.Append(""+Convert.ToBase64String(dsaParams.P)+"
");
sb.Append(""+Convert.ToBase64String(dsaParams.Q)+"
");
sb.Append(""+Convert.ToBase64String(dsaParams.G)+" ");
sb.Append(""+Convert.ToBase64String(dsaParams.Y)+" ");
// Add optional components if present
if (dsaParams.J != null) {
sb.Append(""+Convert.ToBase64String(dsaParams.J)+" ");
}
if ((dsaParams.Seed != null)) { // note we assume counter is correct if Seed is present
sb.Append(""+Convert.ToBase64String(dsaParams.Seed)+" ");
sb.Append(""+Convert.ToBase64String(Utils.ConvertIntToByteArray(dsaParams.Counter))+" ");
}
if (includePrivateParameters) {
// Add the private component
sb.Append(""+Convert.ToBase64String(dsaParams.X)+" ");
}
sb.Append(" ");
return(sb.ToString());
}
abstract public DSAParameters ExportParameters(bool includePrivateParameters);
abstract public void ImportParameters(DSAParameters parameters);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// DSA.cs
//
namespace System.Security.Cryptography {
using System.Text;
using System.Runtime.Serialization;
using System.Security.Util;
using System.Globalization;
// DSAParameters is serializable so that one could pass the public parameters
// across a remote call, but we explicitly make the private key X non-serializable
// so you cannot accidently send it along with the public parameters.
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DSAParameters {
public byte[] P;
public byte[] Q;
public byte[] G;
public byte[] Y;
public byte[] J;
[NonSerialized] public byte[] X;
public byte[] Seed;
public int Counter;
}
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class DSA : AsymmetricAlgorithm
{
//
// Extending this class allows us to know that you are really implementing
// an DSA key. This is required for anybody providing a new DSA key value
// implemention.
//
// The class provides no methods, fields or anything else. Its only purpose is
// as a heirarchy member for identification of the algorithm.
//
protected DSA() { }
//
// public methods
//
new static public DSA Create() {
return Create("System.Security.Cryptography.DSA");
}
new static public DSA Create(String algName) {
return (DSA) CryptoConfig.CreateFromName(algName);
}
abstract public byte[] CreateSignature(byte[] rgbHash);
abstract public bool VerifySignature(byte[] rgbHash, byte[] rgbSignature);
// We can provide a default implementation of FromXmlString because we require
// every DSA implementation to implement ImportParameters
// All we have to do here is parse the XML.
public override void FromXmlString(String xmlString) {
if (xmlString == null) throw new ArgumentNullException("xmlString");
DSAParameters dsaParams = new DSAParameters();
Parser p = new Parser(xmlString);
SecurityElement topElement = p.GetTopElement();
// P is always present
String pString = topElement.SearchForTextOfLocalName("P");
if (pString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","P"));
}
dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
// Q is always present
String qString = topElement.SearchForTextOfLocalName("Q");
if (qString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Q"));
}
dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
// G is always present
String gString = topElement.SearchForTextOfLocalName("G");
if (gString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","G"));
}
dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString));
// Y is always present
String yString = topElement.SearchForTextOfLocalName("Y");
if (yString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Y"));
}
dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));
// J is optional
String jString = topElement.SearchForTextOfLocalName("J");
if (jString != null) dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString));
// X is optional -- private key if present
String xString = topElement.SearchForTextOfLocalName("X");
if (xString != null) dsaParams.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(xString));
// Seed and PgenCounter are optional as a unit -- both present or both absent
String seedString = topElement.SearchForTextOfLocalName("Seed");
String pgenCounterString = topElement.SearchForTextOfLocalName("PgenCounter");
if ((seedString != null) && (pgenCounterString != null)) {
dsaParams.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(seedString));
dsaParams.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(pgenCounterString)));
} else if ((seedString != null) || (pgenCounterString != null)) {
if (seedString == null) {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","Seed"));
} else {
throw new CryptographicException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Cryptography_InvalidFromXmlString"),"DSA","PgenCounter"));
}
}
ImportParameters(dsaParams);
}
// We can provide a default implementation of ToXmlString because we require
// every DSA implementation to implement ImportParameters
// If includePrivateParameters is false, this is just an XMLDSIG DSAKeyValue
// clause. If includePrivateParameters is true, then we extend DSAKeyValue with
// the other (private) elements.
public override String ToXmlString(bool includePrivateParameters) {
// From the XMLDSIG spec, RFC 3075, Section 6.4.1, a DSAKeyValue looks like this:
/*
*/
// we extend appropriately for private component X
DSAParameters dsaParams = this.ExportParameters(includePrivateParameters);
StringBuilder sb = new StringBuilder();
sb.Append("");
// Add P, Q, G and Y
sb.Append(""+Convert.ToBase64String(dsaParams.P)+"
");
sb.Append(""+Convert.ToBase64String(dsaParams.Q)+"
");
sb.Append(""+Convert.ToBase64String(dsaParams.G)+" ");
sb.Append(""+Convert.ToBase64String(dsaParams.Y)+" ");
// Add optional components if present
if (dsaParams.J != null) {
sb.Append(""+Convert.ToBase64String(dsaParams.J)+" ");
}
if ((dsaParams.Seed != null)) { // note we assume counter is correct if Seed is present
sb.Append(""+Convert.ToBase64String(dsaParams.Seed)+" ");
sb.Append(""+Convert.ToBase64String(Utils.ConvertIntToByteArray(dsaParams.Counter))+" ");
}
if (includePrivateParameters) {
// Add the private component
sb.Append(""+Convert.ToBase64String(dsaParams.X)+" ");
}
sb.Append(" ");
return(sb.ToString());
}
abstract public DSAParameters ExportParameters(bool includePrivateParameters);
abstract public void ImportParameters(DSAParameters parameters);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FormatterConverter.cs
- IEnumerable.cs
- SurrogateEncoder.cs
- EmptyReadOnlyDictionaryInternal.cs
- HtmlHead.cs
- DPCustomTypeDescriptor.cs
- DBCommand.cs
- ThicknessAnimationBase.cs
- XmlSchemaChoice.cs
- XmlReaderSettings.cs
- DomNameTable.cs
- DecimalAnimationBase.cs
- NetDispatcherFaultException.cs
- SHA384Managed.cs
- DateTime.cs
- Exceptions.cs
- BrowserInteropHelper.cs
- SimpleExpression.cs
- ModelVisual3D.cs
- Pool.cs
- CustomMenuItemCollection.cs
- TransformConverter.cs
- StringStorage.cs
- MappingException.cs
- DPTypeDescriptorContext.cs
- DurableErrorHandler.cs
- XmlDataLoader.cs
- RadioButtonList.cs
- TimeZoneInfo.cs
- Line.cs
- DependencyPropertyKey.cs
- UIElement.cs
- JsonEncodingStreamWrapper.cs
- DependencyObjectPropertyDescriptor.cs
- HttpRuntime.cs
- DataSourceHelper.cs
- WindowVisualStateTracker.cs
- FeatureAttribute.cs
- DispatcherExceptionEventArgs.cs
- InvalidOleVariantTypeException.cs
- SystemMulticastIPAddressInformation.cs
- ImportCatalogPart.cs
- DelayedRegex.cs
- TextElement.cs
- CollectionBase.cs
- XsltQilFactory.cs
- SkinBuilder.cs
- DataGridViewDataConnection.cs
- Nullable.cs
- HttpException.cs
- DetailsViewDeleteEventArgs.cs
- SizeAnimationBase.cs
- RemotingException.cs
- Cursors.cs
- DoubleLinkListEnumerator.cs
- SafeUserTokenHandle.cs
- StructuredTypeInfo.cs
- SHA512.cs
- HtmlTableCell.cs
- WebBaseEventKeyComparer.cs
- BindingExpression.cs
- TreeViewImageIndexConverter.cs
- RadioButtonPopupAdapter.cs
- BindingNavigatorDesigner.cs
- FilterUserControlBase.cs
- DataGridRowDetailsEventArgs.cs
- ItemContainerGenerator.cs
- XmlWriterSettings.cs
- _SingleItemRequestCache.cs
- EncoderExceptionFallback.cs
- SqlClientPermission.cs
- AspCompat.cs
- StringCollectionMarkupSerializer.cs
- ModulesEntry.cs
- TargetControlTypeCache.cs
- UntypedNullExpression.cs
- DefaultTypeArgumentAttribute.cs
- SafeProcessHandle.cs
- ReadOnlyDataSourceView.cs
- XmlNamedNodeMap.cs
- listitem.cs
- _SpnDictionary.cs
- CodeTryCatchFinallyStatement.cs
- Pens.cs
- UnionExpr.cs
- UnsafeNativeMethods.cs
- SystemIPv6InterfaceProperties.cs
- ClientSettingsProvider.cs
- OdbcHandle.cs
- DataGridViewLinkCell.cs
- SqlTransaction.cs
- BaseValidator.cs
- WindowsRichEditRange.cs
- PrincipalPermission.cs
- ListItemCollection.cs
- MetadataItemSerializer.cs
- XmlTextReaderImplHelpers.cs
- MsmqIntegrationValidationBehavior.cs
- IntegerValidatorAttribute.cs
- DataBoundLiteralControl.cs