コード例 #1
0
        public void Save(string filePath, DerivedBytesProvider derivedBytesProvider, HelixFileVersion fileVersion = null)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (derivedBytesProvider == null)
            {
                throw new ArgumentNullException(nameof(derivedBytesProvider));
            }

            FileVersion = fileVersion ?? HelixFileVersion.Default;

            using Stream streamOut             = File.Open(filePath, FileMode.Create, FileAccess.Write);
            using HelixFileEncryptor encryptor = new HelixFileEncryptor(streamOut, derivedBytesProvider, fileVersion);
            encryptor.WriteHeader(Header);

            var contentSerialized = JsonConvert.SerializeObject(this);

            encryptor.WriteContent(contentSerialized);
        }
コード例 #2
0
        /// <summary>
        /// Encrypts a file and returns the name of the encrypted file
        /// </summary>
        /// <param name="decrFilePath">The file name and path of the file to be encrypted</param>
        /// <param name="encrFilePath">The file name and path for th encrypted file to be saved</param>
        public static void Encrypt(string decrFilePath, string encrFilePath, DerivedBytesProvider derivedBytesProvider, FileEncryptOptions options = null)
        {
            if (string.IsNullOrWhiteSpace(decrFilePath))
            {
                throw new ArgumentNullException(nameof(decrFilePath));
            }
            if (string.IsNullOrEmpty(encrFilePath))
            {
                throw new ArgumentNullException(nameof(encrFilePath));
            }
            if (derivedBytesProvider == null)
            {
                throw new ArgumentNullException(nameof(derivedBytesProvider));
            }

            options ??= new FileEncryptOptions();

            var encrStagedFileName = encrFilePath + HelixConsts.StagedHxExtention;
            var encrBackupFileName = encrFilePath + HelixConsts.BackupExtention;

            if (!string.IsNullOrEmpty(Path.GetDirectoryName(encrStagedFileName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(encrStagedFileName));
            }

            FileEntry header = FileEntry.FromFile(decrFilePath, Path.GetDirectoryName(decrFilePath));

            if (!string.IsNullOrWhiteSpace(options.StoredFileName))
            {
                header.FileName = options.StoredFileName;
            }

            using (Stream streamIn = (header.EntryType != FileEntryType.File)
                                       ? (Stream)(new MemoryStream())
                                       : File.Open(decrFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var streamOut = File.Open(encrStagedFileName, FileMode.CreateNew, FileAccess.Write))
                    using (var encryptor = new HelixFileEncryptor(streamOut, derivedBytesProvider, options.FileVersion))
                    {
                        options.Log?.Invoke("Writing: Header");
                        options.BeforeWriteHeader?.Invoke(header);
                        encryptor.WriteHeader(header);

                        options.Log?.Invoke("Writing: Content");
                        encryptor.WriteContent(streamIn, streamIn.Length);
                    }


            if (File.Exists(encrFilePath))
            {
                if (File.Exists(encrBackupFileName))
                {
                    options.Log?.Invoke("Destaging: Removing incomplete backup");
                    File.Delete(encrBackupFileName);
                }

                options.Log?.Invoke("Destaging: Moving staged to normal");

                if (File.Exists(encrFilePath))
                {
                    File.Move(encrFilePath, encrBackupFileName);
                }
                File.Move(encrStagedFileName, encrFilePath);

                options.Log?.Invoke("Destaging: Removing backup");
                File.Delete(encrBackupFileName);
            }
            else
            {
                options.Log?.Invoke("Destaging: Moving staged to normal");
                File.Move(encrStagedFileName, encrFilePath);
            }
        }