Skip to content

Commit 3a4b05b

Browse files
committed
feat: change way to generate keys and save it into file type
1 parent 0b820b3 commit 3a4b05b

File tree

14 files changed

+129
-206
lines changed

14 files changed

+129
-206
lines changed

EccSDK/ChameleonHashHelper.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using EccSDK.models;
2-
using Org.BouncyCastle.Math;
2+
using EccSDK.models.ChameleonHash;
3+
using EccSDK.Models.ChameleonHash;
34
using Org.BouncyCastle.Math.EC;
45

56
namespace EccSDK;
@@ -13,12 +14,13 @@ public static ChameleonSignature Sign(ChameleonHashRequest request)
1314
{
1415
// sign = (sessionKey - dn) mod n
1516
// dn = H(m) * kn
16-
var msgHash = HashHelper.Sha256(request.Message);
17-
var dn = msgHash.Multiply(request.KeyPair.PrivateKey);
18-
return new ChameleonSignature()
19-
{
20-
Value = request.SessionKey.Add(dn).Mod(request.Order)
21-
};
17+
// var msgHash = HashHelper.Sha256(request.Message);
18+
// var dn = msgHash.Multiply(new BigInteger(request.KeyPairDomain.PrivateKey));
19+
// return new ChameleonSignature()
20+
// {
21+
// Value = request.SessionKey.Add(dn).Mod(request.Order)
22+
// };
23+
return new ChameleonSignature();
2224
}
2325

2426
public static bool Verify(ChameleonHashRequest request, ECPoint rightChameleonHash)
@@ -29,24 +31,8 @@ public static bool Verify(ChameleonHashRequest request, ECPoint rightChameleonHa
2931

3032
public static ChameleonHash GetChameleonHash(ChameleonHashRequest request)
3133
{
32-
// chameleonHash = [Kn x H(m)] + [P x sessionKey]
33-
var msgHash = HashHelper.Sha256(request.Message);
34-
var rP = request.KeyPair.BasePoint.Multiply(request.Signature);
35-
36-
return new ChameleonHash()
37-
{
38-
Value = request.KeyPair.PublicKey.Multiply(msgHash).Add(rP).Normalize()
39-
};
34+
35+
return new ChameleonHash();
4036
}
4137

42-
}
43-
44-
public class ChameleonHash
45-
{
46-
public ECPoint Value { get; set; }
47-
}
48-
49-
public class ChameleonSignature
50-
{
51-
public BigInteger Value { get; set; }
5238
}

EccSDK/EccGenerator.cs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
1-
using EccSDK.models;
2-
using Org.BouncyCastle.Crypto;
1+
using System.Text.Json;
2+
using EccSDK.models.Keys;
3+
using EccSDK.Models.Keys;
4+
using Org.BouncyCastle.Asn1.Sec;
5+
using Org.BouncyCastle.Asn1.X9;
36
using Org.BouncyCastle.Crypto.Generators;
47
using Org.BouncyCastle.Crypto.Parameters;
58
using Org.BouncyCastle.Security;
9+
using BigInteger = Org.BouncyCastle.Math.BigInteger;
610

711
namespace EccSDK;
812

