From 7a4a90d8243d4e4c17119873fb52b531ffd62329 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 18 Oct 2017 12:14:17 -0400 Subject: [PATCH] Documents Helpers.cs --- TGServiceInterface/Helpers.cs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/TGServiceInterface/Helpers.cs b/TGServiceInterface/Helpers.cs index 6d8e3e0f70..c5435c70a4 100644 --- a/TGServiceInterface/Helpers.cs +++ b/TGServiceInterface/Helpers.cs @@ -4,26 +4,41 @@ using System.Text; namespace TGServiceInterface { + /// + /// Helper functions used across the server suite + /// public static class Helpers { - public static string EncryptData(string data, out string sentropy) + /// + /// Takes some and returns an encrypted version along with the required to decrypt it + /// + /// The to encrypt + /// The entropy required the decrypt the ciphertext + /// Ciphertext for the + public static string EncryptData(string cleartext, out string entropy) { // Generate additional entropy (will be used as the Initialization vector) - byte[] entropy = new byte[20]; + byte[] bentropy = new byte[20]; using (var rng = new RNGCryptoServiceProvider()) - rng.GetBytes(entropy); + rng.GetBytes(bentropy); - byte[] ciphertext = ProtectedData.Protect(Encoding.UTF8.GetBytes(data), entropy, DataProtectionScope.CurrentUser); + byte[] ciphertext = ProtectedData.Protect(Encoding.UTF8.GetBytes(data), bentropy, DataProtectionScope.CurrentUser); - sentropy = Convert.ToBase64String(entropy, 0, entropy.Length); + entropy = Convert.ToBase64String(bentropy, 0, bentropy.Length); return Convert.ToBase64String(ciphertext, 0, ciphertext.Length); } - public static string DecryptData(string data, string entropy) + /// + /// Takes ciphertext and entropy from and returns the cleartext. Note that this only works if the OS user of the program is the same one that called + /// + /// A return value from a previous call to + /// The entropy parameter from the previous call to that returned + /// The decrypted on sucess or on failure + public static string DecryptData(string ciphertext, string entropy) { try { - return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(data), Convert.FromBase64String(entropy), DataProtectionScope.CurrentUser)); + return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(ciphertext), Convert.FromBase64String(entropy), DataProtectionScope.CurrentUser)); } catch {