Skip to content

Commit

Permalink
Merge pull request #3976 from Songhee120/pk-exception
Browse files Browse the repository at this point in the history
Raise exception on libplanet for zero byte prive key
  • Loading branch information
OnedgeLee authored Oct 27, 2024
2 parents 2b8189d + 2c8ca49 commit 625e617
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Libplanet.Crypto/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ private static ECPrivateKeyParameters GenerateKeyParam()

private static ECPrivateKeyParameters GenerateKeyFromBytes(byte[] privateKey)
{
if (privateKey.All(b => b.Equals(0x00)))
{
throw new ArgumentException("Every bytes in Private key is zero value.");
}

var param = new ECPrivateKeyParameters(
"ECDSA",
new BigInteger(1, privateKey),
Expand Down
15 changes: 14 additions & 1 deletion test/Libplanet.Tests/Crypto/PrivateKeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,18 @@ public void PrivateKeyGenerateLongerThan31Bytes()

Assert.Empty(faults);
}

[Theory]
[InlineData(new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
})]
public void ThrowsWhenZeroBytesPrivateKey(byte[] bytes)
{
Assert.Throws<ArgumentException>(() => new PrivateKey(bytes));
}
}
}
}

0 comments on commit 625e617

Please sign in to comment.