forked from AscensionGameDev/Intersect-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGzipCompression.cs
More file actions
102 lines (88 loc) · 3.88 KB
/
Copy pathGzipCompression.cs
File metadata and controls
102 lines (88 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
namespace Intersect.Compression;
/// <summary>
/// Contains several wrapper methods used to generalize code in areas of the engine related to compressing and decompressing files and data.
/// </summary>
public static partial class GzipCompression
{
// Our cryptographic provider, and the key to be used to encrypt. Note, it NEEDS to be 16 characters or more!
private static AesCryptoServiceProvider cryptoProvider;
private static string cryptoKey = "T3ZncHUsIGdueHIgdmcgYmUgeXJuaXIgdmcu";
/// <summary>
/// Initialize our Cryptographic Provider and make it usable.
/// </summary>
private static void CreateProvider()
{
cryptoProvider = new AesCryptoServiceProvider();
// Take a few bytes out of this delicious morsel and grow stronk.
var keyBytes = ASCIIEncoding.ASCII.GetBytes(cryptoKey);
cryptoProvider.Key = [.. keyBytes.Take(16)];
cryptoProvider.IV = [.. ((IEnumerable<byte>)keyBytes).Reverse().Take(16)];
}
/// <summary>
/// Read a decompressed unencrypted string from a specified file.
/// </summary>
/// <param name="fileName">The file to decompress.</param>
/// <returns>Returns the decompressed file's content as a string.</returns>
public static string ReadDecompressedString(string fileName)
{
using (var reader = new StreamReader(CreateDecompressedFileStream(fileName)))
{
return reader.ReadToEnd();
}
}
/// <summary>
/// Read a decompressed unencrypted stream from a specified file.
/// </summary>
/// <param name="fileName">The file to decompress.</param>
/// <returns>Returns a decompressed <see cref="CryptoStream"/> of the file's content.</returns>
public static CryptoStream CreateDecompressedFileStream(string fileName)
{
if (cryptoProvider == null)
{
CreateProvider();
}
return new CryptoStream(new GZipStream(File.OpenRead(fileName), CompressionMode.Decompress, false), cryptoProvider.CreateDecryptor(), CryptoStreamMode.Read);
}
/// <summary>
/// Read decompressed unencrypted data from an existing <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to write data from.</param>
/// <returns>Returns a decompressed <see cref="CryptoStream"/> of the stream's content.</returns>
public static CryptoStream CreateDecompressedFileStream(Stream stream)
{
if (cryptoProvider == null)
{
CreateProvider();
}
return new CryptoStream(new GZipStream(stream, CompressionMode.Decompress, false), cryptoProvider.CreateDecryptor(), CryptoStreamMode.Read);
}
/// <summary>
/// Writes the given string to a compressed encrypted file.
/// </summary>
/// <param name="fileName">The file to write the string to.</param>
/// <param name="data">The string to compress and write to the file.</param>
public static void WriteCompressedString(string fileName, string data)
{
using (var stream = CreateCompressedFileStream(fileName))
{
var bytes = Encoding.UTF8.GetBytes(data);
stream.Write(bytes, 0, bytes.Length);
}
}
/// <summary>
/// Creates a compressed encrypted FileStream to write data to.
/// </summary>
/// <param name="fileName">The file to write the data to.</param>
/// <returns>Returns a <see cref="CryptoStream"/> to write data to, saving compressed data to a file.</returns>
public static CryptoStream CreateCompressedFileStream(string fileName)
{
if (cryptoProvider == null)
{
CreateProvider();
}
return new CryptoStream(new GZipStream(new FileStream(fileName, FileMode.Create), CompressionMode.Compress, false), cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write);
}
}