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

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