Exemplo n.º 1
0
        /// <summary>
        /// This is automatically called when you first access Keys.
        /// </summary>
        public static void IndexKeys()
        {
            keys.Clear();
            if(!Directory.Exists(folderName)) Directory.CreateDirectory(folderName);

            var files = Directory.GetFiles(folderName, "*.mek");
            if(files.Length == 0)
            {
                //Generate a new key
                AES newKey = new AES(Encoding.UTF8);
                newKey.KeyToFile(folderName+"/EncryptionKey.mek");
                File.WriteAllText(folderName+"/EncryptionKey.txt", "There were no keys, so this default key was generated for you. Note this key will be erased the next time nodes are synced and this is NOT the proper way to add client keys.");
                files = Directory.GetFiles(folderName, "*.mek");
            }
            foreach(var file in files)
            {
                using (var md5 = MD5.Create())
                {
                    using (var stream = File.OpenRead(file))
                    {
                        var hash = md5.ComputeHash(stream);
                        keys[hash] = new AES(Encoding.UTF8, file);
                    }
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Create a new key database in a location.
 /// </summary>
 public EncryptionKeyDB(string folderName)
 {
     Instance = this;
     this.folderName = folderName;
     //Load mek files (matrix encryption keys)
     if (!Directory.Exists(folderName)) Directory.CreateDirectory(folderName);
     var files = Directory.GetFiles(this.folderName, "*.mek");
     if(files.Length == 0)
     {
         //Generate a new key
         AES newKey = new AES(Encoding.UTF8);
         newKey.KeyToFile(this.folderName+"/DefaultKey.mek");
         File.WriteAllText(this.folderName+"/DefaultKey.txt", "There were no keys, so this default key was generated for you.");
         files = Directory.GetFiles(this.folderName, "*.mek");
     }
     foreach(var file in files)
     {
         using (var md5 = MD5.Create())
         {
             using (var stream = File.OpenRead(file))
             {
                 var hash = md5.ComputeHash(stream);
                 keys[hash] = new AES(Encoding.UTF8, file);
             }
         }
     }
 }