Browse Source

Moved StringHelper and Byte extensions to Utility project.

tags/2.0.6
Teknikode 2 years ago
parent
commit
56362057be

+ 19
- 1
Teknik.sln View File

@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Teknik", "Teknik\Teknik.csproj", "{B20317CD-76C6-4A7B-BCE1-E4BEF8E4F964}"
EndProject
@@ -13,6 +13,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerMaint", "ServerMaint\ServerMaint.csproj", "{E08975F9-1B84-41B0-875A-CEC9778C4F9E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utilities", "Utilities", "{9408DBDE-D7DF-45EE-AAEB-821BABAA6152}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{783361EC-DCD6-4A34-8479-5476DF752C34}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utilities", "Utilities\Utilities\Utilities.csproj", "{F45DE6FC-3754-4954-A20A-4277362CC6C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -38,8 +44,20 @@ Global
{E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|Any CPU.Build.0 = Release|Any CPU
{E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|x64.ActiveCfg = Release|Any CPU
{E08975F9-1B84-41B0-875A-CEC9778C4F9E}.Release|x64.Build.0 = Release|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Debug|x64.ActiveCfg = Debug|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Debug|x64.Build.0 = Debug|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Release|Any CPU.Build.0 = Release|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Release|x64.ActiveCfg = Release|Any CPU
{F45DE6FC-3754-4954-A20A-4277362CC6C1}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E08975F9-1B84-41B0-875A-CEC9778C4F9E} = {783361EC-DCD6-4A34-8479-5476DF752C34}
{F45DE6FC-3754-4954-A20A-4277362CC6C1} = {9408DBDE-D7DF-45EE-AAEB-821BABAA6152}
EndGlobalSection
EndGlobal

+ 4
- 3
Teknik/Areas/API/Controllers/APIv1Controller.cs View File

@@ -14,6 +14,7 @@ using System.Text;
using Teknik.Areas.Shortener.Models;
using nClam;
using Teknik.Filters;
using Teknik.Utilities;

namespace Teknik.Areas.API.Controllers
{
@@ -98,11 +99,11 @@ namespace Teknik.Areas.API.Controllers
// Generate key and iv if empty
if (string.IsNullOrEmpty(key))
{
key = Utility.RandomString(keySize / 8);
key = StringHelper.RandomString(keySize / 8);
}
if (string.IsNullOrEmpty(iv))
{
iv = Utility.RandomString(blockSize / 8);
iv = StringHelper.RandomString(blockSize / 8);
}

data = AES.Encrypt(fileData, key, iv);
@@ -120,7 +121,7 @@ namespace Teknik.Areas.API.Controllers
// Generate delete key if asked to
if (genDeletionKey)
{
string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
string delKey = StringHelper.RandomString(Config.UploadConfig.DeleteKeyLength);
upload.DeleteKey = delKey;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();

+ 1
- 1
Teknik/Areas/Paste/Controllers/PasteController.cs View File

@@ -14,7 +14,7 @@ using Teknik.Controllers;
using Teknik.Filters;
using Teknik.Helpers;
using Teknik.Models;
using Teknik.Utilities;

namespace Teknik.Areas.Paste.Controllers
{

+ 5
- 4
Teknik/Areas/Paste/PasteHelper.cs View File

@@ -6,6 +6,7 @@ using System.Web;
using Teknik.Configuration;
using Teknik.Helpers;
using Teknik.Models;
using Teknik.Utilities;

namespace Teknik.Areas.Paste
{
@@ -21,10 +22,10 @@ namespace Teknik.Areas.Paste
paste.Views = 0;

// Generate random url
string url = Utility.RandomString(config.PasteConfig.UrlLength);
string url = StringHelper.RandomString(config.PasteConfig.UrlLength);
while (db.Pastes.Where(p => p.Url == url).FirstOrDefault() != null)
{
url = Utility.RandomString(config.PasteConfig.UrlLength);
url = StringHelper.RandomString(config.PasteConfig.UrlLength);
}
paste.Url = url;

@@ -58,8 +59,8 @@ namespace Teknik.Areas.Paste
// Set the hashed password if one is provided and encrypt stuff
if (!string.IsNullOrEmpty(password))
{
string key = Utility.RandomString(config.PasteConfig.KeySize / 8);
string iv = Utility.RandomString(config.PasteConfig.BlockSize / 16);
string key = StringHelper.RandomString(config.PasteConfig.KeySize / 8);
string iv = StringHelper.RandomString(config.PasteConfig.BlockSize / 16);
paste.HashedPassword = SHA384.Hash(key, password).ToHex();

// Encrypt Content

+ 3
- 2
Teknik/Areas/Shortener/Shortener.cs View File

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Teknik.Areas.Shortener.Models;
using Teknik.Helpers;
using Teknik.Models;
using Teknik.Utilities;

namespace Teknik.Areas.Shortener
{
@@ -16,10 +17,10 @@ namespace Teknik.Areas.Shortener
TeknikEntities db = new TeknikEntities();
// Generate the shortened url
string shortUrl = Utility.RandomString(length);
string shortUrl = StringHelper.RandomString(length);
while (db.ShortenedUrls.Where(s => s.ShortUrl == shortUrl).FirstOrDefault() != null)
{
shortUrl = Utility.RandomString(length);
shortUrl = StringHelper.RandomString(length);
}

ShortenedUrl newUrl = new ShortenedUrl();

+ 3
- 2
Teknik/Areas/Upload/Controllers/UploadController.cs View File

@@ -17,6 +17,7 @@ using Teknik.Controllers;
using Teknik.Filters;
using Teknik.Helpers;
using Teknik.Models;
using Teknik.Utilities;

namespace Teknik.Areas.Upload.Controllers
{
@@ -102,7 +103,7 @@ namespace Teknik.Areas.Upload.Controllers
// Generate key and iv if empty
if (string.IsNullOrEmpty(key))
{
key = Utility.RandomString(keySize / 8);
key = StringHelper.RandomString(keySize / 8);
}

fileData = AES.Encrypt(fileData, key, iv);
@@ -380,7 +381,7 @@ namespace Teknik.Areas.Upload.Controllers
Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
if (upload != null)
{
string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
string delKey = StringHelper.RandomString(Config.UploadConfig.DeleteKeyLength);
upload.DeleteKey = delKey;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();

+ 3
- 2
Teknik/Areas/Upload/Uploader.cs View File

@@ -6,6 +6,7 @@ using System.IO;
using Teknik.Configuration;
using Teknik.Models;
using Teknik.Helpers;
using Teknik.Utilities;

namespace Teknik.Areas.Upload
{
@@ -46,10 +47,10 @@ namespace Teknik.Areas.Upload

// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
string url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
{
url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
url = StringHelper.RandomString(config.UploadConfig.UrlLength) + extension;
}

// Now we need to update the database with the new upload information

+ 3
- 2
Teknik/Areas/User/Utility/UserHelper.cs View File

@@ -17,6 +17,7 @@ using Teknik.Areas.Users.Models;
using Teknik.Configuration;
using Teknik.Helpers;
using Teknik.Models;
using Teknik.Utilities;

namespace Teknik.Areas.Users.Utility
{
@@ -437,7 +438,7 @@ namespace Teknik.Areas.Users.Utility
}

// Create a new verification code and add it
string verifyCode = Helpers.Utility.RandomString(24);
string verifyCode = StringHelper.RandomString(24);
RecoveryEmailVerification ver = new RecoveryEmailVerification();
ver.UserId = user.UserId;
ver.Code = verifyCode;
@@ -518,7 +519,7 @@ Thank you and enjoy!
}

// Create a new verification code and add it
string verifyCode = Helpers.Utility.RandomString(24);
string verifyCode = StringHelper.RandomString(24);
ResetPasswordVerification ver = new ResetPasswordVerification();
ver.UserId = user.UserId;
ver.Code = verifyCode;

+ 3
- 90
Teknik/Helpers/Utility.cs View File

@@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Teknik.Utilities;

namespace Teknik.Helpers
{
@@ -30,42 +31,11 @@ namespace Teknik.Helpers
return result;
}

public static string RandomString(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
{
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 GenerateUniqueFileName(string path, string extension, int length)
{
if (Directory.Exists(path))
{
string filename = RandomString(length);
string filename = StringHelper.RandomString(length);
string subDir = filename[0].ToString();
path = Path.Combine(path, subDir);
if (!Directory.Exists(path))
@@ -74,7 +44,7 @@ namespace Teknik.Helpers
}
while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
{
filename = RandomString(length);
filename = StringHelper.RandomString(length);
subDir = filename[0].ToString();
path = Path.Combine(path, subDir);
if (!Directory.Exists(path))
@@ -102,63 +72,6 @@ namespace Teknik.Helpers
return result;
}

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 string ToHex(this byte[] bytes)
{
string hashString = string.Empty;
foreach (byte x in bytes)
{
hashString += String.Format("{0:x2}", x);
}
return hashString;
}

public static byte[] ImageToByte(Image img)
{
byte[] byteArray = new byte[0];

+ 6
- 0
Teknik/Teknik.csproj View File

@@ -713,6 +713,12 @@
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Utilities\Utilities\Utilities.csproj">
<Project>{f45de6fc-3754-4954-a20a-4277362cc6c1}</Project>
<Name>Utilities</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

+ 36
- 0
Utilities/Uploader/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Uploader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Uploader")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f9db28e9-efa1-4d0f-a7b3-c8f491306ce3")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 71
- 0
Utilities/Uploader/Uploader.cs View File

@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Teknik.Utilities
{
public class Uploader
{
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength)
{
return SaveFile(db, config, file, contentType, contentLength, string.Empty, null, null, 256, 128);
}
public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension)
{
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, null, null, 256, 128);
}

public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv)
{
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, null, 256, 128);
}

public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key)
{
return SaveFile(db, config, file, contentType, contentLength, defaultExtension, iv, key, 256, 128);
}

public static Models.Upload SaveFile(TeknikEntities db, Config config, byte[] file, string contentType, int contentLength, string defaultExtension, string iv, string key, int keySize, int blockSize)
{
if (!Directory.Exists(config.UploadConfig.UploadDirectory))
{
Directory.CreateDirectory(config.UploadConfig.UploadDirectory);
}

// Generate a unique file name that does not currently exist
string filePath = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
string fileName = Path.GetFileName(filePath);

// once we have the filename, lets save the file
File.WriteAllBytes(filePath, file);

// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType, defaultExtension) : string.Empty;
string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
{
url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
}

// Now we need to update the database with the new upload information
Models.Upload upload = db.Uploads.Create();
upload.DateUploaded = DateTime.Now;
upload.Url = url;
upload.FileName = fileName;
upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
upload.ContentLength = contentLength;
upload.Key = key;
upload.IV = iv;
upload.KeySize = keySize;
upload.BlockSize = blockSize;

db.Uploads.Add(upload);
db.SaveChanges();

return upload;
}
}
}

+ 54
- 0
Utilities/Uploader/Uploader.csproj View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F9DB28E9-EFA1-4D0F-A7B3-C8F491306CE3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Uploader</RootNamespace>
<AssemblyName>Uploader</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Uploader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 21
- 0
Utilities/Utilities/ByteExtensions.cs View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Teknik.Utilities
{
public static class ByteExtensions
{
public static string ToHex(this byte[] bytes)
{
string hashString = string.Empty;
foreach (byte x in bytes)
{
hashString += String.Format("{0:x2}", x);
}
return hashString;
}
}
}

+ 36
- 0
Utilities/Utilities/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Teknik Utilities")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Teknik Utilities")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f45de6fc-3754-4954-a20a-4277362cc6c1")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 89
- 0
Utilities/Utilities/StringHelper.cs View File

@@ -0,0 +1,89 @@
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, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
{
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;
}
}
}

+ 55
- 0
Utilities/Utilities/Utilities.csproj View File

@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F45DE6FC-3754-4954-A20A-4277362CC6C1}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Teknik.Utilities</RootNamespace>
<AssemblyName>Teknik.Utilities</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ByteExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringHelper.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

Loading…
Cancel
Save