.
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VizhTalk.Grpc.Learning.Helpers
|
||||
{
|
||||
internal class CompressionHelper
|
||||
{
|
||||
public static byte[] Compress(byte[] data)
|
||||
{
|
||||
using var output = new MemoryStream();
|
||||
using (var gzip = new GZipStream(output, CompressionMode.Compress))
|
||||
{
|
||||
gzip.Write(data, 0, data.Length);
|
||||
}
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] compressedData)
|
||||
{
|
||||
using var input = new MemoryStream(compressedData);
|
||||
using var gzip = new GZipStream(input, CompressionMode.Decompress);
|
||||
using var output = new MemoryStream();
|
||||
gzip.CopyTo(output);
|
||||
return output.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace VizhTalk.Grpc.Learning.Helpers
|
||||
{
|
||||
internal class EncryptionHelper
|
||||
{
|
||||
private const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
public static string Encrypt(string input)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(input);
|
||||
|
||||
// تبدیل بایتها به عدد بزرگ
|
||||
var number = new System.Numerics.BigInteger(bytes);
|
||||
|
||||
// اگر منفی شد، مثبتش کن
|
||||
if (number < 0) number = -number;
|
||||
|
||||
// تبدیل به Base62
|
||||
StringBuilder result = new StringBuilder();
|
||||
while (number > 0)
|
||||
{
|
||||
result.Insert(0, Alphabet[(int)(number % 62)]);
|
||||
number /= 62;
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
public static string Decrypt(string encoded)
|
||||
{
|
||||
System.Numerics.BigInteger number = 0;
|
||||
foreach (char c in encoded)
|
||||
{
|
||||
number = number * 62 + Alphabet.IndexOf(c);
|
||||
}
|
||||
|
||||
// تبدیل دوباره به بایت
|
||||
byte[] bytes = number.ToByteArray();
|
||||
|
||||
// رشته اصلی
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user