Ejemplo n.º 1
0
        public async Task <bool> WriteAsync(StreamWriter sw, EntryBase entry)
        {
            if (entry.Encrypted)
            {
                if (!await Crypto.HasKey())
                {
                    return(false);
                }
            }

            var serializer = new SerializerBuilder()
                             .WithNamingConvention(CamelCaseNamingConvention.Instance)
                             .WithTypeConverter(NodaTimeConverter)
                             .WithAttributeOverride(typeof(TextEntry), nameof(TextEntry.Body), new YamlIgnoreAttribute())
                             .Build();

            await sw.WriteAsync(Identifier);

            await sw.WriteAsync("\n");

            await sw.WriteAsync(serializer.Serialize(entry).ToCrossPlatformEOL());

            await sw.WriteAsync(Identifier);

            await sw.WriteAsync("\n");

            if (entry is TextEntry textEntry && textEntry.Body != null)
            {
                if (textEntry.Encrypted)
                {
                    try
                    {
                        await sw.WriteAsync(await Crypto.Encrypt(textEntry.Body.ToCrossPlatformEOL()));
                    }
                    catch (InvalidPasswordException)
                    {
                        throw;
                    }
                }
                else
                {
                    await sw.WriteAsync(textEntry.Body.ToCrossPlatformEOL());
                }
            }

            return(true);
        }