2121import org .fisco .bcos .sdk .utils .Numeric ;
2222
2323public class ECDSASignature implements Signature {
24+ private static int INPUT_MESSAGE_SIZE_IN_HEX = 64 ;
25+
2426 @ Override
2527 public SignatureResult sign (final String message , final CryptoKeyPair keyPair ) {
2628 // convert signature string to SignatureResult struct
@@ -32,11 +34,19 @@ public SignatureResult sign(final byte[] message, final CryptoKeyPair keyPair) {
3234 return sign (Hex .toHexString (message ), keyPair );
3335 }
3436
37+ private void checkInputMessage (final String message ) {
38+ if (message .length () != INPUT_MESSAGE_SIZE_IN_HEX ) {
39+ throw new SignatureException (
40+ "Invalid input message " + message + ", must be a hex string of length 64" );
41+ }
42+ }
43+
3544 @ Override
3645 public String signWithStringSignature (final String message , final CryptoKeyPair keyPair ) {
46+ String inputMessage = Numeric .cleanHexPrefix (message );
47+ checkInputMessage (inputMessage );
3748 CryptoResult signatureResult =
38- NativeInterface .secp256k1Sign (
39- keyPair .getHexPrivateKey (), Numeric .cleanHexPrefix (message ));
49+ NativeInterface .secp256k1Sign (keyPair .getHexPrivateKey (), inputMessage );
4050 // call secp256k1Sign failed
4151 if (signatureResult .wedprErrorMessage != null
4252 && !signatureResult .wedprErrorMessage .isEmpty ()) {
@@ -49,14 +59,15 @@ public String signWithStringSignature(final String message, final CryptoKeyPair
4959
5060 @ Override
5161 public boolean verify (final String publicKey , final String message , final String signature ) {
62+ String inputMessage = Numeric .cleanHexPrefix (message );
63+ checkInputMessage (inputMessage );
5264 String hexPubKeyWithPrefix =
5365 Numeric .getHexKeyWithPrefix (
5466 publicKey ,
5567 CryptoKeyPair .UNCOMPRESSED_PUBLICKEY_FLAG_STR ,
5668 CryptoKeyPair .PUBLIC_KEY_LENGTH_IN_HEX );
5769 CryptoResult verifyResult =
58- NativeInterface .secp256k1verify (
59- hexPubKeyWithPrefix , Numeric .cleanHexPrefix (message ), signature );
70+ NativeInterface .secp256k1verify (hexPubKeyWithPrefix , inputMessage , signature );
6071 // call secp256k1verify failed
6172 if (verifyResult .wedprErrorMessage != null && !verifyResult .wedprErrorMessage .isEmpty ()) {
6273 throw new SignatureException (
0 commit comments