/// <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); } }
/// <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; }