using Newtonsoft.Json.Linq; using System.IO.Compression; using System.Security.Cryptography; using System.Text; namespace Quizzer.Extensions { public static class Extensions { /// /// Compresses a string and returns a deflate compressed, Base64 encoded string. /// /// String to compress public static string Compress(this string uncompressedString) { byte[] compressedBytes; using (var uncompressedStream = new MemoryStream(Encoding.UTF8.GetBytes(uncompressedString))) { using (var compressedStream = new MemoryStream()) { // setting the leaveOpen parameter to true to ensure that compressedStream will not be closed when compressorStream is disposed // this allows compressorStream to close and flush its buffers to compressedStream and guarantees that compressedStream.ToArray() can be called afterward // although MSDN documentation states that ToArray() can be called on a closed MemoryStream, I don't want to rely on that very odd behavior should it ever change using (var compressorStream = new DeflateStream(compressedStream, CompressionLevel.Fastest, true)) { uncompressedStream.CopyTo(compressorStream); } // call compressedStream.ToArray() after the enclosing DeflateStream has closed and flushed its buffer to compressedStream compressedBytes = compressedStream.ToArray(); } } return Convert.ToBase64String(compressedBytes); } /// /// Decompresses a deflate compressed, Base64 encoded string and returns an uncompressed string. /// /// String to decompress. public static string Decompress(this string compressedString) { byte[] decompressedBytes; var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString)); using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) { using (var decompressedStream = new MemoryStream()) { decompressorStream.CopyTo(decompressedStream); decompressedBytes = decompressedStream.ToArray(); } } return Encoding.UTF8.GetString(decompressedBytes); } /// /// Hashes the string using SHA256 /// /// /// What needs to be hashed /// /// /// The hash /// public static string HashSHA256(this string plaintext) { StringBuilder Sb = new StringBuilder(); using (var hash = SHA256.Create()) { Encoding enc = Encoding.UTF8; byte[] result = hash.ComputeHash(enc.GetBytes(plaintext)); foreach (byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); } /// /// Hashes the string using MD5 /// /// /// What needs to be hashed /// /// /// The hash /// public static string HashMD5(this string plaintext) { StringBuilder Sb = new StringBuilder(); using (var hash = MD5.Create()) { Encoding enc = Encoding.UTF8; byte[] result = hash.ComputeHash(enc.GetBytes(plaintext)); foreach (byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); } } }