<p><a href="https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/interpreter.h#L21" rel="nofollow">This struct appears to describe the SIGHASH flags:</a></p>

<pre><code>/** Signature hash types/flags */
enum
{
    SIGHASH_ALL = 1,
    SIGHASH_NONE = 2,
    SIGHASH_SINGLE = 3,
    SIGHASH_ANYONECANPAY = 0x80,
};
</code></pre>

<p>and it's encoded as the last byte. Note that SIGHASH_ANYONECANPAY can appear in conjunction with any of the three previous flags.</p>

CC BY-SA 3.0
2015-04-27T22:24:19.223Z
2015-04-29T01:13:32.830Z
2015-04-29T01:13:32.830Z
Nick ODell
<p>The SIGHASH type is serialized as a single byte and then simply appended to the DER-encoded signature.</p>

<p>Example of a typical P2PKH scriptSig:</p>

<pre><code>304402206e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd022037dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b62001 03ff1104b46b2141df1948dd0df2223720a3a471ec57404cace47063843a699a0f
</code></pre>

<p>The second element above is the public key, serialized as per <a href="http://www.secg.org/sec1-v2.pdf#page=16" rel="noreferrer">SEC 1's Elliptic-Curve-Point-to-Octet-String Conversion</a>. The first element is the DER encoded signature, with a 0x01 appended to specify it's a SIGHASH_ALL (whose values you've already found). To break the signature string down further, if you're interested:</p>

<pre><code>30 &lt;- a sequence
44 &lt;- of length 0x44
  02 &lt;- an integer
  20 &lt;- of length 0x20
    6e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd &lt;- r
  02 &lt;- an integer
  20 &lt;- of length 0x20
    37dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b620 &lt;- s
01 &lt;- SIGHASH type (not part of the DER encoding)
</code></pre>

<p>The SIGHASH type is also temporarily appended to the transaction before it is signed so that the signature precludes its modification, see <a href="https://bitcoin.stackexchange.com/questions/37093/what-goes-in-to-the-message-of-a-transaction-signature/37095#37095">this answer</a> for more details.</p>

CC BY-SA 3.0
2018-01-06T14:57:21.903Z
How does it work for ANYONE_CAN_PAY? Is it `0x81`, similarly, for the others is it `0x81, 0x83`?
Jus12
CC BY-SA 3.0
2018-01-11T01:03:50.950Z
@Jus12 Yes, SIGHASH_ANYONECANPAY is bitwise ANDed (same as added in this case) to one of the other three SIGHASH types.
Christopher Gurnee
CC BY-SA 3.0
2015-04-28T13:02:07.523Z
2015-04-28T13:02:07.523Z
2017-04-13T12:47:33.283Z
Christopher Gurnee
<p><a href="https://bitcoin.org/en/developer-guide#signature-hash-types" rel="noreferrer">This page describes SIGHASH flags:</a></p>

<blockquote>
  <p>OP_CHECKSIG extracts a non-stack argument from each signature it evaluates, allowing the signer to decide which parts of the transaction to sign. Since the signature protects those parts of the transaction from modification, this lets signers selectively choose to let other people modify their transactions.</p>
</blockquote>

<p>But how can I extract the SIGHASH flags from the signature?</p>

<signature><sighash>
How are SIGHASH flags encoded into a signature?
Nick ODell
# How are SIGHASH flags encoded into a signature?

(Asked by: username_0 on 2015-04-27)

[This page describes SIGHASH flags:](https://bitcoin.org/en/developer-guide#signature-hash-types)

> OP\_CHECKSIG extracts a non-stack argument from each signature it evaluates, allowing the signer to decide which parts of the transaction to sign. Since the signature protects those parts of the transaction from modification, this lets signers selectively choose to let other people modify their transactions.

But how can I extract the SIGHASH flags from the signature?

**Tags:** signature, sighash


---

## 2 answers

### Answer by username_1 on 2015-04-28. Score: 13 | Accepted answer

The SIGHASH type is serialized as a single byte and then simply appended to the DER-encoded signature.

Example of a typical P2PKH scriptSig:

```
304402206e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd022037dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b62001 03ff1104b46b2141df1948dd0df2223720a3a471ec57404cace47063843a699a0f

```

The second element above is the public key, serialized as per [SEC 1's Elliptic-Curve-Point-to-Octet-String Conversion](http://www.secg.org/sec1-v2.pdf#page=16). The first element is the DER encoded signature, with a 0x01 appended to specify it's a SIGHASH\_ALL (whose values you've already found). To break the signature string down further, if you're interested:

```
30 <- a sequence
44 <- of length 0x44
  02 <- an integer
  20 <- of length 0x20
    6e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd <- r
  02 <- an integer
  20 <- of length 0x20
    37dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b620 <- s
01 <- SIGHASH type (not part of the DER encoding)

```

The SIGHASH type is also temporarily appended to the transaction before it is signed so that the signature precludes its modification, see [this answer](https://bitcoin.stackexchange.com/questions/37093/what-goes-in-to-the-message-of-a-transaction-signature/37095#37095) for more details.

> **Comments:**
> **username_2**: How does it work for ANYONE_CAN_PAY? Is it `0x81`, similarly, for the others is it `0x81, 0x83`?  
> **username_1**: @username_2 Yes, SIGHASH_ANYONECANPAY is bitwise ANDed (same as added in this case) to one of the other three SIGHASH types.  

---

### Answer by username_0 on 2015-04-27. Score: 4

[This struct appears to describe the SIGHASH flags:](https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/interpreter.h#L21)

```
/** Signature hash types/flags */
enum
{
    SIGHASH_ALL = 1,
    SIGHASH_NONE = 2,
    SIGHASH_SINGLE = 3,
    SIGHASH_ANYONECANPAY = 0x80,
};

```

and it's encoded as the last byte. Note that SIGHASH\_ANYONECANPAY can appear in conjunction with any of the three previous flags.


---

