コード例 #1
0
        public MacEncryptedStore(IFileSystem fileSystem)
            : base(fileSystem)
        {
            _fileSystem   = fileSystem;
            _cryptKeyPath = _fileSystem.Path.Combine(StorageLocation, "ck");
            _authKeyPath  = _fileSystem.Path.Combine(StorageLocation, "ak");

            if (!_fileSystem.File.Exists(_cryptKeyPath) || !_fileSystem.File.Exists(_authKeyPath))
            {
                fileSystem.Directory.CreateDirectory(StorageLocation);
                var cryptKey = AuthenticatedEncryption.NewKey();
                var authKey  = AuthenticatedEncryption.NewKey();

                _fileSystem.File.WriteAllBytes(_cryptKeyPath, cryptKey);
                _fileSystem.File.WriteAllBytes(_authKeyPath, authKey);
            }
        }
コード例 #2
0
 public override T Load()
 {
     try
     {
         var cryptKey     = _fileSystem.File.ReadAllBytes(_cryptKeyPath);
         var authKey      = _fileSystem.File.ReadAllBytes(_authKeyPath);
         var cipherPath   = _fileSystem.Path.Combine(StorageLocation, typeof(T).Name);
         var cipherText   = _fileSystem.File.ReadAllText(cipherPath);
         var plainText    = AuthenticatedEncryption.Decrypt(cipherText, cryptKey, authKey);
         var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build();
         return(deserializer.Deserialize <T>(plainText));
     }
     catch (Exception)
     {
         return(new T());
     }
 }