ac3cac3e2c0691246e804cad28bb8898411e93f2
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / strongnamepublickeyblob.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // StrongNamePublicKeyBlob.cs
7 // 
8 // <OWNER>Microsoft</OWNER>
9 // 
10
11 namespace System.Security.Permissions
12 {
13     using System;
14     using System.Security.Util;
15     using System.Diagnostics.Contracts;
16
17     [System.Runtime.InteropServices.ComVisible(true)]
18     [Serializable] sealed public class StrongNamePublicKeyBlob
19     {
20         internal byte[] PublicKey;
21         
22         internal StrongNamePublicKeyBlob()
23         {
24         }
25         
26         public StrongNamePublicKeyBlob( byte[] publicKey )
27         {
28             if (publicKey == null)
29                 throw new ArgumentNullException( "PublicKey" );
30             Contract.EndContractBlock();
31         
32             this.PublicKey = new byte[publicKey.Length];
33             Array.Copy( publicKey, 0, this.PublicKey, 0, publicKey.Length );
34         }
35         
36         internal StrongNamePublicKeyBlob( String publicKey )
37         {
38             this.PublicKey = Hex.DecodeHexString( publicKey );
39         }        
40         
41         private static bool CompareArrays( byte[] first, byte[] second )
42         {
43             if (first.Length != second.Length)
44             {
45                 return false;
46             }
47             
48             int count = first.Length;
49             for (int i = 0; i < count; ++i)
50             {
51                 if (first[i] != second[i])
52                     return false;
53             }
54             
55             return true;
56         }
57                 
58         
59         internal bool Equals( StrongNamePublicKeyBlob blob )
60         {
61             if (blob == null)
62                 return false;
63             else 
64                 return CompareArrays( this.PublicKey, blob.PublicKey );
65         }
66
67         public override bool Equals( Object obj )
68         {
69             if (obj == null || !(obj is StrongNamePublicKeyBlob))
70                 return false;
71
72             return this.Equals( (StrongNamePublicKeyBlob)obj );
73         }
74
75         static private int GetByteArrayHashCode( byte[] baData )
76         {
77             if (baData == null)
78                 return 0;
79
80             int accumulator = 0;
81
82             for (int i = 0; i < baData.Length; ++i)
83             {
84                 accumulator = (accumulator << 8) ^ (int)baData[i] ^ (accumulator >> 24);
85             }
86
87             return accumulator;
88         }
89
90         public override int GetHashCode()
91         {
92             return GetByteArrayHashCode( PublicKey );
93         }
94
95         public override String ToString()
96         {
97             return Hex.EncodeHexString( PublicKey );
98         }
99     }
100 }