-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraphCompression.cs
44 lines (40 loc) · 1.57 KB
/
GraphCompression.cs
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
using System;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using NodeBlock.Engine.Encoding;
namespace NodeBlock.Engine.Encoding
{
public static class GraphCompression
{
public static string DecompressGraphData(string input)
{
byte[] compressed = Convert.FromBase64String(input);
byte[] decompressed = GzipCompression.Decompress(compressed);
return System.Text.Encoding.UTF8.GetString(decompressed);
}
public static string CompressGraphData(string input)
{
byte[] encoded = System.Text.Encoding.UTF8.GetBytes(input);
byte[] compressed = GzipCompression.Compress(encoded);
return Convert.ToBase64String(compressed);
}
public static string GetUniqueGraphHash(int identifier, string compressedData)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(string.Format("{0},{1}", Convert.ToString(identifier), compressedData)));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
}
}