public OpenOtpPasswordStoreTask(string filePath, string keyFilePath) : base(ResourceType.File, filePath, keyFilePath) { InnerTask = new Task(() => { var model = new OpenOtpPasswordStoreTaskResultModel { StorePath = filePath, KeyPath = keyFilePath }; if (!File.Exists(filePath)) { using (var fs = File.Create(filePath)) { new BinaryFormatter().Serialize(fs, new List <PasswordModel>()); } model.Exception = new OtpPasswordStoreFirstUseException(); Result.Value = model; return; } if (!File.Exists(keyFilePath)) { model.Exception = new OtpKeyFileNotAvailableException(); Result.Value = model; return; } if (new FileInfo(filePath).Length > new FileInfo(keyFilePath).Length) { throw new InvalidKeyException(); } OtpHelper.Transform(filePath, keyFilePath); var models = new List <PasswordModel>(); using (var fs = new FileStream(filePath, FileMode.Open)) { if (fs.Length > 0) { if (new BinaryFormatter().Deserialize(fs) is List <PasswordModel> list) { models = list; } } } OtpHelper.Transform(filePath, keyFilePath); model.Models = models; Result.Value = model; }); }
public CreateOtpPasswordStoreKeyTask(string storeFilePath, string keyPath, bool open) : base(ResourceType.File, keyPath) { InnerTask = new Task(() => { OtpHelper.GenerateKey(keyPath, 1024 * 1024); OtpHelper.Transform(storeFilePath, keyPath); Result.Value = new CreateOtpPasswordStoreKeyTaskResultModel { StorePath = storeFilePath, KeyPath = keyPath, OpenAfter = open }; }); }
public OtpSavePasswordsTask(string storePath, string keyPath, IReadOnlyCollection <PasswordModel> models) : base(ResourceType.File, storePath, keyPath) { InnerTask = new Task(() => { if (!File.Exists(storePath)) { throw new FileNotFoundException(nameof(storePath)); } if (!File.Exists(keyPath)) { throw new FileNotFoundException(nameof(keyPath)); } OtpHelper.Transform(storePath, keyPath); using (var fs = new FileStream(storePath, FileMode.Create)) { new BinaryFormatter().Serialize(fs, models); } var storeLength = new FileInfo(storePath).Length; var keyLength = new FileInfo(keyPath).Length; if (storeLength > keyLength) { var increase = storeLength - keyLength; using (var crypto = new RNGCryptoServiceProvider()) { using (var fs = new FileStream(keyPath, FileMode.Append)) { var buffer = new byte[1024]; while (increase > 0) { if (buffer.Length > increase) { buffer = new byte[increase]; } crypto.GetNonZeroBytes(buffer); fs.Write(buffer, 0, buffer.Length); increase -= buffer.Length; } } } } OtpHelper.Transform(storePath, keyPath); }); }
public OtpTransformTask(string filePath, string ext, string keyFilePath = "", bool encrypt = true) : base(ResourceType.File, filePath, keyFilePath) { InnerTask = new Task(() => { string newFileName; if (encrypt) { newFileName = FileGeneratorHelper.GetValidFileNameForDirectory( DirectoryHelper.GetDirectoryPath(filePath), Path.GetFileName(filePath), ext); } else { if (!filePath.EndsWith(ext, StringComparison.Ordinal)) { throw new InvalidEncryptedFileException(); } var name = filePath.RemoveLast(ext.Length); newFileName = FileGeneratorHelper.GetValidFileNameForDirectory( DirectoryHelper.GetDirectoryPath(name), Path.GetFileName(name), string.Empty); } if (newFileName == null) { throw new NoSuitableNameFoundException(); } File.Move(filePath, newFileName); if (string.IsNullOrEmpty(keyFilePath)) { OtpHelper.EncryptWithoutKey(newFileName); } else { OtpHelper.Transform(newFileName, keyFilePath); } }); }