| 1 | using Microsoft.Data.Sqlite; |
| 2 | using System.Security.Cryptography; |
| 3 | using System.Text; |
| 4 | |
| 5 | namespace SimPas2_Windows.Managers |
| 6 | { |
| 7 | public class PinManager |
| 8 | { |
| 9 | private readonly string mConnectionString; |
| 10 | private readonly byte[] mEncryptionKey; |
| 11 | private readonly string mCulture; |
| 12 | |
| 13 | private string mJpLang; |
| 14 | |
| 15 | public PinManager(string databasePath, byte[] encryptionKey, string culture) |
| 16 | { |
| 17 | mConnectionString = $"Data Source={databasePath}"; |
| 18 | mEncryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey)); |
| 19 | mCulture = culture; |
| 20 | |
| 21 | mJpLang = "ja-JP"; |
| 22 | } |
| 23 | |
| 24 | private bool AlreadyExists(string website, int? excludeId = null) |
| 25 | { |
| 26 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 27 | { |
| 28 | conn.Open(); |
| 29 | SqliteCommand com = conn.CreateCommand(); |
| 30 | com.CommandText = @" |
| 31 | SELECT COUNT(*) FROM Pin |
| 32 | WHERE UPPER(Website) = UPPER(@website)"; |
| 33 | com.Parameters.AddWithValue("@website", website); |
| 34 | |
| 35 | if (excludeId.HasValue) |
| 36 | { |
| 37 | com.CommandText += " AND Id != @excludeId"; |
| 38 | com.Parameters.AddWithValue("@excludeId", excludeId.Value); |
| 39 | } |
| 40 | |
| 41 | return Convert.ToInt32(com.ExecuteScalar()) > 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public void AddPin(string website, string pincode, string note) |
| 46 | { |
| 47 | if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(pincode)) |
| 48 | { |
| 49 | string err = mCulture == mJpLang |
| 50 | ? "ウェブサイト及び暗証番号を御入力下さい。" |
| 51 | : "Please fill in the website and pincode."; |
| 52 | throw new ArgumentException(err); |
| 53 | } |
| 54 | |
| 55 | if (AlreadyExists(website)) |
| 56 | { |
| 57 | string err = mCulture == mJpLang |
| 58 | ? $"ウェブサイト「{website}」付き暗証番号は既に存在します。" |
| 59 | : $"A pincode with the website '{website}' already exists."; |
| 60 | throw new ArgumentException(err); |
| 61 | } |
| 62 | |
| 63 | string encryptedPin = EncryptPin(pincode); |
| 64 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 65 | { |
| 66 | conn.Open(); |
| 67 | SqliteCommand com = conn.CreateCommand(); |
| 68 | com.CommandText = @"INSERT INTO Pin (Website, Pincode, Note) VALUES ($website, $pincode, $note)"; |
| 69 | com.Parameters.AddWithValue("$website", website); |
| 70 | com.Parameters.AddWithValue("$pincode", encryptedPin); |
| 71 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 72 | com.ExecuteNonQuery(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public bool EditPin(int id, string website, string pincode, string note) |
| 77 | { |
| 78 | if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(pincode)) |
| 79 | { |
| 80 | string err = mCulture == mJpLang |
| 81 | ? "ウェブサイト及び暗証番号を御入力下さい。" |
| 82 | : "Please fill in the website and pincode."; |
| 83 | throw new ArgumentException(err); |
| 84 | } |
| 85 | |
| 86 | if (AlreadyExists(website, id)) |
| 87 | { |
| 88 | string err = mCulture == mJpLang |
| 89 | ? $"ウェブサイト「{website}」付き暗証番号は既に存在します。" |
| 90 | : $"A pincode with the website '{website}' already exists."; |
| 91 | throw new ArgumentException(err); |
| 92 | } |
| 93 | |
| 94 | string encryptedPin = EncryptPin(pincode); |
| 95 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 96 | { |
| 97 | conn.Open(); |
| 98 | SqliteCommand com = conn.CreateCommand(); |
| 99 | com.CommandText = @"UPDATE Pin SET Website = $website, Pincode = $pincode, Note = $note WHERE Id = $id"; |
| 100 | com.Parameters.AddWithValue("id", id); |
| 101 | com.Parameters.AddWithValue("$website", website); |
| 102 | com.Parameters.AddWithValue("$pincode", encryptedPin); |
| 103 | com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note); |
| 104 | |
| 105 | return com.ExecuteNonQuery() > 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public bool DeletePin(int id) |
| 110 | { |
| 111 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 112 | { |
| 113 | conn.Open(); |
| 114 | SqliteCommand com = conn.CreateCommand(); |
| 115 | com.CommandText = "DELETE FROM Pin WHERE Id = $id"; |
| 116 | com.Parameters.AddWithValue("$id", id); |
| 117 | |
| 118 | return com.ExecuteNonQuery() > 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public List<(int Id, string Website, string Pincode, string Note)> GetAll(string keyword = "") |
| 123 | { |
| 124 | var pins = new List<(int, string, string, string)>(); |
| 125 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 126 | { |
| 127 | conn.Open(); |
| 128 | SqliteCommand com = conn.CreateCommand(); |
| 129 | |
| 130 | if (string.IsNullOrWhiteSpace(keyword)) |
| 131 | { |
| 132 | com.CommandText = @" |
| 133 | SELECT Id, Website, Pincode, Note FROM Pin |
| 134 | ORDER BY Website ASC"; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | com.CommandText = @" |
| 139 | SELECT Id, Website, Pincode, Note FROM Pin |
| 140 | WHERE Website LIKE @keyword |
| 141 | ORDER BY Website ASC"; |
| 142 | com.Parameters.AddWithValue("@keyword", $"%{keyword}%"); |
| 143 | } |
| 144 | |
| 145 | using (SqliteDataReader reader = com.ExecuteReader()) |
| 146 | { |
| 147 | while (reader.Read()) |
| 148 | { |
| 149 | string encryptedPin = reader.GetString(2); |
| 150 | string decryptedPin = DecryptPin(encryptedPin); |
| 151 | pins.Add(( |
| 152 | reader.GetInt32(0), |
| 153 | reader.GetString(1), |
| 154 | decryptedPin, |
| 155 | reader.IsDBNull(3) ? string.Empty : reader.GetString(3) |
| 156 | )); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return pins; |
| 162 | } |
| 163 | |
| 164 | private string EncryptPin(string plainPassword) |
| 165 | { |
| 166 | using (Aes aes = Aes.Create()) |
| 167 | { |
| 168 | aes.Key = mEncryptionKey; |
| 169 | aes.GenerateIV(); |
| 170 | byte[] iv = aes.IV; |
| 171 | |
| 172 | using (var encryptor = aes.CreateEncryptor(aes.Key, iv)) |
| 173 | { |
| 174 | byte[] plainBytes = Encoding.UTF8.GetBytes(plainPassword); |
| 175 | byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); |
| 176 | byte[] result = new byte[iv.Length + encryptedBytes.Length]; |
| 177 | |
| 178 | Buffer.BlockCopy(iv, 0, result, 0, iv.Length); |
| 179 | Buffer.BlockCopy(encryptedBytes, 0, result, iv.Length, encryptedBytes.Length); |
| 180 | |
| 181 | return Convert.ToBase64String(result); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private string DecryptPin(string encryptedPassword) |
| 187 | { |
| 188 | byte[] combined = Convert.FromBase64String(encryptedPassword); |
| 189 | byte[] iv = new byte[16]; |
| 190 | byte[] encryptedBytes = new byte[combined.Length - iv.Length]; |
| 191 | |
| 192 | Buffer.BlockCopy(combined, 0, iv, 0, iv.Length); |
| 193 | Buffer.BlockCopy(combined, iv.Length, encryptedBytes, 0, encryptedBytes.Length); |
| 194 | |
| 195 | using (Aes aes = Aes.Create()) |
| 196 | { |
| 197 | aes.Key = mEncryptionKey; |
| 198 | aes.IV = iv; |
| 199 | |
| 200 | using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) |
| 201 | { |
| 202 | byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); |
| 203 | return Encoding.UTF8.GetString(decryptedBytes); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |