WIP saving

This commit is contained in:
2017-09-20 18:45:37 +02:00
parent 668ffcb107
commit c4ac244270
6 changed files with 64 additions and 67 deletions

View File

@@ -18,17 +18,16 @@ namespace ModernKeePassLib.Serialization
private IEnumerator<byte> m_enumerator = null;
public CryptoStream(Stream s, String strAlgName, bool bEncrypt, byte[] pbKey, byte[] pbIV)
: base()
{
IBuffer iv = CryptographicBuffer.CreateFromByteArray(pbIV);
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);
CryptographicKey key = objAlg.CreateSymmetricKey( CryptographicBuffer.CreateFromByteArray(pbKey) );
if (bEncrypt)
/*if (bEncrypt)
{
Debug.Assert(false, "Not implemented yet");
}
else
{
{*/
// For the time being, WinRT CryptographicEngine doesn't support stream decoding. Bummer.
// Copy the file to a memory buffer, then decode all at once.
@@ -50,7 +49,7 @@ namespace ModernKeePassLib.Serialization
}
CryptographicBuffer.CopyToByteArray(decoded, out m_decoded);
m_enumerator = m_decoded.AsEnumerable().GetEnumerator();
}
//}
}

View File

@@ -281,13 +281,13 @@ namespace ModernKeePassLib.Serialization
return CreateWebClient(ioc).OpenWrite(uri);
}
#else
public async static Task<Stream> OpenWrite(IOConnectionInfo ioc)
public static async Task<Stream> OpenWrite(IOConnectionInfo ioc)
{
return await OpenWriteLocal(ioc);
}
#endif
private async static Task<Stream> OpenWriteLocal(IOConnectionInfo ioc)
private static async Task<Stream> OpenWriteLocal(IOConnectionInfo ioc)
{
try
{
@@ -303,18 +303,15 @@ namespace ModernKeePassLib.Serialization
public static bool FileExists(IOConnectionInfo ioc)
{
return FileExists(ioc, false);
//return FileExists(ioc, false);
return true;
}
public static bool FileExists(IOConnectionInfo ioc, bool bThrowErrors)
/*public static bool FileExists(IOConnectionInfo ioc, bool bThrowErrors)
{
Debug.Assert(false, "Not implemented yet");
return false;
#if TODO
if(ioc == null) { Debug.Assert(false); return false; }
if(ioc.IsLocalFile()) return File.Exists(ioc.Path);
if(ioc.IsLocalFile()) return ioc.StorageFile.IsAvailable;
try
{
@@ -334,15 +331,12 @@ namespace ModernKeePassLib.Serialization
}
return true;
#endif
}
}*/
public static void DeleteFile(IOConnectionInfo ioc)
public static async void DeleteFile(IOConnectionInfo ioc)
{
Debug.Assert(false, "Not implemented yet");
return ;
#if TODO
if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; }
if(ioc.IsLocalFile()) { await ioc.StorageFile.DeleteAsync(StorageDeleteOption.Default);
}
#if !KeePassLibSD && TODO
WebRequest req = CreateWebRequest(ioc);
@@ -359,7 +353,6 @@ namespace ModernKeePassLib.Serialization
DisposeResponse(req.GetResponse(), true);
}
#endif
#endif
}
@@ -372,11 +365,9 @@ namespace ModernKeePassLib.Serialization
/// </summary>
/// <param name="iocFrom">Source file path.</param>
/// <param name="iocTo">Target file path.</param>
public static void RenameFile(IOConnectionInfo iocFrom, IOConnectionInfo iocTo)
public static async void RenameFile(IOConnectionInfo iocFrom, IOConnectionInfo iocTo)
{
return;
#if TODO
if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; }
if(iocFrom.IsLocalFile()) { await iocTo.StorageFile.RenameAsync(iocTo.Path, NameCollisionOption.GenerateUniqueName); }
#if !KeePassLibSD && TODO
WebRequest req = CreateWebRequest(iocFrom);
@@ -419,7 +410,6 @@ namespace ModernKeePassLib.Serialization
// sIn.Close();
// }
// DeleteFile(iocFrom);
#endif
}
private static void DisposeResponse(WebResponse wr, bool bGetStream)

