MongoUtils.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. using System.Security.Cryptography;
  19. using System.Text;
  20. using MongoDB.Bson;
  21. namespace MongoDB.Driver
  22. {
  23. /// <summary>
  24. /// Various static utility methods.
  25. /// </summary>
  26. public static class MongoUtils
  27. {
  28. // public static methods
  29. /// <summary>
  30. /// Gets the MD5 hash of a string.
  31. /// </summary>
  32. /// <param name="text">The string to get the MD5 hash of.</param>
  33. /// <returns>The MD5 hash.</returns>
  34. public static string Hash(string text)
  35. {
  36. using (var md5 = MD5.Create())
  37. {
  38. var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
  39. return BsonUtils.ToHexString(hash);
  40. }
  41. }
  42. /// <summary>
  43. /// Creates a TimeSpan from microseconds.
  44. /// </summary>
  45. /// <param name="microseconds">The microseconds.</param>
  46. /// <returns>The TimeSpan.</returns>
  47. public static TimeSpan TimeSpanFromMicroseconds(long microseconds)
  48. {
  49. return TimeSpan.FromTicks(microseconds * 10); // there are 10 ticks in a microsecond
  50. }
  51. /// <summary>
  52. /// Converts a string to camel case by lower casing the first letter (only the first letter is modified).
  53. /// </summary>
  54. /// <param name="value">The string to camel case.</param>
  55. /// <returns>The camel cased string.</returns>
  56. public static string ToCamelCase(string value)
  57. {
  58. return value.Length == 0 ? "" : value.Substring(0, 1).ToLower() + value.Substring(1);
  59. }
  60. // internal methods
  61. /// <summary>
  62. /// Should only be used when the safety of the data cannot be guaranteed. For instance,
  63. /// when the secure string is a password used in a plain text protocol.
  64. /// </summary>
  65. /// <param name="secureString">The secure string.</param>
  66. /// <returns>The CLR string.</returns>
  67. internal static string ToInsecureString(SecureString secureString)
  68. {
  69. if (secureString == null || secureString.Length == 0)
  70. {
  71. return "";
  72. }
  73. else
  74. {
  75. #if NET452
  76. var secureStringIntPtr = Marshal.SecureStringToGlobalAllocUnicode(secureString);
  77. #else
  78. var secureStringIntPtr = SecureStringMarshal.SecureStringToGlobalAllocUnicode(secureString);
  79. #endif
  80. try
  81. {
  82. return Marshal.PtrToStringUni(secureStringIntPtr, secureString.Length);
  83. }
  84. finally
  85. {
  86. Marshal.ZeroFreeGlobalAllocUnicode(secureStringIntPtr);
  87. }
  88. }
  89. }
  90. }
  91. }