Skip to content

Commit 07cf09d

Browse files
committed
feat: add key storage Helper
1 parent 0a10a03 commit 07cf09d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

EccSDK/KeyStorageHelper.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Text.Json;
2+
using EccSDK.models;
3+
4+
namespace EccSDK;
5+
6+
public static class KeyStorageHelper
7+
{
8+
private const string KeysPath = "~/keys.json";
9+
10+
public static KeyStorageData LoadKeyPair()
11+
{
12+
if (File.Exists(KeysPath))
13+
{
14+
return RestoreKeys();
15+
}
16+
17+
var keyStorageData = new KeyStorageData
18+
{
19+
KeyPair = EccGenerator.GenerateKeyPair(256),
20+
SessionKey = SessionKeyGenerator.GenerateSessionKey()
21+
};
22+
23+
keyStorageData.SaveKeys(KeysPath);
24+
25+
return keyStorageData;
26+
}
27+
28+
private static KeyStorageData RestoreKeys()
29+
{
30+
var keyData = File.ReadAllText(KeysPath);
31+
return JsonSerializer.Deserialize<KeyStorageData>(keyData)!;
32+
}
33+
}

EccSDK/models/KeyStorageData.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json;
2+
3+
namespace EccSDK.models;
4+
5+
public class KeyStorageData
6+
{
7+
public KeyPair KeyPair { get; set; }
8+
public SessionKey SessionKey { get; set; }
9+
10+
public void SaveKeys(string keyPath)
11+
{
12+
var keyWithJson = JsonSerializer.Serialize(this);
13+
File.WriteAllText(keyPath, keyWithJson);
14+
}
15+
}

0 commit comments

Comments
 (0)