913
public static class EccGenerator
1014
{
11-
public static KeyPair GenerateKeyPair(int keySize)
12-
{
13-
var gen = new ECKeyPairGenerator("ECDSA");
14-
gen.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
15+
private static readonly string KeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "key.json");
16+
private static readonly X9ECParameters Curve = SecNamedCurves.GetByName("secp256k1");
17+
private static readonly ECDomainParameters Domain = new(Curve.Curve, Curve.G, Curve.N, Curve.H);
1518

16-
var keyGen = gen.GenerateKeyPair();
19+
public static KeyPairDomain GetKeyDomain()
20+
{
21+
if (File.Exists(KeyPath))
22+
{
23+
var keyInText = File.ReadAllText(KeyPath);
24+
var keyPairSaved = JsonSerializer.Deserialize<KeyPairSaved>(keyInText);
25+
return LoadEccKeyPair(keyPairSaved);
26+
}
1727

18-
var privateKey = (ECPrivateKeyParameters) keyGen.Private;
19-
var publicKey = (ECPublicKeyParameters) keyGen.Public;
28+
return GenerateKeyPairWithSessionKey();
29+
}
2030

21-
var generateKeyPair = new KeyPair()
31+
private static KeyPairDomain GenerateKeyPairWithSessionKey()
32+
{
33+
var keyGen = new ECKeyPairGenerator();
34+
var keyGenParam = new ECKeyGenerationParameters(Domain, new SecureRandom());
35+
keyGen.Init(keyGenParam);
36+
var keyPair = keyGen.GenerateKeyPair();
37+
38+
var keyPairDomain = new KeyPairDomain
2239
{
23-
PublicKey = publicKey.Q,
24-
PrivateKey = privateKey.D,
25-
BasePoint = publicKey.Parameters.G,
26-
KeySize = keySize
40+
PrivateKey = (ECPrivateKeyParameters)keyPair.Private,
41+
PublicKey = (ECPublicKeyParameters)keyPair.Public,
42+
SessionKey = new BigInteger(256, new SecureRandom())
2743
};
2844

29-
return generateKeyPair;
45+
keyPairDomain.SaveAsFile(KeyPath);
46+
return keyPairDomain;
47+
}
48+
49+
50+
private static KeyPairDomain LoadEccKeyPair(KeyPairSaved keyPairSaved)
51+
{
52+
var privateKeyD = new BigInteger(keyPairSaved.StrPrivateKey, 16);
53+
var privateKey = new ECPrivateKeyParameters(privateKeyD, Domain);
54+
55+
var q = Domain.G.Multiply(privateKey.D);
56+
var publicKey = new ECPublicKeyParameters(q, Domain);
57+
58+
return new KeyPairDomain
59+
{
60+
PrivateKey = privateKey,
61+
PublicKey = publicKey,
62+
SessionKey = new BigInteger(keyPairSaved.StrSessionKey, 16)
63+
};
3064
}
31-
3265
}

EccSDK/KeyStorageHelper.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Org.BouncyCastle.Math.EC;
2+
3+
namespace EccSDK.models.ChameleonHash;
4+
5+
public class ChameleonHash
6+
{
7+
public ECPoint Value { get; set; }
8+
}

EccSDK/models/ChameleonHashRequest.cs renamed to EccSDK/Models/ChameleonHash/ChameleonHashRequest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using EccSDK.models.Keys;
12
using Org.BouncyCastle.Math;
23

3-
namespace EccSDK.models;
4+
namespace EccSDK.Models.ChameleonHash;
45

56
public class ChameleonHashRequest
67
{
7-
public KeyPair KeyPair { get; set; }
8+
public KeyPairDomain KeyPairDomain { get; set; }
89
public string Message { get; set; }
910
public BigInteger SessionKey { get; set; }
1011
public BigInteger Order { get; set; }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Org.BouncyCastle.Math;
2+
3+
namespace EccSDK.models.ChameleonHash;
4+
5+
public class ChameleonSignature
6+
{
7+
public BigInteger Value { get; set; }
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text.Json;
2+
using EccSDK.Models.Keys;
3+
using Org.BouncyCastle.Crypto.Parameters;
4+
using Org.BouncyCastle.Math;
5+
6+
namespace EccSDK.models.Keys;
7+
8+
public class KeyPairDomain
9+
{
10+
public ECPrivateKeyParameters PrivateKey { get; set; }
11+
public ECPublicKeyParameters PublicKey { get; set; }
12+
public BigInteger SessionKey { get; set; }
13+
14+
public void SaveAsFile(string path)
15+
{
16+
File.WriteAllText(path, JsonSerializer.Serialize(new KeyPairSaved
17+
{
18+
StrPrivateKey = PrivateKey.D.ToString(16),
19+
StrSessionKey = SessionKey.ToString(16)
20+
}));
21+
}
22+
}

EccSDK/Models/Keys/KeyPairSaved.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace EccSDK.Models.Keys;
2+
3+
public class KeyPairSaved
4+
{
5+
public string StrPrivateKey { get; set; }
6+
public string StrSessionKey { get; set; }
7+
}

EccSDK/SessionKeyGenerator.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

EccSDK/models/KeyPair.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)