View File

@@ -85,52 +85,60 @@ namespace ModernKeePassLib.Serialization
UTF8Encoding encNoBom = StrUtil.Utf8;
CryptoRandom cr = CryptoRandom.Instance;
try
{
m_pbMasterSeed = cr.GetRandomBytes(32);
m_pbTransformSeed = cr.GetRandomBytes(32);
m_pbEncryptionIV = cr.GetRandomBytes(16);
try
{
m_pbMasterSeed = cr.GetRandomBytes(32);
m_pbTransformSeed = cr.GetRandomBytes(32);
m_pbEncryptionIV = cr.GetRandomBytes(16);
m_pbProtectedStreamKey = cr.GetRandomBytes(32);
m_craInnerRandomStream = CrsAlgorithm.Salsa20;
m_randomStream = new CryptoRandomStream(m_craInnerRandomStream,
m_pbProtectedStreamKey);
m_pbProtectedStreamKey = cr.GetRandomBytes(32);
m_craInnerRandomStream = CrsAlgorithm.Salsa20;
m_randomStream = new CryptoRandomStream(m_craInnerRandomStream,
m_pbProtectedStreamKey);
m_pbStreamStartBytes = cr.GetRandomBytes(32);
m_pbStreamStartBytes = cr.GetRandomBytes(32);
Stream writerStream;
BinaryWriter bw = null;
if(m_format == Kdb4Format.Default)
{
bw = new BinaryWriter(hashedStream, encNoBom);
WriteHeader(bw); // Also flushes bw
Stream writerStream;
BinaryWriter bw = null;
if (m_format == Kdb4Format.Default)
{
bw = new BinaryWriter(hashedStream, encNoBom);
WriteHeader(bw); // Also flushes bw
Stream sEncrypted = AttachStreamEncryptor(hashedStream);
if((sEncrypted == null) || (sEncrypted == hashedStream))
throw new SecurityException(KLRes.CryptoStreamFailed);
Stream sEncrypted = AttachStreamEncryptor(hashedStream);
if ((sEncrypted == null) || (sEncrypted == hashedStream))
throw new SecurityException(KLRes.CryptoStreamFailed);
sEncrypted.Write(m_pbStreamStartBytes, 0, m_pbStreamStartBytes.Length);
sEncrypted.Write(m_pbStreamStartBytes, 0, m_pbStreamStartBytes.Length);
Stream sHashed = new HashedBlockStream(sEncrypted, true);
Stream sHashed = new HashedBlockStream(sEncrypted, true);
if(m_pwDatabase.Compression == PwCompressionAlgorithm.GZip)
writerStream = new GZipStream(sHashed, CompressionMode.Compress);
else
writerStream = sHashed;
}
else if(m_format == Kdb4Format.PlainXml)
writerStream = hashedStream;
else { Debug.Assert(false); throw new FormatException("KdbFormat"); }
if (m_pwDatabase.Compression == PwCompressionAlgorithm.GZip)
writerStream = new GZipStream(sHashed, CompressionMode.Compress);
else
writerStream = sHashed;
}
else if (m_format == Kdb4Format.PlainXml)
writerStream = hashedStream;
else
{
Debug.Assert(false);
throw new FormatException("KdbFormat");
}
using (m_xmlWriter = XmlWriter.Create(writerStream, new XmlWriterSettings { Encoding = encNoBom }))
{
WriteDocument(pgDataSource);
using (m_xmlWriter = XmlWriter.Create(writerStream, new XmlWriterSettings {Encoding = encNoBom}))
{
WriteDocument(pgDataSource);
m_xmlWriter.Flush();
writerStream.Dispose();
}
GC.KeepAlive(bw);
}
m_xmlWriter.Flush();
writerStream.Dispose();
}
GC.KeepAlive(bw);
}
catch (Exception ex)
{
}
finally { CommonCleanUpWrite(sSaveTo, hashedStream); }
}