public void Save(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } var fileProvider = new FileProvider(fileName); byte[] buffer; if (Path.GetExtension(fileName).Equals(CryptExtension)) { if (dataCrypter == null) { var key = getSecurityKey.Invoke(); dataCrypter = new DataCrypter(key); } buffer = dataCrypter.Encrypt(Document.GetBytes(), TextDocument.FormatPrefix); } else { buffer = Document.GetBytes(); } fileProvider.SaveData(buffer); FileName = fileName; }
public CryptFile(string fileName, Func <string> securityKeyGetter) { if (!File.Exists(fileName)) { throw new FileNotFoundException(); } getSecurityKey = securityKeyGetter; var fileProvider = new FileProvider(fileName); var buffer = fileProvider.LoadData(); if (Path.GetExtension(fileName).Equals(CryptExtension)) { var key = getSecurityKey.Invoke(); dataCrypter = new DataCrypter(key); buffer = dataCrypter.Decrypt(buffer, out _); } FileName = fileName; Document = new TextDocument(buffer); }