mscorlib(4.0.0.0) API with additions
StrongName.cs
1 using System.IO;
2 using System.Reflection;
5 using System.Security.Util;
6 
8 {
10  [Serializable]
11  [ComVisible(true)]
12  public sealed class StrongName : EvidenceBase, IIdentityPermissionFactory, IDelayEvaluatedEvidence
13  {
14  private StrongNamePublicKeyBlob m_publicKeyBlob;
15 
16  private string m_name;
17 
18  private Version m_version;
19 
20  [NonSerialized]
21  private RuntimeAssembly m_assembly;
22 
23  [NonSerialized]
24  private bool m_wasUsed;
25 
28  public StrongNamePublicKeyBlob PublicKey => m_publicKeyBlob;
29 
32  public string Name => m_name;
33 
36  public Version Version => m_version;
37 
38  bool IDelayEvaluatedEvidence.IsVerified
39  {
40  [SecurityCritical]
41  get
42  {
43  if (!(m_assembly != null))
44  {
45  return true;
46  }
47  return m_assembly.IsStrongNameVerified;
48  }
49  }
50 
51  bool IDelayEvaluatedEvidence.WasUsed
52  {
53  get
54  {
55  return m_wasUsed;
56  }
57  }
58 
59  internal StrongName()
60  {
61  }
62 
69  public StrongName(StrongNamePublicKeyBlob blob, string name, Version version)
70  : this(blob, name, version, null)
71  {
72  }
73 
74  internal StrongName(StrongNamePublicKeyBlob blob, string name, Version version, Assembly assembly)
75  {
76  if (name == null)
77  {
78  throw new ArgumentNullException("name");
79  }
80  if (string.IsNullOrEmpty(name))
81  {
82  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyStrongName"));
83  }
84  if (blob == null)
85  {
86  throw new ArgumentNullException("blob");
87  }
88  if (version == null)
89  {
90  throw new ArgumentNullException("version");
91  }
92  RuntimeAssembly runtimeAssembly = assembly as RuntimeAssembly;
93  if (assembly != null && runtimeAssembly == null)
94  {
95  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"), "assembly");
96  }
97  m_publicKeyBlob = blob;
98  m_name = name;
99  m_version = version;
100  m_assembly = runtimeAssembly;
101  }
102 
103  void IDelayEvaluatedEvidence.MarkUsed()
104  {
105  m_wasUsed = true;
106  }
107 
108  internal static bool CompareNames(string asmName, string mcName)
109  {
110  if (mcName.Length > 0 && mcName[mcName.Length - 1] == '*' && mcName.Length - 1 <= asmName.Length)
111  {
112  return string.Compare(mcName, 0, asmName, 0, mcName.Length - 1, StringComparison.OrdinalIgnoreCase) == 0;
113  }
114  return string.Compare(mcName, asmName, StringComparison.OrdinalIgnoreCase) == 0;
115  }
116 
121  {
122  return new StrongNameIdentityPermission(m_publicKeyBlob, m_name, m_version);
123  }
124 
127  public override EvidenceBase Clone()
128  {
129  return new StrongName(m_publicKeyBlob, m_name, m_version);
130  }
131 
134  public object Copy()
135  {
136  return Clone();
137  }
138 
139  internal SecurityElement ToXml()
140  {
141  SecurityElement securityElement = new SecurityElement("StrongName");
142  securityElement.AddAttribute("version", "1");
143  if (m_publicKeyBlob != null)
144  {
145  securityElement.AddAttribute("Key", Hex.EncodeHexString(m_publicKeyBlob.PublicKey));
146  }
147  if (m_name != null)
148  {
149  securityElement.AddAttribute("Name", m_name);
150  }
151  if (m_version != null)
152  {
153  securityElement.AddAttribute("Version", m_version.ToString());
154  }
155  return securityElement;
156  }
157 
158  internal void FromXml(SecurityElement element)
159  {
160  if (element == null)
161  {
162  throw new ArgumentNullException("element");
163  }
164  if (string.Compare(element.Tag, "StrongName", StringComparison.Ordinal) != 0)
165  {
166  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
167  }
168  m_publicKeyBlob = null;
169  m_version = null;
170  string text = element.Attribute("Key");
171  if (text != null)
172  {
173  m_publicKeyBlob = new StrongNamePublicKeyBlob(Hex.DecodeHexString(text));
174  }
175  m_name = element.Attribute("Name");
176  string text2 = element.Attribute("Version");
177  if (text2 != null)
178  {
179  m_version = new Version(text2);
180  }
181  }
182 
185  public override string ToString()
186  {
187  return ToXml().ToString();
188  }
189 
194  public override bool Equals(object o)
195  {
196  StrongName strongName = o as StrongName;
197  if (strongName != null && object.Equals(m_publicKeyBlob, strongName.m_publicKeyBlob) && object.Equals(m_name, strongName.m_name))
198  {
199  return object.Equals(m_version, strongName.m_version);
200  }
201  return false;
202  }
203 
206  public override int GetHashCode()
207  {
208  if (m_publicKeyBlob != null)
209  {
210  return m_publicKeyBlob.GetHashCode();
211  }
212  if (m_name != null || m_version != null)
213  {
214  return ((m_name != null) ? m_name.GetHashCode() : 0) + ((!(m_version == null)) ? m_version.GetHashCode() : 0);
215  }
216  return typeof(StrongName).GetHashCode();
217  }
218 
219  internal object Normalize()
220  {
221  MemoryStream memoryStream = new MemoryStream();
222  BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
223  binaryWriter.Write(m_publicKeyBlob.PublicKey);
224  binaryWriter.Write(m_version.Major);
225  binaryWriter.Write(m_name);
226  memoryStream.Position = 0L;
227  return memoryStream;
228  }
229  }
230 }
override int GetHashCode()
Returns a hash code for the current T:System.Version object.
Definition: Version.cs:425
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
object Copy()
Creates an equivalent copy of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:134
override string ToString()
Creates a string representation of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:185
Provides the strong name of a code assembly as evidence for policy evaluation. This class cannot be i...
Definition: StrongName.cs:12
override int GetHashCode()
Gets the hash code of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:206
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
StrongNamePublicKeyBlob PublicKey
Gets the T:System.Security.Permissions.StrongNamePublicKeyBlob of the current T:System....
Definition: StrongName.cs:28
Version Version
Gets the T:System.Version of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:36
Provides a base class from which all objects to be used as evidence must derive.
Definition: EvidenceBase.cs:12
Defines the method that creates a new identity permission.
override string ToString()
Converts the value of the current T:System.Version object to its equivalent T:System....
Definition: Version.cs:437
override bool Equals(object o)
Determines whether the specified strong name is equal to the current strong name.
Definition: StrongName.cs:194
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
Definition: MemoryStream.cs:13
int Major
Gets the value of the major component of the version number for the current T:System....
Definition: Version.cs:103
Represents the XML object model for encoding security objects. This class cannot be inherited.
StrongName(StrongNamePublicKeyBlob blob, string name, Version version)
Initializes a new instance of the T:System.Security.Policy.StrongName class with the strong name publ...
Definition: StrongName.cs:69
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
Defines the identity permission for strong names. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
IPermission CreateIdentityPermission(Evidence evidence)
Creates a T:System.Security.Permissions.StrongNameIdentityPermission that corresponds to the current ...
Definition: StrongName.cs:120
Defines the set of information that constitutes input to security policy decisions....
Definition: Evidence.cs:17
override EvidenceBase Clone()
Creates a new object that is a copy of the current instance.
Definition: StrongName.cs:127
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
Writes primitive types in binary to a stream and supports writing strings in a specific encoding.
Definition: BinaryWriter.cs:12
override long Position
Gets or sets the current position within the stream.
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
string Name
Gets the simple name of the current T:System.Security.Policy.StrongName.
Definition: StrongName.cs:32
Represents the public key information (called a blob) for a strong name. This class cannot be inherit...