How are SIGHASH flags encoded into a signature? This page describes SIGHASH flags:


  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?
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. 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 &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)


The SIGHASH type is also temporarily appended to the transaction before it is signed so that the signature precludes its modification, see this answer for more details.