2
0

SecureHashCodeProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Collections.SecureHashCodeProvider.cs
  3. //
  4. // Authors:
  5. // Sergey Chaban ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. // Copyright 2012 Xamarin, Inc (http://xamarin.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Globalization;
  34. namespace System.Web.Util
  35. {
  36. class SecureHashCodeProvider : IHashCodeProvider
  37. {
  38. static readonly SecureHashCodeProvider singletonInvariant = new SecureHashCodeProvider (CultureInfo.InvariantCulture);
  39. static SecureHashCodeProvider singleton;
  40. static readonly object sync = new object ();
  41. static readonly int seed;
  42. TextInfo m_text; // must match MS name for serialization
  43. public static SecureHashCodeProvider Default {
  44. get {
  45. lock (sync) {
  46. if (singleton == null) {
  47. singleton = new SecureHashCodeProvider ();
  48. } else if (singleton.m_text == null) {
  49. if (!AreEqual (CultureInfo.CurrentCulture, CultureInfo.InvariantCulture))
  50. singleton = new SecureHashCodeProvider ();
  51. } else if (!AreEqual (singleton.m_text, CultureInfo.CurrentCulture)) {
  52. singleton = new SecureHashCodeProvider ();
  53. }
  54. return singleton;
  55. }
  56. }
  57. }
  58. public static SecureHashCodeProvider DefaultInvariant {
  59. get { return singletonInvariant; }
  60. }
  61. static SecureHashCodeProvider ()
  62. {
  63. // It should be enough to fend off the attack described in
  64. // https://bugzilla.novell.com/show_bug.cgi?id=739119
  65. // In order to predict value of the seed, the attacker would have to know the exact time when
  66. // the server process started and since it's a remote attack, this is next to impossible.
  67. // Using milliseconds instead of ticks here would make it easier for the attackers since there
  68. // would only be as many as 1000 possible values
  69. seed = (int)DateTime.UtcNow.Ticks;
  70. }
  71. // Public instance constructor
  72. public SecureHashCodeProvider ()
  73. {
  74. CultureInfo culture = CultureInfo.CurrentCulture;
  75. if (!AreEqual (culture, CultureInfo.InvariantCulture))
  76. m_text = CultureInfo.CurrentCulture.TextInfo;
  77. }
  78. public SecureHashCodeProvider (CultureInfo culture)
  79. {
  80. if (culture == null)
  81. throw new ArgumentNullException ("culture");
  82. if (!AreEqual (culture, CultureInfo.InvariantCulture))
  83. m_text = culture.TextInfo;
  84. }
  85. static bool AreEqual (CultureInfo a, CultureInfo b)
  86. {
  87. return a.LCID == b.LCID;
  88. }
  89. static bool AreEqual (TextInfo info, CultureInfo culture)
  90. {
  91. return info.LCID == culture.LCID;
  92. }
  93. public int GetHashCode (object obj)
  94. {
  95. if (obj == null)
  96. throw new ArgumentNullException ("obj");
  97. string str = obj as string;
  98. if (str == null)
  99. return obj.GetHashCode ();
  100. int h = seed;
  101. char c;
  102. if ((m_text != null) && !AreEqual (m_text, CultureInfo.InvariantCulture)) {
  103. str = m_text.ToLower (str);
  104. for (int i = 0; i < str.Length; i++) {
  105. c = str [i];
  106. h = h * 31 + c;
  107. }
  108. } else {
  109. for (int i = 0; i < str.Length; i++) {
  110. c = Char.ToLower (str [i], CultureInfo.InvariantCulture);
  111. h = h * 31 + c;
  112. }
  113. }
  114. return h;
  115. }
  116. }
  117. }