Skip to content

Commit ee83404

Browse files
authored
Update code
-account form by default hide password -remove unused using
1 parent 4993855 commit ee83404

File tree

5 files changed

+72
-57
lines changed

5 files changed

+72
-57
lines changed

Source/Database/QPDB.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ namespace QPass.Database
99
/// QPass Database
1010
/// </summary>
1111
[Serializable]
12-
class QPDB
12+
public class QPDB
1313
{
1414
#region Member
1515

1616
private byte[] _Header;
1717
private byte[] _Checksum;
1818
private byte[] _MasterPassword;
1919
private byte[] _DatabaseFile;
20+
private short _Version = 1;
21+
private string _Extension = "QPDB";
2022

2123
#endregion Member
2224

Source/Entities/Account.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using System;
22

33
using QPass.Core.Utilities.Extension;
4-
5-
using Org.BouncyCastle.Crypto.Engines;
6-
using Org.BouncyCastle.Crypto.Modes;
7-
using Org.BouncyCastle.Crypto.Paddings;
8-
using Org.BouncyCastle.Crypto.Parameters;
94
using QPass.Crypto;
105

116
namespace QPass.Entities

Source/Entities/FileManager.cs

+64-48
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
namespace QPass.Entities
88
{
9-
class FileManager
9+
public class FileManager
1010
{
1111
#region Member
1212

1313
private string _Location;
14-
private byte[] _File; //QPDB
14+
private QPDB _QPDBFile; //QPDB file
15+
private string _Extension = ".QPDB";
1516

1617
#endregion Member
1718

@@ -25,97 +26,112 @@ public FileManager(string location = "")
2526
{
2627
this._Location = location;
2728
}
28-
29-
/// <summary>
30-
///
31-
/// </summary>
32-
/// <param name="location"></param>
33-
/// <param name="file"></param>
34-
protected FileManager(string location, byte[] file)
35-
{
36-
this._Location = location;
37-
this._File = file;
38-
}
39-
29+
4030
/// <summary>
4131
/// Destructor.
4232
/// </summary>
4333
~FileManager()
4434
{
45-
Array.Clear(this._File, 0, this._File.Length);
4635
this._Location = string.Empty;
4736
}
4837

4938
#endregion Constructor & Destructor
5039

5140
#region Protected
5241

53-
protected void Save()
42+
/// <summary>
43+
/// write file to hard disk
44+
/// </summary>
45+
/// <param name="filename"></param>
46+
/// <param name="data"></param>
47+
public void Write(string filename, byte[] data)
5448
{
55-
using (FileStream fs = new FileStream(this._Location, FileMode.Create))
49+
using (FileStream fs = new FileStream(filename, FileMode.Create))
5650
{
5751
using (BinaryWriter bw = new BinaryWriter(fs))
5852
{
59-
bw.Write(this._File);
53+
bw.Write(data);
6054
fs.Close();
6155
bw.Close();
6256
}
6357
}
6458
}
6559

66-
protected void Load()
60+
/// <summary>
61+
/// read file from hard disk
62+
/// </summary>
63+
/// <param name="filename"></param>
64+
/// <returns></returns>
65+
public byte[] Read(string filename)
6766
{
68-
if (File.Exists(this._Location) == false)
67+
if (File.Exists(filename) == false)
6968
{
7069
throw new Exception("File not exist.");
7170
}
7271

73-
using (FileStream fs = new FileStream(this._Location, FileMode.Open))
72+
byte[] buffer;
73+
74+
using (FileStream fs = new FileStream(filename, FileMode.Open))
7475
{
7576
using (BinaryReader br = new BinaryReader(fs))
7677
{
77-
this._File = br.ReadBytes((int)br.BaseStream.Length);
78+
buffer = br.ReadBytes((int)br.BaseStream.Length);
7879
fs.Close();
7980
br.Close();
8081
}
8182
}
83+
84+
return buffer;
8285
}
83-
84-
#endregion Protected
8586

86-
#region Public
87+
/// <summary>
88+
/// save file
89+
/// </summary>
90+
protected void Save()
91+
{
92+
byte[] buffer = Core.Utilities.Convert.Objects.Serialize(this._QPDBFile);
93+
this.Write(this._Location, buffer);
94+
System.Array.Clear(buffer, 0, buffer.Length);
95+
}
8796

88-
public void SaveData(QPDB qpdb)
97+
/// <summary>
98+
/// load file
99+
/// </summary>
100+
protected void Load()
89101
{
90-
if (qpdb == null)
91-
{
92-
throw new Exception("QPDB can't be null.");
93-
}
102+
//read or load QPDB file
103+
byte[] buffer = this.Read(this._Location);
104+
this._QPDBFile = (QPDB)QPass.Core.Utilities.Convert.Objects.Deserialize(buffer);
94105

95-
try
96-
{
97-
this.SaveData(qpdb.GetBytes());
98-
}
99-
catch
100-
{
101-
throw new Exception("QPDB can't be serialize.");
102-
}
106+
//delete QPDB file in hard disk
107+
File.Delete(this._Location);
108+
109+
System.Array.Clear(buffer, 0, buffer.Length);
103110
}
104111

105-
public void SaveData(byte[] file)
106-
{
107-
if (file == null || file.Length == 0)
108-
{
109-
throw new Exception("File can't empty.");
110-
}
112+
#endregion Protected
111113

112-
this._File = file;
113-
this.Save();
114+
#region Public
115+
116+
public void SetMasterPassword(byte[] password)
117+
{
118+
this._QPDBFile.SetMasterPassword(password);
114119
}
115120

116-
public void LoadData()
121+
public bool ValidMasterPassword(byte[] password)
117122
{
118-
this.Load();
123+
bool valid = true;
124+
byte[] temp = this._QPDBFile.GetMasterPassword();
125+
126+
for (int i = 0; i < password.Length; i++)
127+
{
128+
if (password[i] != temp[i])
129+
{
130+
valid = false;
131+
break;
132+
}
133+
}
134+
return valid;
119135
}
120136

121137
#endregion Public

Source/Forms/AccountForm.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ private void AccountForm_Load(object sender, EventArgs e)
7878
//load font from setting
7979
this.Font = Properties.Settings.Default.Font;
8080

81-
//load picture for password show
82-
this.PasswordStatusButton.Image = Properties.Resources.icons8_eye_hide_32;
81+
//hide password by default
82+
this.PasswordTextBox.PasswordChar = '*';
83+
this._PasswordStatus = PasswordStatus.Hide;
84+
this.PasswordStatusButton.Image = Properties.Resources.icons8_eye_32;
8385
}
8486

8587
private void SaveButton_Click(object sender, EventArgs e)

Source/Forms/GenerateRandomPasswordForm.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private void PasswordBuilder(string parameter)
111111
{
112112
result.Append(chars[b % (chars.Length)]);
113113
}
114-
this.ResultTextBox.Text = result.ToString();
114+
this.ResultTextBox.Text = result.ToString().Trim();
115115
}
116116
}
117117
}

0 commit comments

Comments
 (0)