The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringHelper.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. namespace Teknik.Utilities
  7. {
  8. public static class StringHelper
  9. {
  10. public static string RandomString(int length)
  11. {
  12. return RandomString(length, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
  13. }
  14. public static string RandomString(int length, string allowedChars)
  15. {
  16. const int byteSize = 0x100;
  17. var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
  18. if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize));
  19. // Guid.NewGuid and System.Random are not particularly random. By using a
  20. // cryptographically-secure random number generator, the caller is always
  21. // protected, regardless of use.
  22. using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
  23. {
  24. var result = new StringBuilder();
  25. var buf = new byte[128];
  26. while (result.Length < length)
  27. {
  28. rng.GetBytes(buf);
  29. for (var i = 0; i < buf.Length && result.Length < length; ++i)
  30. {
  31. // Divide the byte into allowedCharSet-sized groups. If the
  32. // random value falls into the last group and the last group is
  33. // too small to choose from the entire allowedCharSet, ignore
  34. // the value in order to avoid biasing the result.
  35. var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
  36. if (outOfRangeStart <= buf[i]) continue;
  37. result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
  38. }
  39. }
  40. return result.ToString();
  41. }
  42. }
  43. public static string GetBytesReadable(long i)
  44. {
  45. // Get absolute value
  46. long absolute_i = (i < 0 ? -i : i);
  47. // Determine the suffix and readable value
  48. string suffix;
  49. double readable;
  50. if (absolute_i >= 0x1000000000000000) // Exabyte
  51. {
  52. suffix = "EB";
  53. readable = (i >> 50);
  54. }
  55. else if (absolute_i >= 0x4000000000000) // Petabyte
  56. {
  57. suffix = "PB";
  58. readable = (i >> 40);
  59. }
  60. else if (absolute_i >= 0x10000000000) // Terabyte
  61. {
  62. suffix = "TB";
  63. readable = (i >> 30);
  64. }
  65. else if (absolute_i >= 0x40000000) // Gigabyte
  66. {
  67. suffix = "GB";
  68. readable = (i >> 20);
  69. }
  70. else if (absolute_i >= 0x100000) // Megabyte
  71. {
  72. suffix = "MB";
  73. readable = (i >> 10);
  74. }
  75. else if (absolute_i >= 0x400) // Kilobyte
  76. {
  77. suffix = "KB";
  78. readable = i;
  79. }
  80. else
  81. {
  82. return i.ToString("0 B"); // Byte
  83. }
  84. // Divide by 1024 to get fractional value
  85. readable = (readable / 1024);
  86. // Return formatted number with suffix
  87. return readable.ToString("0.### ") + suffix;
  88. }
  89. public static KeyValuePair<string, string> ParseBasicAuthHeader(string value)
  90. {
  91. return ParseBasicAuthHeader(value, Encoding.UTF8);
  92. }
  93. public static KeyValuePair<string, string> ParseBasicAuthHeader(string value, Encoding encoding)
  94. {
  95. KeyValuePair<string, string> result = new KeyValuePair<string, string>();
  96. if (!string.IsNullOrEmpty(value))
  97. {
  98. byte[] rawVal = Convert.FromBase64String(value);
  99. string stringVal = encoding.GetString(rawVal);
  100. string[] parts = stringVal.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  101. if (parts.Length > 1)
  102. {
  103. result = new KeyValuePair<string, string>(parts[0], parts[1]);
  104. }
  105. }
  106. return result;
  107. }
  108. }
  109. }