Files
Dec2017-Script-Dump/mscorlib/System/Security/Policy/Hash.cs
T
2026-06-04 11:42:34 +02:00

223 lines
8.7 KiB
C#

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
namespace System.Security.Policy
{
/// <summary>Provides evidence about the hash value for an assembly. This class cannot be inherited.</summary>
// Token: 0x0200058D RID: 1421
[ComVisible(true)]
[Serializable]
public sealed class Hash : ISerializable, IBuiltInEvidence
{
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Policy.Hash" /> class.</summary>
/// <param name="assembly">The <see cref="T:System.Reflection.Assembly" /> for which to compute the hash value. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="assembly" /> parameter is null. </exception>
// Token: 0x06003400 RID: 13312 RVA: 0x000B2D08 File Offset: 0x000B0F08
public Hash(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException("assembly");
}
this.assembly = assembly;
}
// Token: 0x06003401 RID: 13313 RVA: 0x000B2D28 File Offset: 0x000B0F28
internal Hash()
{
}
// Token: 0x06003402 RID: 13314 RVA: 0x000B2D30 File Offset: 0x000B0F30
internal Hash(SerializationInfo info, StreamingContext context)
{
this.data = (byte[])info.GetValue("RawData", typeof(byte[]));
}
// Token: 0x06003403 RID: 13315 RVA: 0x000B2D64 File Offset: 0x000B0F64
int IBuiltInEvidence.GetRequiredSize(bool verbose)
{
return (!verbose) ? 0 : 5;
}
// Token: 0x06003404 RID: 13316 RVA: 0x000B2D74 File Offset: 0x000B0F74
[MonoTODO("IBuiltInEvidence")]
int IBuiltInEvidence.InitFromBuffer(char[] buffer, int position)
{
return 0;
}
// Token: 0x06003405 RID: 13317 RVA: 0x000B2D78 File Offset: 0x000B0F78
[MonoTODO("IBuiltInEvidence")]
int IBuiltInEvidence.OutputToBuffer(char[] buffer, int position, bool verbose)
{
return 0;
}
/// <summary>Gets the <see cref="T:System.Security.Cryptography.MD5" /> hash value for the assembly.</summary>
/// <returns>A byte array that represents the <see cref="T:System.Security.Cryptography.MD5" /> hash value for the assembly.</returns>
// Token: 0x17000A3E RID: 2622
// (get) Token: 0x06003406 RID: 13318 RVA: 0x000B2D7C File Offset: 0x000B0F7C
public byte[] MD5
{
get
{
if (this._md5 != null)
{
return this._md5;
}
if (this.assembly == null && this._sha1 != null)
{
string text = Locale.GetText("No assembly data. This instance was initialized with an MSHA1 digest value.");
throw new SecurityException(text);
}
HashAlgorithm hashAlgorithm = global::System.Security.Cryptography.MD5.Create();
this._md5 = this.GenerateHash(hashAlgorithm);
return this._md5;
}
}
/// <summary>Gets the <see cref="T:System.Security.Cryptography.SHA1" /> hash value for the assembly.</summary>
/// <returns>A byte array that represents the <see cref="T:System.Security.Cryptography.SHA1" /> hash value for the assembly.</returns>
// Token: 0x17000A3F RID: 2623
// (get) Token: 0x06003407 RID: 13319 RVA: 0x000B2DDC File Offset: 0x000B0FDC
public byte[] SHA1
{
get
{
if (this._sha1 != null)
{
return this._sha1;
}
if (this.assembly == null && this._md5 != null)
{
string text = Locale.GetText("No assembly data. This instance was initialized with an MD5 digest value.");
throw new SecurityException(text);
}
HashAlgorithm hashAlgorithm = global::System.Security.Cryptography.SHA1.Create();
this._sha1 = this.GenerateHash(hashAlgorithm);
return this._sha1;
}
}
/// <summary>Computes the hash value for the assembly using the specified hash algorithm.</summary>
/// <returns>A byte array that represents the hash value for the assembly.</returns>
/// <param name="hashAlg">The hash algorithm to use to compute the hash value for the assembly. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="hashAlg" /> parameter is null. </exception>
/// <exception cref="T:System.Security.SecurityException">The hash value for the assembly cannot be generated.</exception>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// <IPermission class="System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
// Token: 0x06003408 RID: 13320 RVA: 0x000B2E3C File Offset: 0x000B103C
public byte[] GenerateHash(HashAlgorithm hashAlg)
{
if (hashAlg == null)
{
throw new ArgumentNullException("hashAlg");
}
return hashAlg.ComputeHash(this.GetData());
}
/// <summary>Gets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> object with the parameter name and additional exception information.</summary>
/// <param name="info">The object that holds the serialized object data. </param>
/// <param name="context">The contextual information about the source or destination. </param>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
/// </PermissionSet>
// Token: 0x06003409 RID: 13321 RVA: 0x000B2E5C File Offset: 0x000B105C
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException("info");
}
info.AddValue("RawData", this.GetData());
}
/// <summary>Returns a string representation of the current <see cref="T:System.Security.Policy.Hash" />.</summary>
/// <returns>A representation of the current <see cref="T:System.Security.Policy.Hash" />.</returns>
// Token: 0x0600340A RID: 13322 RVA: 0x000B2E8C File Offset: 0x000B108C
public override string ToString()
{
SecurityElement securityElement = new SecurityElement(base.GetType().FullName);
securityElement.AddAttribute("version", "1");
StringBuilder stringBuilder = new StringBuilder();
byte[] array = this.GetData();
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append(array[i].ToString("X2"));
}
securityElement.AddChild(new SecurityElement("RawData", stringBuilder.ToString()));
return securityElement.ToString();
}
// Token: 0x0600340B RID: 13323 RVA: 0x000B2F10 File Offset: 0x000B1110
private byte[] GetData()
{
if (this.assembly == null && this.data == null)
{
string text = Locale.GetText("No assembly data.");
throw new SecurityException(text);
}
if (this.data == null)
{
FileStream fileStream = new FileStream(this.assembly.Location, FileMode.Open, FileAccess.Read);
this.data = new byte[fileStream.Length];
fileStream.Read(this.data, 0, (int)fileStream.Length);
}
return this.data;
}
/// <summary>Creates a <see cref="T:System.Security.Policy.Hash" /> object containing an <see cref="T:System.Security.Cryptography.MD5" /> hash value.</summary>
/// <returns>A <see cref="T:System.Security.Policy.Hash" /> object containing the hash value provided by the <paramref name="md5" /> parameter.</returns>
/// <param name="md5">A byte array containing an <see cref="T:System.Security.Cryptography.MD5" /> hash value.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="md5" /> parameter is null. </exception>
// Token: 0x0600340C RID: 13324 RVA: 0x000B2F90 File Offset: 0x000B1190
public static Hash CreateMD5(byte[] md5)
{
if (md5 == null)
{
throw new ArgumentNullException("md5");
}
return new Hash
{
_md5 = md5
};
}
/// <summary>Creates a <see cref="T:System.Security.Policy.Hash" /> object containing an <see cref="T:System.Security.Cryptography.SHA1" /> hash value.</summary>
/// <returns>A <see cref="T:System.Security.Policy.Hash" /> object containing the hash value provided by the <paramref name="sha1" /> parameter.</returns>
/// <param name="sha1">A byte array containing a <see cref="T:System.Security.Cryptography.SHA1" /> hash value.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="sha1" /> parameter is null. </exception>
// Token: 0x0600340D RID: 13325 RVA: 0x000B2FBC File Offset: 0x000B11BC
public static Hash CreateSHA1(byte[] sha1)
{
if (sha1 == null)
{
throw new ArgumentNullException("sha1");
}
return new Hash
{
_sha1 = sha1
};
}
// Token: 0x04001551 RID: 5457
private Assembly assembly;
// Token: 0x04001552 RID: 5458
private byte[] data;
// Token: 0x04001553 RID: 5459
internal byte[] _md5;
// Token: 0x04001554 RID: 5460
internal byte[] _sha1;
}
}