<p>I suppose you use the BitCoinJ library.</p>

<p>You are not creating a signature the correct way; an <code>ECDSASignature</code> object should be initialized with the ECDSA signature parameters (r,s), not the actual private and public keys. The constructor you are using, implies that the ECDSA signature has been computed already, you somehow got <code>r</code> and <code>s</code> and you initiate the signature object.</p>

<p>To achieve your required functionality, use the <code>doSign(Sha256Hash input, BigInteger privateKeyForSigning)</code> method of your <code>ECKey</code> and the output will be your desired <code>ECDSASignature</code> object. </p>

<p>The input on <code>doSign</code> should be the same with the input on <code>verify</code>, which is always the SHA256 of your message to be signed. If you want to spend an P2PKH output you must sign the public key, check this <a href="https://bitcoin.stackexchange.com/questions/22589/in-scriptsig-which-form-of-the-public-key-is-considered-canonical?rq=1">post</a> for more information.</p>

CC BY-SA 3.0
2015-12-30T07:58:48.023Z
I actually created my ECDSASignature object by using the sign(Sha256 input). My input was : Sha256Hash input=new Sha256Hash(rwallet.getClientKey().getPrivKeyBytes());   And then in verification function in both ways first with:rKey      .verify(sha256hash, redeemerSignature))  and second with : ECKey
 .verify(sha256hash.getBytes(),
 redeemerSignature,
 rKey.getPubKey())) works correct.
gtopal
CC BY-SA 3.0
2016-01-09T20:39:42.360Z
Eventually, @Konstantinos you are right about the thing that I have to sign a message and not the getPrivKeyBytes() result,as it will reveal information about my private key. I think the best thing is to sign a Transaction object,but for my program's functionality requirements...i will sign a message( which in fact will be a String).
gtopal
CC BY-SA 3.0
2015-12-29T13:01:05.333Z
2015-12-29T14:21:06.193Z
2017-04-13T12:47:33.743Z
Kostas Kryptos
<p>According to this function:</p>

<pre><code>public boolean verify(Sha256Hash sigHash, ECDSASignature signature) {
        return ECKey.verify(sigHash.getBytes(), signature, getPubKey());
    }
</code></pre>

<p>I do not understand which must be the content of the variable <strong>sighash</strong>.</p>

<p>Here is my function where the creation of signature happend:</p>

<pre><code>public ECDSASignature scriptSig(Receiver r) {
        System.out.println("First step :\n");
        System.out
                .println("CREATION OF SCRIPTSIG-----OR----- OUTPUT TRANSACTION");

        r = super.getReceiver();

        Wallet rwallet = r.getReceiverWalletList().get(0);

        BigInteger R = rwallet.getClientKey().getPrivKey();
        System.out
                .println("*******************************************************");
        System.out.println(R);

        BigInteger S = new BigInteger(r.getReceiverWalletList().get(0).getClientKey().getPubKey()); // i
                                                                                // had
                                                                                // .getPubkeyHash()
        System.out
                .println("*******************************************************");
        System.out.println(S);


        // constructs a signature with the given components
        ECDSASignature receiverSig = new ECDSASignature(R, S);

        return receiverSig;
    }
</code></pre>

<p>and here i am trying to verify my signature:</p>

<pre><code>sha256hash = new Sha256Hash(receiver.getReceiverWalletList()
                        .get(0).getClientKey().getPrivKeyBytes());
            redeemerSignature = scriptSig(receiver);


.....


if (receiver.getReceiverWalletList().get(0).getClientKey()
                        .verify(sha256hash, redeemerSignature))
                    System.out.println("The transaction finished SUCCESSFULLY");

                else
                    System.out
                            .println("The transaction finished UNSUCCESSFULLY");
</code></pre>

<p>I think i my problem is the <strong>right input in sha256hash = new Sha256Hash(...)</strong> instance.</p>

<p>Any help?</p>

<bitcoinj><ecdsa>
Verifing the given R/S pair (signature) against a hash using the public key
gtopal
# Verifing the given R/S pair (signature) against a hash using the public key

(Asked by: username_0 on 2015-12-29)

According to this function:

```
public boolean verify(Sha256Hash sigHash, ECDSASignature signature) {
        return ECKey.verify(sigHash.getBytes(), signature, getPubKey());
    }

```

I do not understand which must be the content of the variable **sighash**.

Here is my function where the creation of signature happend:

```
public ECDSASignature scriptSig(Receiver r) {
        System.out.println("First step :\n");
        System.out
                .println("CREATION OF SCRIPTSIG-----OR----- OUTPUT TRANSACTION");

        r = super.getReceiver();

        Wallet rwallet = r.getReceiverWalletList().get(0);

        BigInteger R = rwallet.getClientKey().getPrivKey();
        System.out
                .println("*******************************************************");
        System.out.println(R);

        BigInteger S = new BigInteger(r.getReceiverWalletList().get(0).getClientKey().getPubKey()); // i
                                                                                // had
                                                                                // .getPubkeyHash()
        System.out
                .println("*******************************************************");
        System.out.println(S);

        // constructs a signature with the given components
        ECDSASignature receiverSig = new ECDSASignature(R, S);

        return receiverSig;
    }

```

and here i am trying to verify my signature:

```
sha256hash = new Sha256Hash(receiver.getReceiverWalletList()
                        .get(0).getClientKey().getPrivKeyBytes());
            redeemerSignature = scriptSig(receiver);

.....

if (receiver.getReceiverWalletList().get(0).getClientKey()
                        .verify(sha256hash, redeemerSignature))
                    System.out.println("The transaction finished SUCCESSFULLY");

                else
                    System.out
                            .println("The transaction finished UNSUCCESSFULLY");

```

I think i my problem is the **right input in sha256hash = new Sha256Hash(...)** instance.

Any help?

**Tags:** bitcoinj, ecdsa


---

## 1 answers

### Answer by username_1 on 2015-12-29. Score: 3 | Accepted answer

I suppose you use the BitCoinJ library.

You are not creating a signature the correct way; an `ECDSASignature` object should be initialized with the ECDSA signature parameters (r,s), not the actual private and public keys. The constructor you are using, implies that the ECDSA signature has been computed already, you somehow got `r` and `s` and you initiate the signature object.

To achieve your required functionality, use the `doSign(Sha256Hash input, BigInteger privateKeyForSigning)` method of your `ECKey` and the output will be your desired `ECDSASignature` object.

The input on `doSign` should be the same with the input on `verify`, which is always the SHA256 of your message to be signed. If you want to spend an P2PKH output you must sign the public key, check this [post](https://bitcoin.stackexchange.com/questions/22589/in-scriptsig-which-form-of-the-public-key-is-considered-canonical?rq=1) for more information.

> **Comments:**
> **username_0**: I actually created my ECDSASignature object by using the sign(Sha256 input). My input was : Sha256Hash input=new Sha256Hash(rwallet.getClientKey().getPrivKeyBytes());   And then in verification function in both ways first with:rKey      .verify(sha256hash, redeemerSignature))  and second with : ECKey  .verify(sha256hash.getBytes(),  redeemerSignature,  rKey.getPubKey())) works correct.  
> **username_0**: Eventually, @Konstantinos you are right about the thing that I have to sign a message and not the getPrivKeyBytes() result,as it will reveal information about my private key. I think the best thing is to sign a Transaction object,but for my program's functionality requirements...i will sign a message( which in fact will be a String).  

---

