This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
SimPas2-Windows/Managers/PinManager.cs
2026-01-21 03:07:35 +09:00

209 lines
7.1 KiB
C#

using Microsoft.Data.Sqlite;
using System.Security.Cryptography;
using System.Text;
namespace SimPas2_Windows.Managers
{
public class PinManager
{
private readonly string mConnectionString;
private readonly byte[] mEncryptionKey;
private readonly string mCulture;
private string mJpLang;
public PinManager(string databasePath, byte[] encryptionKey, string culture)
{
mConnectionString = $"Data Source={databasePath}";
mEncryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey));
mCulture = culture;
mJpLang = "ja-JP";
}
private bool AlreadyExists(string website, int? excludeId = null)
{
using (SqliteConnection conn = new SqliteConnection(mConnectionString))
{
conn.Open();
SqliteCommand com = conn.CreateCommand();
com.CommandText = @"
SELECT COUNT(*) FROM Pin
WHERE UPPER(Website) = UPPER(@website)";
com.Parameters.AddWithValue("@website", website);
if (excludeId.HasValue)
{
com.CommandText += " AND Id != @excludeId";
com.Parameters.AddWithValue("@excludeId", excludeId.Value);
}
return Convert.ToInt32(com.ExecuteScalar()) > 0;
}
}
public void AddPin(string website, string pincode, string note)
{
if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(pincode))
{
string err = mCulture == mJpLang
? "ウェブサイト及び暗証番号を御入力下さい。"
: "Please fill in the website and pincode.";
throw new ArgumentException(err);
}
if (AlreadyExists(website))
{
string err = mCulture == mJpLang
? $"ウェブサイト「{website}」付き暗証番号は既に存在します。"
: $"A pincode with the website '{website}' already exists.";
throw new ArgumentException(err);
}
string encryptedPin = EncryptPin(pincode);
using (SqliteConnection conn = new SqliteConnection(mConnectionString))
{
conn.Open();
SqliteCommand com = conn.CreateCommand();
com.CommandText = @"INSERT INTO Pin (Website, Pincode, Note) VALUES ($website, $pincode, $note)";
com.Parameters.AddWithValue("$website", website);
com.Parameters.AddWithValue("$pincode", encryptedPin);
com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note);
com.ExecuteNonQuery();
}
}
public bool EditPin(int id, string website, string pincode, string note)
{
if (string.IsNullOrWhiteSpace(website) || string.IsNullOrWhiteSpace(pincode))
{
string err = mCulture == mJpLang
? "ウェブサイト及び暗証番号を御入力下さい。"
: "Please fill in the website and pincode.";
throw new ArgumentException(err);
}
if (AlreadyExists(website, id))
{
string err = mCulture == mJpLang
? $"ウェブサイト「{website}」付き暗証番号は既に存在します。"
: $"A pincode with the website '{website}' already exists.";
throw new ArgumentException(err);
}
string encryptedPin = EncryptPin(pincode);
using (SqliteConnection conn = new SqliteConnection(mConnectionString))
{
conn.Open();
SqliteCommand com = conn.CreateCommand();
com.CommandText = @"UPDATE Pin SET Website = $website, Pincode = $pincode, Note = $note WHERE Id = $id";
com.Parameters.AddWithValue("id", id);
com.Parameters.AddWithValue("$website", website);
com.Parameters.AddWithValue("$pincode", encryptedPin);
com.Parameters.AddWithValue("$note", string.IsNullOrEmpty(note) ? DBNull.Value : note);
return com.ExecuteNonQuery() > 0;
}
}
public bool DeletePin(int id)
{
using (SqliteConnection conn = new SqliteConnection(mConnectionString))
{
conn.Open();
SqliteCommand com = conn.CreateCommand();
com.CommandText = "DELETE FROM Pin WHERE Id = $id";
com.Parameters.AddWithValue("$id", id);
return com.ExecuteNonQuery() > 0;
}
}
public List<(int Id, string Website, string Pincode, string Note)> GetAll(string keyword = "")
{
var pins = new List<(int, string, string, string)>();
using (SqliteConnection conn = new SqliteConnection(mConnectionString))
{
conn.Open();
SqliteCommand com = conn.CreateCommand();
if (string.IsNullOrWhiteSpace(keyword))
{
com.CommandText = @"
SELECT Id, Website, Pincode, Note FROM Pin
ORDER BY Website ASC";
}
else
{
com.CommandText = @"
SELECT Id, Website, Pincode, Note FROM Pin
WHERE Website LIKE @keyword
ORDER BY Website ASC";
com.Parameters.AddWithValue("@keyword", $"%{keyword}%");
}
using (SqliteDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
string encryptedPin = reader.GetString(2);
string decryptedPin = DecryptPin(encryptedPin);
pins.Add((
reader.GetInt32(0),
reader.GetString(1),
decryptedPin,
reader.IsDBNull(3) ? string.Empty : reader.GetString(3)
));
}
}
}
return pins;
}
private string EncryptPin(string plainPassword)
{
using (Aes aes = Aes.Create())
{
aes.Key = mEncryptionKey;
aes.GenerateIV();
byte[] iv = aes.IV;
using (var encryptor = aes.CreateEncryptor(aes.Key, iv))
{
byte[] plainBytes = Encoding.UTF8.GetBytes(plainPassword);
byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
byte[] result = new byte[iv.Length + encryptedBytes.Length];
Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
Buffer.BlockCopy(encryptedBytes, 0, result, iv.Length, encryptedBytes.Length);
return Convert.ToBase64String(result);
}
}
}
private string DecryptPin(string encryptedPassword)
{
byte[] combined = Convert.FromBase64String(encryptedPassword);
byte[] iv = new byte[16]; // AES IV is 16 bytes
byte[] encryptedBytes = new byte[combined.Length - iv.Length];
Buffer.BlockCopy(combined, 0, iv, 0, iv.Length);
Buffer.BlockCopy(combined, iv.Length, encryptedBytes, 0, encryptedBytes.Length);
using (Aes aes = Aes.Create())
{
aes.Key = mEncryptionKey;
aes.IV = iv;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
}
}
}