Skip to content

Commit c225364

Browse files
Add NFKD string normalization.
1 parent a7fb5f8 commit c225364

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Src/FinderOuter/Services/InputService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,12 @@ public bool CanBePrivateKey(string key)
2828
(key[0] == Constants.UncompPrivKeyChar));
2929
}
3030

31+
32+
public bool NormalizeNFKD(string s, out string norm)
33+
{
34+
norm = s.Normalize(NormalizationForm.FormKD);
35+
return !s.IsNormalized(NormalizationForm.FormKD);
36+
}
37+
3138
}
3239
}

Src/FinderOuter/Services/MessageSignatureService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public MessageSignatureService(Report rep) : base(rep)
2121
{
2222
calc = new EllipticCurveCalculator(new SecP256k1());
2323
addressBuilder = new Address();
24+
inputService = new InputService(rep);
2425
}
2526

2627

@@ -29,6 +30,7 @@ public MessageSignatureService(Report rep) : base(rep)
2930
private readonly Sha256 hash = new Sha256(true);
3031
private readonly EllipticCurveCalculator calc;
3132
private readonly Address addressBuilder;
33+
private InputService inputService;
3234

3335

3436
private bool CheckAddress(string addr, out Address.AddressType addrType)
@@ -46,7 +48,7 @@ private bool CheckMessage(string message, out byte[] toSign)
4648
byte[] toHash = ByteArray.ConcatArrays(
4749
new byte[] { (byte)MessageSignConstant.Length },
4850
Encoding.UTF8.GetBytes(MessageSignConstant),
49-
new CompactInt((ulong)message.Length).ToByteArray(),
51+
new CompactInt((ulong)msgBa.Length).ToByteArray(),
5052
msgBa);
5153

5254
toSign = hash.ComputeHash(toHash);
@@ -134,6 +136,11 @@ public bool Validate(string message, string address, string signature)
134136
{
135137
InitReport();
136138

139+
if(inputService.NormalizeNFKD(message, out string norm))
140+
{
141+
message = norm;
142+
AddMessage("Input message was normalized using Unicode Normalization Form Compatibility Decomposition.");
143+
}
137144
if (!CheckMessage(message, out byte[] toSign))
138145
return Fail("Invalid message UTF8 format.");
139146
if (!CheckAddress(address, out Address.AddressType addrType))
@@ -301,6 +308,11 @@ public async Task<bool> TryFindProblem(string message, string address, string si
301308
{
302309
InitReport();
303310

311+
if (inputService.NormalizeNFKD(message, out string norm))
312+
{
313+
message = norm;
314+
AddMessage("Input message was normalized using Unicode Normalization Form Compatibility Decomposition.");
315+
}
304316
if (!CheckMessage(message, out _) ||
305317
!CheckAddress(address, out Address.AddressType addrType) ||
306318
!CheckSignature(signature, out byte[] sigBa))

0 commit comments

Comments
 (0)