|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace PassXYZLib |
| 7 | +{ |
| 8 | + public static class PxDataFile |
| 9 | + { |
| 10 | + public static string DataFilePath |
| 11 | + { |
| 12 | + get |
| 13 | + { |
| 14 | + string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "data"); |
| 15 | + if(!Directory.Exists(path)) |
| 16 | + { |
| 17 | + Directory.CreateDirectory(path); |
| 18 | + } |
| 19 | + return path; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static string KeyFilePath |
| 24 | + { |
| 25 | + get |
| 26 | + { |
| 27 | + string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "key"); |
| 28 | + if (!Directory.Exists(path)) |
| 29 | + { |
| 30 | + Directory.CreateDirectory(path); |
| 31 | + } |
| 32 | + return path; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Decode the username from filename |
| 38 | + /// </summary> |
| 39 | + /// <param name="fileName">File name used to decode username</param> |
| 40 | + /// <returns>Decoded username</returns> |
| 41 | + public static string GetUserName(string fileName) |
| 42 | + { |
| 43 | + string trimedName; |
| 44 | + |
| 45 | + if (fileName.StartsWith(PxDefs.head_xyz) || fileName.StartsWith(PxDefs.head_data)) |
| 46 | + { |
| 47 | + trimedName = fileName.Substring(PxDefs.head_xyz.Length); |
| 48 | + trimedName = trimedName.Substring(0, trimedName.LastIndexOf(PxDefs.xyz)); |
| 49 | + Debug.WriteLine($"PxDataFile: {fileName}, {trimedName} in GetUserName()."); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + Debug.WriteLine($"PxDataFile: {fileName} is not PassXYZ data file in GetUserName()."); |
| 54 | + return string.Empty; |
| 55 | + } |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + if (trimedName != null) |
| 60 | + { |
| 61 | + trimedName = Base58CheckEncoding.GetString(trimedName); |
| 62 | + } |
| 63 | + } |
| 64 | + catch (FormatException e) |
| 65 | + { |
| 66 | + Debug.WriteLine($"PxDataFile: {e.Message}"); |
| 67 | + trimedName = string.Empty; |
| 68 | + } |
| 69 | + |
| 70 | + return trimedName; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Get a list of existing users from the encoded data files |
| 75 | + /// </summary> |
| 76 | + /// <returns>user list</returns> |
| 77 | + public static List<string> GetUsersList() |
| 78 | + { |
| 79 | + List<string> userList = new List<string>(); |
| 80 | + |
| 81 | + var dataFiles = Directory.EnumerateFiles(DataFilePath, PxDefs.all_xyz); |
| 82 | + foreach (string currentFile in dataFiles) |
| 83 | + { |
| 84 | + string fileName = currentFile.Substring(DataFilePath.Length + 1); |
| 85 | + string userName = GetUserName(fileName); |
| 86 | + if (userName != string.Empty && !string.IsNullOrWhiteSpace(userName)) |
| 87 | + { |
| 88 | + userList.Add(userName); |
| 89 | + } |
| 90 | + } |
| 91 | + return userList; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public class User |
| 96 | + { |
| 97 | + private string _username; |
| 98 | + public string Username |
| 99 | + { |
| 100 | + get => _username; |
| 101 | + set |
| 102 | + { |
| 103 | + _username = value; |
| 104 | + |
| 105 | + // Check whether Device Lock is enabled, but key file may not exist. |
| 106 | + if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true)))) |
| 107 | + { |
| 108 | + IsDeviceLockEnabled = true; |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + IsDeviceLockEnabled = false; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public string Password { get; set; } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Check whether Device Lock is enabled for this user. |
| 121 | + /// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled |
| 122 | + /// </summary> |
| 123 | + public bool IsDeviceLockEnabled { get; set; } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Check whether the key file is existed. |
| 127 | + /// true - key file is available, false - key file is not available |
| 128 | + /// </summary> |
| 129 | + public bool IsKeyFileExist |
| 130 | + { |
| 131 | + get |
| 132 | + { |
| 133 | + if (IsDeviceLockEnabled) |
| 134 | + { |
| 135 | + var keyFilePath = System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName); |
| 136 | + if (System.IO.File.Exists(keyFilePath)) |
| 137 | + { |
| 138 | + return true; |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Data file name. Converted Username to file name |
| 154 | + /// </summary> |
| 155 | + public string FileName => GetFileName(IsDeviceLockEnabled); |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Data file path |
| 159 | + /// </summary> |
| 160 | + public string Path |
| 161 | + { |
| 162 | + get |
| 163 | + { |
| 164 | + return System.IO.Path.Combine(PxDataFile.DataFilePath, FileName); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Get the key file name. The key file name is generated using base58 encoding from username. |
| 170 | + /// If Device Lock is not enabled, return <c>string.Empty</c>. |
| 171 | + /// </summary> |
| 172 | + public string KeyFileName |
| 173 | + { |
| 174 | + get |
| 175 | + { |
| 176 | + if(IsDeviceLockEnabled) |
| 177 | + { |
| 178 | + return PxDefs.head_k4xyz + Base58CheckEncoding.ToBase58String(Username) + PxDefs.k4xyz; |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + return string.Empty; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Get the data file name from the username. The data file name is generated |
| 189 | + /// using base58 encoding from username. |
| 190 | + /// </summary> |
| 191 | + /// <param name="isDeviceLockEnabled">Device lock enabled or not</param> |
| 192 | + /// <returns>Data file name</returns> |
| 193 | + private string GetFileName(bool isDeviceLockEnabled = false) |
| 194 | + { |
| 195 | + if (isDeviceLockEnabled) |
| 196 | + { |
| 197 | + return PxDefs.head_data + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz; |
| 198 | + } |
| 199 | + else |
| 200 | + { |
| 201 | + return PxDefs.head_xyz + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + public User() |
| 206 | + { |
| 207 | + IsDeviceLockEnabled = false; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments