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.2KB

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