PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DotNet/PublicKeyBase.cs

https://bitbucket.org/0xd4d/dnlib
C# | 218 lines | 88 code | 21 blank | 109 comment | 34 complexity | dad7dc52dcf2daf8a122e3854340c3a8 MD5 | raw file
  1. /*
  2. Copyright (C) 2012-2013 de4dot@gmail.com
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. ???namespace dnlib.DotNet {
  21. /// <summary>
  22. /// Public key / public key token base class
  23. /// </summary>
  24. public abstract class PublicKeyBase {
  25. /// <summary>
  26. /// The key data
  27. /// </summary>
  28. protected byte[] data;
  29. /// <summary>
  30. /// Returns <c>true</c> if <see cref="Data"/> is <c>null</c> or empty
  31. /// </summary>
  32. public bool IsNullOrEmpty {
  33. get { return data == null || data.Length == 0; }
  34. }
  35. /// <summary>
  36. /// Returns <c>true</c> if <see cref="Data"/> is <c>null</c>
  37. /// </summary>
  38. public bool IsNull {
  39. get { return data == null; }
  40. }
  41. /// <summary>
  42. /// Gets/sets key data
  43. /// </summary>
  44. public virtual byte[] Data {
  45. get { return data; }
  46. set { data = value; }
  47. }
  48. /// <summary>
  49. /// Default constructor
  50. /// </summary>
  51. protected PublicKeyBase() {
  52. }
  53. /// <summary>
  54. /// Constructor
  55. /// </summary>
  56. /// <param name="data">Key data</param>
  57. protected PublicKeyBase(byte[] data) {
  58. this.data = data;
  59. }
  60. /// <summary>
  61. /// Constructor
  62. /// </summary>
  63. /// <param name="hexString">Key data as a hex string or the string <c>"null"</c>
  64. /// to set key data to <c>null</c></param>
  65. protected PublicKeyBase(string hexString) {
  66. this.data = Parse(hexString);
  67. }
  68. static byte[] Parse(string hexString) {
  69. if (hexString == null || hexString == "null")
  70. return null;
  71. return Utils.ParseBytes(hexString);
  72. }
  73. /// <summary>
  74. /// Checks whether a public key or token is null or empty
  75. /// </summary>
  76. /// <param name="a">Public key or token instance</param>
  77. public static bool IsNullOrEmpty2(PublicKeyBase a) {
  78. return a == null || a.data == null || a.data.Length == 0;
  79. }
  80. /// <summary>
  81. /// Returns a <see cref="PublicKeyToken"/>
  82. /// </summary>
  83. /// <param name="pkb">A <see cref="PublicKey"/> or a <see cref="PublicKeyToken"/> instance</param>
  84. public static PublicKeyToken ToPublicKeyToken(PublicKeyBase pkb) {
  85. var pkt = pkb as PublicKeyToken;
  86. if (pkt != null)
  87. return pkt;
  88. var pk = pkb as PublicKey;
  89. if (pk != null)
  90. return pk.Token;
  91. return null;
  92. }
  93. /// <summary>
  94. /// Compares two <see cref="PublicKeyBase"/>s as <see cref="PublicKeyToken"/>s
  95. /// </summary>
  96. /// <param name="a">First</param>
  97. /// <param name="b">Second</param>
  98. /// <returns>&lt; 0 if a &lt; b, 0 if a == b, &gt; 0 if a &gt; b</returns>
  99. public static int TokenCompareTo(PublicKeyBase a, PublicKeyBase b) {
  100. if (a == b)
  101. return 0;
  102. return TokenCompareTo(ToPublicKeyToken(a), ToPublicKeyToken(b));
  103. }
  104. /// <summary>
  105. /// Checks whether two public key tokens are equal
  106. /// </summary>
  107. /// <param name="a">First</param>
  108. /// <param name="b">Second</param>
  109. /// <returns><c>true</c> if same, <c>false</c> otherwise</returns>
  110. public static bool TokenEquals(PublicKeyBase a, PublicKeyBase b) {
  111. return TokenCompareTo(a, b) == 0;
  112. }
  113. static readonly byte[] EmptyByteArray = new byte[0];
  114. /// <summary>
  115. /// Compares two <see cref="PublicKeyToken"/>s
  116. /// </summary>
  117. /// <param name="a">First</param>
  118. /// <param name="b">Second</param>
  119. /// <returns>&lt; 0 if a &lt; b, 0 if a == b, &gt; 0 if a &gt; b</returns>
  120. public static int TokenCompareTo(PublicKeyToken a, PublicKeyToken b) {
  121. if (a == b)
  122. return 0;
  123. return TokenCompareTo(a == null ? null : a.data, b == null ? null : b.data);
  124. }
  125. static int TokenCompareTo(byte[] a, byte[] b) {
  126. return Utils.CompareTo(a ?? EmptyByteArray, b ?? EmptyByteArray);
  127. }
  128. /// <summary>
  129. /// Checks whether two public key tokens are equal
  130. /// </summary>
  131. /// <param name="a">First</param>
  132. /// <param name="b">Second</param>
  133. /// <returns><c>true</c> if same, <c>false</c> otherwise</returns>
  134. public static bool TokenEquals(PublicKeyToken a, PublicKeyToken b) {
  135. return TokenCompareTo(a, b) == 0;
  136. }
  137. /// <summary>
  138. /// Gets the public key token hash code
  139. /// </summary>
  140. /// <param name="a">Public key or token</param>
  141. /// <returns>The hash code</returns>
  142. public static int GetHashCodeToken(PublicKeyBase a) {
  143. return GetHashCode(ToPublicKeyToken(a));
  144. }
  145. /// <summary>
  146. /// Gets the public key token hash code
  147. /// </summary>
  148. /// <param name="a">Public key token</param>
  149. /// <returns>The hash code</returns>
  150. public static int GetHashCode(PublicKeyToken a) {
  151. if (a == null)
  152. return 0;
  153. return Utils.GetHashCode(a.Data);
  154. }
  155. /// <summary>
  156. /// Creates a <see cref="PublicKey"/>
  157. /// </summary>
  158. /// <param name="data">Public key data or <c>null</c></param>
  159. /// <returns>A new <see cref="PublicKey"/> instance or <c>null</c> if <paramref name="data"/>
  160. /// was <c>null</c></returns>
  161. public static PublicKey CreatePublicKey(byte[] data) {
  162. if (data == null)
  163. return null;
  164. return new PublicKey(data);
  165. }
  166. /// <summary>
  167. /// Creates a <see cref="PublicKeyToken"/>
  168. /// </summary>
  169. /// <param name="data">Public key token data or <c>null</c></param>
  170. /// <returns>A new <see cref="PublicKeyToken"/> instance or <c>null</c> if <paramref name="data"/>
  171. /// was <c>null</c></returns>
  172. public static PublicKeyToken CreatePublicKeyToken(byte[] data) {
  173. if (data == null)
  174. return null;
  175. return new PublicKeyToken(data);
  176. }
  177. /// <summary>
  178. /// Gets the raw public key / public key token byte array
  179. /// </summary>
  180. /// <param name="pkb">The instance or <c>null</c></param>
  181. /// <returns>Raw public key / public key token data or <c>null</c></returns>
  182. public static byte[] GetRawData(PublicKeyBase pkb) {
  183. if (pkb == null)
  184. return null;
  185. return pkb.Data;
  186. }
  187. /// <inheritdoc/>
  188. public override string ToString() {
  189. if (IsNullOrEmpty)
  190. return "null";
  191. return Utils.ToHex(data, false);
  192. }
  193. }
  194. }