Advertisement
Guest User

DNDUtility DLL

a guest
May 1st, 2014
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System.ServiceModel;
  2. using System.Xml.Linq;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace DNDUtility
  8. {
  9.     using ContentService;
  10.  
  11.     public class DataManager
  12.     {
  13.         static WSHttpBinding customBinding = new WSHttpBinding(SecurityMode.None) { AllowCookies = true, MaxReceivedMessageSize = 200000000, Name = "CustomBinding_IContentVaultService"};
  14.         static EndpointAddress remoteAddress = new EndpointAddress("http://ioun.wizards.com/ContentVault.svc");
  15.         static ContentVaultServiceClient contentClient = new ContentVaultServiceClient(customBinding, remoteAddress);
  16.  
  17.         public static bool Login(string user, string pass)
  18.         {
  19.             return contentClient.Login(user,
  20.                 SimpleEncrypt(pass, user));
  21.         }
  22.  
  23.         public static string[] GetChars()
  24.         {
  25.             ContentInfo[] content = contentClient.GetAvailableContent(0);
  26.  
  27.             string[] chars = new string[content.Length];
  28.  
  29.             for (int i = 0; i < content.Length; i++)
  30.             {
  31.                 XDocument doc = XDocument.Parse(content[i].CommittedContent.Details.ToString());
  32.                 var name = doc.Element("CharacterDetails").Element("Name").Value;
  33.  
  34.                 chars[i] = string.Format("{0} : {1}", i, name);
  35.             }
  36.  
  37.             return chars;
  38.         }
  39.  
  40.         /*public static void GetData()
  41.         {
  42.             ContentInfo[] content = contentClient.GetAvailableContent(0);
  43.  
  44.             DataWithVersion data = contentClient.GetData(
  45.                 new ContentIdentifier() { ContentID = content[0].CommittedContent.Identifier.ContentID },
  46.                 null);
  47.  
  48.             RawContentBlob blob = data.Data as RawContentBlob;
  49.  
  50.             String charFile = new UTF8Encoding().GetString(blob.RawData, 0, blob.RawData.Length);
  51.  
  52.             XDocument doc = XDocument.Parse(charFile);
  53.             doc.Save(@"d:\Users\Tim Williams\Desktop\Work Documents\TerabyteTim\DND App\ContentVault\ConsoleApplication1\contentdoc.xml");
  54.         }
  55.  
  56.         //Updates the given character with new data from an XML sheet.
  57.         public static void SaveData(int charNum, byte[] newData)
  58.         {
  59.             ContentInfo[] content = contentClient.GetAvailableContent(0);
  60.  
  61.             ContentIdentifier newID = contentClient.Edit(content[charNum].CommittedContent.Identifier);
  62.  
  63.             contentClient.UpdateData(newID, newData,
  64.                 content[charNum].CommittedContent.Details);
  65.  
  66.             contentClient.Commit(newID, newID);
  67.         }*/
  68.  
  69.         public static byte[] SimpleEncrypt(string value, string key)
  70.         {
  71.             byte[] buffer2;
  72.             ICryptoTransform transform = GetSimpleAlgorithm(key).CreateEncryptor();
  73.             using (MemoryStream stream = new MemoryStream())
  74.             {
  75.                 using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
  76.                 {
  77.                     byte[] bytes = Encoding.UTF8.GetBytes(value);
  78.                     stream2.Write(bytes, 0, bytes.Length);
  79.                     stream2.Flush();
  80.                     stream2.FlushFinalBlock();
  81.                     stream.Position = 0L;
  82.                     buffer2 = stream.ToArray();
  83.                 }
  84.             }
  85.             return buffer2;
  86.         }
  87.  
  88.         static SymmetricAlgorithm GetSimpleAlgorithm(string key)
  89.         {
  90.             AesManaged aes = new AesManaged();
  91.             byte[] source = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(key));
  92.  
  93.             byte[] take = new byte[aes.BlockSize / 8];
  94.             for (int i = 0; i < aes.BlockSize / 8; i++)
  95.             {
  96.                 take[i] = source[i];
  97.             }
  98.  
  99.             return new AesManaged { Key = source, IV = take };
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement