Пример #1
0
        public void CreateCryptoConfigNoPrefixPath()
        {
            const string targetFile   = Preparations.TestFolderPath + "CreateCryptoConfigNoPrefixPath.td";
            const string mockFileName = "importantData.txt";

            var key = CryptoRNG.GetRandomBytes(AesSizes.Key);

            var vaultFile = UserDataHeader.Create(mockFileName);
            var writer    = new UserDataHeaderWriter(vaultFile);

            using (var stream = new FileStream(targetFile, FileMode.Create, FileAccess.Write))
            {
                writer.WriteTo(stream, key);
            }

            UserDataHeader readUserDataHeader;

            using (var stream = new FileStream(targetFile, FileMode.Open, FileAccess.Read))
            {
                readUserDataHeader = UserDataHeaderReader.ReadFrom(stream, key);
            }

            Assert.AreEqual(vaultFile.TargetPath, readUserDataHeader.TargetPath);
            Assert.AreEqual(vaultFile.TargetCipherIV, readUserDataHeader.TargetCipherIV);
            Assert.AreEqual(vaultFile.TargetAuthentication, readUserDataHeader.TargetAuthentication);

            Assert.AreEqual(vaultFile.IsUnlocked, readUserDataHeader.IsUnlocked);
            Assert.AreEqual(vaultFile.SecuredPlainName.PlainName, readUserDataHeader.SecuredPlainName.PlainName);
        }
        //++++++++++++++++++++++++++++
        //        Reading
        //++++++++++++++++++++++++++++

        public static Vault ReadFromConfig(VaultPaths paths, byte[] key)
        {
            var result = new Vault(paths.Name, key)
            {
                VaultPath = paths.FullVaultFolderPath
            };

            using var vaultFile = new FileStream(paths.VaultFilePath, FileMode.Open, FileAccess.Read);

            result.Header = VaultHeaderReader.ReadFrom(vaultFile);

            var(keyWasCorrect, password) = result.Header.MasterPassword.GetDecryptedPassword(key);

            if (!keyWasCorrect)
            {
                throw new CryptographicException(Strings.VaultReaderWriter_ReadFromConfig_Password_wasn_t_able_to_be_verified);
            }

            while (vaultFile.Position < vaultFile.Length)
            {
                result.UserDataFiles.Add(new UserDataFile(UserDataHeaderReader.ReadFrom(vaultFile, password)));
            }

            result.CheckAndCorrectAllItemHeaders();
            // TODO: check if cipher files exists, if not remove that file

            return(result);
        }