-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9194516
commit 60b097d
Showing
7 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using DevBase.Cryptography.BouncyCastle.AES; | ||
|
||
namespace OpenLyricsClient.Backend.Helper; | ||
|
||
public class Sealing | ||
{ | ||
private string _serverPublicKey; | ||
private string _simpleEncryptionKey; | ||
|
||
private DevBase.Cryptography.BouncyCastle.Sealing.Sealing _sealing; | ||
private AESBuilderEngine _aesBuilder; | ||
|
||
public Sealing() | ||
{ | ||
this._serverPublicKey = "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABBqSdbiWAMxcEig+rX1FlApI7pE/kPNUmejo5PXvElsf6pjHuDlBN4fYvpmaX6lncddAuNPnQmZ89Ogb95xwPnA="; | ||
this._sealing = new DevBase.Cryptography.BouncyCastle.Sealing.Sealing(this._serverPublicKey); | ||
|
||
this._simpleEncryptionKey = "g0o6Z9cInEYPJlLTNcm8iZND5AWeRfepa6xQhjpXd/k="; | ||
|
||
this._aesBuilder = new AESBuilderEngine() | ||
.SetKey(this._simpleEncryptionKey) | ||
.SetRandomSeed(); | ||
|
||
} | ||
public string SimpleEncrypt(string data) => this._aesBuilder.EncryptString(data); | ||
|
||
public string SimpleDecrypt(string encryptedData) => this._aesBuilder.DecryptString(encryptedData); | ||
|
||
public string Seal(string data) => this._sealing.Seal(data); | ||
|
||
public string UnSeal(string sealedData) => this._sealing.UnSeal(sealedData); | ||
|
||
public string ServerPublicKey | ||
{ | ||
get => _serverPublicKey; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
OpenLyricsClient/Backend/Settings/Sections/Account/AccountSection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using DevBase.Api.Apis.OpenLyricsClient.Structure.Json; | ||
using Newtonsoft.Json.Linq; | ||
using OpenLyricsClient.Backend; | ||
using OpenLyricsClient.Backend.Settings; | ||
using OpenLyricsClient.Backend.Settings.Sections.Account; | ||
using OpenLyricsClient.Backend.Structure; | ||
using OpenLyricsClient.Backend.Utils; | ||
using OpenLyricsClient = DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient; | ||
|
||
public class AccountSection : ISettingSection | ||
{ | ||
private FileInfo _file; | ||
private JObject _data; | ||
|
||
public AccountSection(string filePath) | ||
{ | ||
this._file = new FileInfo(filePath); | ||
} | ||
|
||
public async Task WriteToDisk() | ||
{ | ||
string encoded = Core.INSTANCE.Sealing.SimpleEncrypt(this._data.ToString()); | ||
await File.WriteAllTextAsync(this._file.FullName, encoded); | ||
} | ||
|
||
public async Task ReadFromDisk() | ||
{ | ||
if (!this._file.Exists) | ||
{ | ||
this._data = Defaults(); | ||
await WriteToDisk(); | ||
return; | ||
} | ||
|
||
await using FileStream stream = this._file.OpenRead(); | ||
using StreamReader reader = new StreamReader(stream); | ||
|
||
string decoded = Core.INSTANCE.Sealing.SimpleDecrypt(reader.ReadToEnd()); | ||
|
||
this._data = JObject.Parse(decoded); | ||
|
||
await stream.FlushAsync(); | ||
|
||
stream.Close(); | ||
reader.Close(); | ||
} | ||
|
||
public T GetValue<T>(string field) | ||
{ | ||
return (T)this._data[field].ToObject<T>(); | ||
} | ||
|
||
public async Task SetValue<T>(string field, T value) | ||
{ | ||
this._data[field] = JToken.FromObject(value); | ||
await WriteToDisk(); | ||
} | ||
|
||
public JObject Defaults() | ||
{ | ||
JsonOpenLyricsClientSubscription account = | ||
new DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient(Core.INSTANCE.Sealing.ServerPublicKey) | ||
.CreateSubscription() | ||
.GetAwaiter() | ||
.GetResult(); | ||
|
||
Structure structure = new Structure | ||
{ | ||
UserID = account.UserID, | ||
UserSecret = account.UserSecret | ||
}; | ||
|
||
return new JsonDeserializer().Serialize(structure); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
OpenLyricsClient/Backend/Settings/Sections/Account/Structure.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenLyricsClient.Backend.Settings.Sections.Account; | ||
|
||
public class Structure | ||
{ | ||
public string UserID { get; set; } | ||
public string UserSecret { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters