using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace GreenTree.Nachtragsmanagement.Core
{
public static class StaticHelper
{
#region Encryption
///
/// Hashes an input text with MD5
///
/// The input text.
public static string GetMD5Hash(string text)
{
MD5 md5 = MD5.Create();
var inputBytes = System.Text.Encoding.ASCII.GetBytes(text);
var hash = md5.ComputeHash(inputBytes);
var sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("X2"));
return sb.ToString();
}
#endregion
#region String-Compression
///
/// Compresses a string
///
/// The text to compress.
/// Determines if the string should be compressed by a DeflateStream.
/// The base encoding of the text.
/// The compressed text.
public static string CompressString(string text, bool deflate, Encoding encoding)
{
var buffer = encoding.GetBytes(text);
var memoryStream = new MemoryStream();
if (!deflate)
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
else
using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true))
{
deflateStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
var compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
var gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
///
/// Decompresses a string
///
/// The compressed text.
/// Determines if the string should be decompressed by a DeflateStream.
/// The base encoding of the compressed text.
/// The decompressed text.
public static string DecompressString(string compressedText, bool deflate, Encoding encoding)
{
var gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
var dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
var buffer = new byte[dataLength];
memoryStream.Position = 0;
if (!deflate)
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
else
using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
{
deflateStream.Read(buffer, 0, buffer.Length);
}
return encoding.GetString(buffer);
}
}
///
/// Compresses a string
///
/// The text to compress.
/// Der komprimierte String.
public static string CompressString(string text)
{
return CompressString(text, true, Encoding.Default);
}
///
/// Decompresses a string
///
/// The text to be decompressed.
/// The decompressed text.
public static string DecompressString(string text)
{
return DecompressString(text, true, Encoding.Default);
}
#endregion
#region File-renaming
///
/// Adds a suffix to a filepath between name and extension
///
/// The original filepath.
/// The suffix.
/// Determines if the original filepath is a relative path.
public static string AddSuffix(string filename, string suffix, bool isRelativePath)
{
var fDir = Path.GetDirectoryName(filename);
var fName = Path.GetFileNameWithoutExtension(filename);
var fExt = Path.GetExtension(filename);
if (isRelativePath)
return Path.Combine(fDir, String.Concat(fName, suffix, fExt)).Replace("\\", "/");
else
return Path.Combine(fDir, String.Concat(fName, suffix, fExt));
}
#endregion
}
}