StaticHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace GreenTree.Nachtragsmanagement.Core
  10. {
  11. public static class StaticHelper
  12. {
  13. #region Encryption
  14. /// <summary>
  15. /// Hashes an input text with MD5
  16. /// </summary>
  17. /// <param name="text">The input text.</param>
  18. public static string GetMD5Hash(string text)
  19. {
  20. MD5 md5 = MD5.Create();
  21. var inputBytes = System.Text.Encoding.ASCII.GetBytes(text);
  22. var hash = md5.ComputeHash(inputBytes);
  23. var sb = new StringBuilder();
  24. for (int i = 0; i < hash.Length; i++)
  25. sb.Append(hash[i].ToString("X2"));
  26. return sb.ToString();
  27. }
  28. #endregion
  29. #region String-Compression
  30. /// <summary>
  31. /// Compresses a string
  32. /// </summary>
  33. /// <param name="text">The text to compress.</param>
  34. /// <param name="deflate">Determines if the string should be compressed by a DeflateStream.</param>
  35. /// <param name="encoding">The base encoding of the text.</param>
  36. /// <returns>The compressed text.</returns>
  37. public static string CompressString(string text, bool deflate, Encoding encoding)
  38. {
  39. var buffer = encoding.GetBytes(text);
  40. var memoryStream = new MemoryStream();
  41. if (!deflate)
  42. using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
  43. {
  44. gZipStream.Write(buffer, 0, buffer.Length);
  45. }
  46. else
  47. using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true))
  48. {
  49. deflateStream.Write(buffer, 0, buffer.Length);
  50. }
  51. memoryStream.Position = 0;
  52. var compressedData = new byte[memoryStream.Length];
  53. memoryStream.Read(compressedData, 0, compressedData.Length);
  54. var gZipBuffer = new byte[compressedData.Length + 4];
  55. Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
  56. Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
  57. return Convert.ToBase64String(gZipBuffer);
  58. }
  59. /// <summary>
  60. /// Decompresses a string
  61. /// </summary>
  62. /// <param name="compressedText">The compressed text.</param>
  63. /// <param name="deflate">Determines if the string should be decompressed by a DeflateStream.</param>
  64. /// <param name="encoding">The base encoding of the compressed text.</param>
  65. /// <returns>The decompressed text.</returns>
  66. public static string DecompressString(string compressedText, bool deflate, Encoding encoding)
  67. {
  68. var gZipBuffer = Convert.FromBase64String(compressedText);
  69. using (var memoryStream = new MemoryStream())
  70. {
  71. var dataLength = BitConverter.ToInt32(gZipBuffer, 0);
  72. memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
  73. var buffer = new byte[dataLength];
  74. memoryStream.Position = 0;
  75. if (!deflate)
  76. using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
  77. {
  78. gZipStream.Read(buffer, 0, buffer.Length);
  79. }
  80. else
  81. using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
  82. {
  83. deflateStream.Read(buffer, 0, buffer.Length);
  84. }
  85. return encoding.GetString(buffer);
  86. }
  87. }
  88. /// <summary>
  89. /// Compresses a string
  90. /// </summary>
  91. /// <param name="text">The text to compress.</param>
  92. /// <returns>Der komprimierte String.</returns>
  93. public static string CompressString(string text)
  94. {
  95. return CompressString(text, true, Encoding.Default);
  96. }
  97. /// <summary>
  98. /// Methode zur Dekomprimierung eines Strings
  99. /// </summary>
  100. /// <param name="text">The text to be decompressed.</param>
  101. /// <returns>The decompressed text.</returns>
  102. public static string DecompressString(string text)
  103. {
  104. return DecompressString(text, true, Encoding.Default);
  105. }
  106. #endregion
  107. }
  108. }