Esempio n. 1
0
 /// <summary>
 /// Adds a file to the file list
 /// </summary>
 /// <param name="userFile">file to add</param>
 internal void Add(UserFile userFile)
 {
     if (Crypto.VerifySignature(userFile.HashObject(), publicKey, userFile.Signature))
     {
         files.Add(userFile);
     }
 }
Esempio n. 2
0
        internal void ShowFile(UserFile fileToShow, string fileName = null)
        {
            if (fileName == null)  fileName = session.DecryptFileName(fileToShow);
            var dirs = fileName.Split('/');
            TreeNode node = virtualLocalFileTree.Nodes[0];
            for (int i = 0; i < dirs.Count(); i++)
            {
                TreeNodeCollection nodes = node.Nodes;

                if (nodes.ContainsKey(dirs[i]))
                {
                    node = nodes[dirs[i]];
                }
                else
                {
                    node = nodes.Add(dirs[i], dirs[i]);
                    if (i == dirs.Count() - 1)
                        node.ImageIndex = 3;
                    else
                        node.ImageIndex = 1;

                    node.SelectedImageIndex = node.ImageIndex;
                }
            }
            node.Tag = fileToShow;
        }
Esempio n. 3
0
        internal void Test()
        {
            var objectId = KademliaId.RandomId;
            var chunkId = KademliaId.RandomId;

            var user = new User();
            user.ObjectId = objectId;
            var userFile = new UserFile();
            userFile.ChunkList.Add(chunkId);
            user.Add(userFile);

            var ms = new MemoryStream();
            Serializer.Serialize(ms, user);
            var bytes = ms.ToArray();

            // store the object

            var ct1 = new CallbackTimeout<Error>();
            nodeA.StoreObject(nodeBInfo, objectId, bytes, ct1.Done);
            if (!ct1.Block(TestParameters.LocalhostCommunicationTimeout))
            {
                Assert.Fail("No response within timeout");
            }
            Assert.AreEqual(Error.Success, ct1.Result);

            // now retrieve it

            var ct2 = new CallbackTimeout<Error, byte[]>();
            nodeC.GetObjectFromNode(new List<NodeInformation>{nodeBInfo}, objectId, ct2.Done);
            if (!ct2.Block(TestParameters.LocalhostCommunicationTimeout))
            {
                Assert.Fail("No response within timeout");
            }
            Assert.AreEqual(Error.Success, ct2.Result1);
            Assert.AreElementsEqual(bytes, ct2.Result2);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a UserFile from a normal file
        /// </summary>
        /// <param name="input">the file to add to the User's files</param>
        /// <param name="fileName">the file's name</param>
        /// <returns></returns>
        internal UserFile AddFile(Stream input, string fileName)
        {
            var data = new byte[input.Length];
            var chunkList = new List<KademliaId>();
            byte[] fileKey = Crypto.GenerateAesKey();
            byte[] fileIV = Crypto.GenerateIV();
            byte[] nameIV = Crypto.GenerateIV();

            input.Read(data, 0, (int) input.Length);
            data = Crypto.EncryptAes(data, fileKey, fileIV);

            int finalChunkSize = data.Length % LocalObjectStorage.MaxObjectSize;
            int chunks = data.Length / LocalObjectStorage.MaxObjectSize;
            var chunkData = new byte[LocalObjectStorage.MaxObjectSize];

            for (int i = 0; i < chunks; i++)
            {
                Array.Copy(data, i * LocalObjectStorage.MaxObjectSize, chunkData, 0, LocalObjectStorage.MaxObjectSize);
                AddChunk(chunkList, chunkData);
            }

            if (finalChunkSize != 0)
            {
                Array.Copy(data, chunks * LocalObjectStorage.MaxObjectSize, chunkData, 0, finalChunkSize);
                AddChunk(chunkList, chunkData.Take(finalChunkSize).ToArray());
            }

            var userFile = new UserFile {
                                            FileId = Random.UInt64(),
                                            ChunkList = chunkList.Select(c => c.Bytes).ToList(),
                                            EncryptedFileName = Crypto.EncryptAes(Encoding.UTF8.GetBytes(fileName), fileKey, nameIV),
                                            EncryptedKey = Crypto.EncryptRsa(fileKey, rsaKeyPair.Public),
                                            FileIV = fileIV,
                                            NameIV = nameIV,
                                            Hash = Crypto.Hash(data)
                                        };
            userFile.Signature = Crypto.Sign(userFile.HashObject(), rsaKeyPair);

            User.Add(userFile);

            LocalObjectStorage.StoreObject(User, true);
            return userFile;
        }
Esempio n. 5
0
 /// <summary>
 /// decrypts the filekey belonging to a UserFile
 /// </summary>
 /// <param name="userFile">UserFile object containing an encrypted filekey</param>
 /// <returns>the decrypted filekey</returns>
 private byte[] DecryptKey(UserFile userFile)
 {
     return Crypto.DecryptRsa(userFile.EncryptedKey, rsaKeyPair);
 }
Esempio n. 6
0
        /// <summary>
        /// Changes a user's file's status to deleted
        /// </summary>
        /// <param name="userFile"></param>
        internal void DeleteFile(UserFile userFile)
        {
            User.AddDeletedFile(Crypto.Sign(BitConverter.GetBytes(userFile.FileId), rsaKeyPair), userFile.FileId);

            LocalObjectStorage.StoreObject(User, true);
        }
Esempio n. 7
0
 /// <summary>
 /// Decrypts the file name of a given UserFile
 /// </summary>
 /// <param name="userFile">File of which the name is desired</param>
 /// <returns>The filename</returns>
 internal string DecryptFileName(UserFile userFile)
 {
     return Encoding.UTF8.GetString(Crypto.DecryptAes(userFile.EncryptedFileName, DecryptKey(userFile), userFile.NameIV));
 }
Esempio n. 8
0
        /// <summary>
        /// Decrypts a file and outputs the result in the stream. The file's chunks must be present before calling this method.
        /// </summary>
        /// <param name="userFile">the file to decrypt</param>
        /// <param name="output">the destination of the decrypted file</param>
        /// <exception cref="NullReferenceException">One or more of the chunks could not be found</exception>
        internal void DecryptFile(UserFile userFile, Stream output)
        {
            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                foreach (KademliaId chunkId in userFile.ChunkList)
                {
                    var chunk = LocalObjectStorage.GetObject(chunkId) as Chunk;
                    ms.Write(chunk.Data, 0, chunk.Data.Length);
                }

                bytes = ms.ToArray();
            }
            bytes = Crypto.DecryptAes(bytes, DecryptKey(userFile), userFile.FileIV);
            output.Write(bytes, 0, bytes.Length);
        }
Esempio n. 9
0
        private void StoreTestObject()
        {
            // create the test object

            objectId = KademliaId.RandomId;
            var chunkId = KademliaId.RandomId;

            var user = new User();
            user.ObjectId = objectId;
            var userFile = new UserFile();
            userFile.ChunkList.Add(chunkId);
            user.Add(userFile);

            var ms = new MemoryStream();
            Serializer.Serialize(ms, user);
            bytes = ms.ToArray();

            // store it

            var ct = new CallbackTimeout<Error>();
            var ni = new NodeInformation(new IPEndPoint(IPAddress.Loopback, nodes[1].Port), nodes[1].Id);
            nodes[0].StoreObject(ni, objectId, bytes, ct.Done);
            if (!ct.Block(TestParameters.LocalhostCommunicationTimeout))
            {
                Assert.Fail("No response within timeout");
            }
            Assert.AreEqual(Error.Success, ct.Result);
        }