|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Security;
using System.Runtime.InteropServices;
namespace Aleph.SSM
{
[Serializable]
public class Storage
{
[Serializable]
public struct Entry
{
string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
string user;
public string User
{
get { return this.user; }
set { this.user = value; }
}
string password;
public string Password
{
get { return this.password; }
}
string site;
public string Site
{
get { return this.site; }
set { this.site = value; }
}
string comment;
public string Comment
{
get { return this.comment; }
set { this.comment = value; }
}
public Entry(string name, string user, SecureString password, string site, string comment)
{
this.name = name;
this.user = user;
this.comment = comment;
this.site = site;
using (CryptoCore cryptor = new CryptoCore())
{
IntPtr ptr = Marshal.SecureStringToBSTR(password);
this.password = cryptor.EncryptString(Marshal.PtrToStringAuto(ptr));
Marshal.ZeroFreeBSTR(ptr);
}
}
public void UpdatePassword(SecureString password)
{
using (CryptoCore cryptor = new CryptoCore())
{
IntPtr ptr = Marshal.SecureStringToBSTR(password);
this.password = cryptor.EncryptString(Marshal.PtrToStringAuto(ptr));
Marshal.ZeroFreeBSTR(ptr);
}
}
}
public List<Entry> Entries;
public Storage()
{
Entries = new List<Entry>();
}
public Storage(string filePath)
{
Entries = new List<Entry>();
using (CryptoCore cryptor = new CryptoCore())
{
byte[] storage = cryptor.DecryptBuffer(File.ReadAllBytes(filePath));
using (MemoryStream ms = new MemoryStream(storage))
{
BinaryFormatter bf = new BinaryFormatter();
Entries = ((Storage)bf.Deserialize(ms)).Entries;
}
}
}
public void Serialize(string filePath)
{
byte[] storage;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
storage = ms.ToArray();
}
using (CryptoCore cryptor = new CryptoCore())
{
File.WriteAllBytes(filePath, cryptor.EncryptBuffer(storage));
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.