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.
117 lines
4.3 KiB
117 lines
4.3 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Web; |
|
|
|
namespace Teknik.Utilities |
|
{ |
|
public static class StringHelper |
|
{ |
|
public static string RandomString(int length) |
|
{ |
|
return RandomString(length, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); |
|
} |
|
|
|
public static string RandomString(int length, string allowedChars) |
|
{ |
|
const int byteSize = 0x100; |
|
var allowedCharSet = new HashSet<char>(allowedChars).ToArray(); |
|
if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize)); |
|
|
|
// Guid.NewGuid and System.Random are not particularly random. By using a |
|
// cryptographically-secure random number generator, the caller is always |
|
// protected, regardless of use. |
|
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) |
|
{ |
|
var result = new StringBuilder(); |
|
var buf = new byte[128]; |
|
while (result.Length < length) |
|
{ |
|
rng.GetBytes(buf); |
|
for (var i = 0; i < buf.Length && result.Length < length; ++i) |
|
{ |
|
// Divide the byte into allowedCharSet-sized groups. If the |
|
// random value falls into the last group and the last group is |
|
// too small to choose from the entire allowedCharSet, ignore |
|
// the value in order to avoid biasing the result. |
|
var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length); |
|
if (outOfRangeStart <= buf[i]) continue; |
|
result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]); |
|
} |
|
} |
|
return result.ToString(); |
|
} |
|
} |
|
|
|
public static string GetBytesReadable(long i) |
|
{ |
|
// Get absolute value |
|
long absolute_i = (i < 0 ? -i : i); |
|
// Determine the suffix and readable value |
|
string suffix; |
|
double readable; |
|
if (absolute_i >= 0x1000000000000000) // Exabyte |
|
{ |
|
suffix = "EB"; |
|
readable = (i >> 50); |
|
} |
|
else if (absolute_i >= 0x4000000000000) // Petabyte |
|
{ |
|
suffix = "PB"; |
|
readable = (i >> 40); |
|
} |
|
else if (absolute_i >= 0x10000000000) // Terabyte |
|
{ |
|
suffix = "TB"; |
|
readable = (i >> 30); |
|
} |
|
else if (absolute_i >= 0x40000000) // Gigabyte |
|
{ |
|
suffix = "GB"; |
|
readable = (i >> 20); |
|
} |
|
else if (absolute_i >= 0x100000) // Megabyte |
|
{ |
|
suffix = "MB"; |
|
readable = (i >> 10); |
|
} |
|
else if (absolute_i >= 0x400) // Kilobyte |
|
{ |
|
suffix = "KB"; |
|
readable = i; |
|
} |
|
else |
|
{ |
|
return i.ToString("0 B"); // Byte |
|
} |
|
// Divide by 1024 to get fractional value |
|
readable = (readable / 1024); |
|
// Return formatted number with suffix |
|
return readable.ToString("0.### ") + suffix; |
|
} |
|
|
|
public static KeyValuePair<string, string> ParseBasicAuthHeader(string value) |
|
{ |
|
return ParseBasicAuthHeader(value, Encoding.UTF8); |
|
} |
|
|
|
public static KeyValuePair<string, string> ParseBasicAuthHeader(string value, Encoding encoding) |
|
{ |
|
KeyValuePair<string, string> result = new KeyValuePair<string, string>(); |
|
|
|
if (!string.IsNullOrEmpty(value)) |
|
{ |
|
byte[] rawVal = Convert.FromBase64String(value); |
|
string stringVal = encoding.GetString(rawVal); |
|
string[] parts = stringVal.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); |
|
if (parts.Length > 1) |
|
{ |
|
result = new KeyValuePair<string, string>(parts[0], parts[1]); |
|
} |
|
} |
|
|
|
return result; |
|
} |
|
} |
|
} |