<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: thunderbiscuit</title>
    <description>The latest articles on DEV Community by thunderbiscuit (@thunderbiscuit).</description>
    <link>https://dev.to/thunderbiscuit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F593807%2Fa0d73389-43d1-45f6-b3c1-60dec7f09637.jpeg</url>
      <title>DEV Community: thunderbiscuit</title>
      <link>https://dev.to/thunderbiscuit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thunderbiscuit"/>
    <language>en</language>
    <item>
      <title>PGP for the Working Dev (2)</title>
      <dc:creator>thunderbiscuit</dc:creator>
      <pubDate>Thu, 10 Jun 2021 02:29:11 +0000</pubDate>
      <link>https://dev.to/thunderbiscuit/pgp-for-the-working-dev-2-2ol1</link>
      <guid>https://dev.to/thunderbiscuit/pgp-for-the-working-dev-2-2ol1</guid>
      <description>&lt;p&gt;The goal of this post is to get you up to speed on two of the main uses of GPG: cryptographically encrypting and decrypting data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: this post is the second in a 2-part series aimed at developing a functional use of GPG for the day-to-day tasks of anyone who cares about software. Check out &lt;a href="https://dev.to/thunderbiscuit/pgp-for-the-working-dev-1-4f4g"&gt;Part 1&lt;/a&gt; to learn about signing and verifying signatures.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One important aspect of cryptographic keys is that they are &lt;em&gt;simply numbers&lt;/em&gt;. They are often really, really big numbers, but simple integers nonetheless. Another important aspect of cryptographic keys as used in GPG is that they come in pairs: a &lt;em&gt;public&lt;/em&gt; key and a &lt;em&gt;private&lt;/em&gt; key. Knowledge of these numbers (the keys) allows us to perform many tasks, two of which we'll tackle here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Encrypt a message in a way that only a person with the knowledge of a specific key could decrypt.&lt;/li&gt;
&lt;li&gt;Decrypt a message that was encrypted for a private key you own.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;There is much more to learn about privacy, encryption, and GPG than I cover in this series; for information on who invented this whole thing in the first place, check out &lt;a href="https://en.wikipedia.org/wiki/Phil_Zimmermann"&gt;Phil Zimmermann's Wikipedia page&lt;/a&gt;; for more on the difference between GPG and PGP, check out &lt;a href="https://www.goanywhere.com/blog/2013/07/18/openpgp-pgp-gpg-difference"&gt;this article&lt;/a&gt;; to dig deeper into the specifics of GPG, check out &lt;a href="https://gnupg.org/"&gt;their docs&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Priors
&lt;/h2&gt;

&lt;p&gt;If you are coming to this article without having read &lt;a href=""&gt;Part 1&lt;/a&gt;, you should go and read the &lt;em&gt;Priors&lt;/em&gt; section there. If you have already set up &lt;code&gt;gpg&lt;/code&gt;, created a key, exported it, and imported other people's keys, you can safely move on to &lt;a href=""&gt;Task 3&lt;/a&gt; on encrypting messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Task 3: Encrypting Messages
&lt;/h2&gt;

&lt;p&gt;You can encrypt a message addressed to a specific recipient using their public key if it is in your keyring. For information on how to export your public key in order to share it with others as well as information about how to import other people's public keys, check out &lt;a href="https://dev.to/thunderbiscuit/pgp-for-the-working-dev-2-2ol1"&gt;Task 1 from Part 1&lt;/a&gt; of this series.&lt;/p&gt;

&lt;p&gt;To encrypt the file &lt;code&gt;message.txt&lt;/code&gt; in a way that only Morpheus will be able to decrypt it, we first check that his key in our keyring, then use the &lt;code&gt;--encrypt&lt;/code&gt; command like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# check for the recipient's info in your keyring&lt;/span&gt;
&lt;span class="c"&gt;# the second key here is our friend Morpheus' key we just imported&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--list-keys&lt;/span&gt;
pub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;SC]
      0E94A26389C660DFE8C2F31705D085D9893479A7
uid           &lt;span class="o"&gt;[&lt;/span&gt;ultimate] MrAnderson &amp;lt;MrAnderson@email.com&amp;gt;
sub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;E]

pub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;SC]
      5F357B756A571BF8A88567AF06898DFB48E3D899
uid           &lt;span class="o"&gt;[&lt;/span&gt; full &lt;span class="o"&gt;]&lt;/span&gt; Morpheus &amp;lt;morpheus@email.com&amp;gt;
sub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;E]

&lt;span class="c"&gt;# produce the encrypted file message.txt.asc&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--encrypt&lt;/span&gt; &lt;span class="nt"&gt;--armor&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; Morpheus message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always make sure that the public key you use to encrypt is genuine! Call the person, verify on multiple channels, or message them on another encypted platform first before using their public key to encrypt a message. Note that you can enrypt a message to multiple public keys, but that will show up when people decrypt it (in other words you could not fake that a message was encrypted &lt;em&gt;only&lt;/em&gt; for someone when in fact it was also encrypted for someone else at the same time).&lt;/p&gt;

&lt;h2&gt;
  
  
  Task 4: Decrypting Messages
&lt;/h2&gt;

&lt;p&gt;Upon getting the encrypted file, our friend Morpheus could decrypt its content using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# print message to console&lt;/span&gt;
gpg &lt;span class="nt"&gt;--decrypt&lt;/span&gt; message.txt.asc

&lt;span class="c"&gt;# send message to file&lt;/span&gt;
gpg &lt;span class="nt"&gt;--decrypt&lt;/span&gt; message.txt.asc &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; decryptedMessage.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. There is much more to this little piece of software than explored in these two articles, so go ahead and type &lt;code&gt;gpg --help&lt;/code&gt;!&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>pgp</category>
      <category>commandline</category>
    </item>
    <item>
      <title>PGP for the Working Dev (1)</title>
      <dc:creator>thunderbiscuit</dc:creator>
      <pubDate>Thu, 10 Jun 2021 02:29:05 +0000</pubDate>
      <link>https://dev.to/thunderbiscuit/pgp-for-the-working-dev-1-4f4g</link>
      <guid>https://dev.to/thunderbiscuit/pgp-for-the-working-dev-1-4f4g</guid>
      <description>&lt;p&gt;Anyone who cares about software should learn how to produce cryptographic signatures on data (documents, messages, binaries, git commits) and how to verify other people's signatures on those types of data. It turns out it's rather easy, and all you need is a terminal and maybe half an hour of time!&lt;/p&gt;

&lt;p&gt;The goal of this article is to get you up to speed on two of the main uses of the GPG cli software: cryptographically signing and verifying signatures on data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: this post is the first of a 2-part series aimed at developing a functional use of GPG for the day-to-day tasks of anyone who cares about software. Check out &lt;a href="https://dev.to/thunderbiscuit/pgp-for-the-working-dev-2-2ol1"&gt;Part 2&lt;/a&gt; for the lowdown on how to encrypt and decrypt messages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;GPG stands for &lt;em&gt;GNU Privacy Guard&lt;/em&gt; and is a small open source piece of software that allows you to manage keys for cryptographic purposes. GPG implements the OpenPGP standard maintained by the Internet Engineering Task Force. In practice, the terms GPG, OpenPGP, and PGP are often used interchangably even though they probably shouldn't be. You can think of OpenPGP as email and GPG as one implementation of the email protocol (like Hotmail or Gmail). PGP is the protocol, and the command line tool we use to perform tasks using this protocol is called &lt;code&gt;gpg&lt;/code&gt;. Confusing I know.&lt;/p&gt;

&lt;p&gt;One important aspect of cryptographic keys is that they are &lt;em&gt;simply numbers&lt;/em&gt;. They are often really, really big numbers, but simple integers nonetheless. Another important aspect of cryptographic keys as used in GPG is that they come in pairs: a &lt;em&gt;public&lt;/em&gt; key and a &lt;em&gt;private&lt;/em&gt; key. Knowledge of these numbers (the keys) allows us to perform many tasks, two of which we'll tackle here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Signing a piece of data in a way that only a person knowing a specific key could have done.&lt;/li&gt;
&lt;li&gt;Verify the validity of a signature from someone else on a specific piece of data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;GPG does much more, but this post is really about those two basic tasks and their related options.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There is much more to learn about privacy, encryption, and GPG than I cover in this series; for information on who invented this whole thing in the first place, check out &lt;a href="https://en.wikipedia.org/wiki/Phil_Zimmermann"&gt;Phil Zimmermann's Wikipedia page&lt;/a&gt;; for more on the difference between GPG and PGP, check out &lt;a href="https://www.goanywhere.com/blog/2013/07/18/openpgp-pgp-gpg-difference"&gt;this article&lt;/a&gt;; to dig deeper into the specifics of GPG, check out &lt;a href="https://gnupg.org/"&gt;their docs&lt;/a&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Priors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installing the Software
&lt;/h3&gt;

&lt;p&gt;First, make sure you have GPG installed on your computer. You can get it on MacOS using homebrew (&lt;code&gt;brew install gpg&lt;/code&gt;), on Linux using any of the package managers, or on Windows by downloading the binaries directly from the &lt;a href="https://gnupg.org/download/index.html"&gt;GnuPG official website&lt;/a&gt;. You will also need a bash terminal to interact with the software.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# if this returns a version number, you're good!&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating Keys
&lt;/h3&gt;

&lt;p&gt;The first thing we want to do is create a key set (a public and a private key pair). I recommend creating an set of test keys with a simple password to use for the rest of this tutorial, and deleting it afterwards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--full-generate-key&lt;/span&gt;  &lt;span class="c"&gt;# generate a new key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The process of creating a new key pair requires that we answer some question about key type and size, as well as associate some information with the keys. If you answer the few questions required to build keys, you should soon find that you now have a key set available in your GPG keyring. To see what keys are available, enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--list-keys&lt;/span&gt;
pub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;SC]
      0E94A26389C61705D08560DFE8C2F3D9893479A7
uid           &lt;span class="o"&gt;[&lt;/span&gt;ultimate] MrAnderson &amp;lt;MrAnderson@email.com&amp;gt;
sub   rsa1024 2019-09-14 &lt;span class="o"&gt;[&lt;/span&gt;E]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have a key pair available to us for the tasks below. The &lt;code&gt;--list-key&lt;/code&gt; command outputs some metadata about the key, as well as a fingerprint—the &lt;code&gt;0E94A26389C61705D08560DFE8C2F3D9893479A7&lt;/code&gt; part. The fingerprint is a hash of the public key, and can therefore be used for identification.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exporting and Importing Keys
&lt;/h3&gt;

&lt;p&gt;To verify the validity of your signature on data or encrypt messages destined for you, other users will need to have your public key information. We can export our public key to a file using the following command, and then simply share the file with anyone we wish:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gpg --export --armor MrAnderson &amp;gt; MrAndersonPubKey.asc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have someone's public key file, you'll need to import it into your keyring before being able to use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gpg --import MorpheusPubKey.asc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ASCII-armor Format
&lt;/h3&gt;

&lt;p&gt;You can share your keys in binary format, or, often more conveniently, in a text format called ASCII-armor. You can print your public key to the console using the name or the email associated with the key as the last argument as in &lt;code&gt;gpg --export --armor MrAnderson&lt;/code&gt;, or generate a file with your key in it using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# generate public key file in binary format&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--export&lt;/span&gt; MrAnderson &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; myBinaryPubKey.gpg

&lt;span class="c"&gt;# generate public key file in ASCII-armor format&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--export&lt;/span&gt; &lt;span class="nt"&gt;--armor&lt;/span&gt; MrAnderson &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; MrAndersonPubKey.asc

&lt;span class="c"&gt;# print content of key file in ASCII-armor format to console&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;MrAndersonPubKey.asc
&lt;span class="nt"&gt;-----BEGIN&lt;/span&gt; PGP PUBLIC KEY BLOCK-----

mI0EXX0oXQEEAK/NyXBQxd1s9s3SYSVXMXfW0a3XR6JGwzf3ocfCu8OP12hVMvfo
BQfdj2WllNsWWpzNRJdeK2QCtLudykPNKtY7BuAk5bIWbGuVsNkWU/UmJZqijE0Q
aA+g2K1DFST5h4N12vLyLnN0On7RaJsTizR083E2QATsU/3VjP3Y3LUnABEBAAG0
IU1yQW5kZXJzb24gPE1yQW5kZXJzb25AZW1haWwuY29tPojOBBMBCAA4FiEEDpSi
Y4nGYN/owvMXBdCF2Yk0eacFAl19KF0CGwMFCwkIBwIGFQoJCAsCBBYCAwECHgEC
F4AACgkQBdCF2Yk0eadEqgQAm/LeK4BK77OPhy2jiouhmyhM922sATp07uP0s+WY
lJZZOlLSELc7FMn+iSzgsoBic432UFZVfJ5FZukn9oSSrvJagw7nx4Y3rIyUNDc+
0Dcw2lKH8WDqXXoANgWSewPb6P+kBq+ihVanaWgsG9a1nvb4/BoubMMoccAj3EaH
0Yq4jQRdfShdAQQArXj8/Ch3y//xeSPDXXu/HlylYT56s9gTOs8PzW/yeR8XAzGG
+YThFvMsRdfr8VYwr8fsNT+fS12IadaiwOX6ejs63LnE9WnyfnYdVTQO5ZTh61eU
zB1q97/YevWXIwTYAP/0h+tASqirffamdLZHctx54iozfb/M3QuSU2c4i5fAEQEA
AYi2BBgBCAAgFiEEDpSiY4nGYN/owvMXBdCF2Yk0eacFAl19KF0CGwwACgkQBdCF
2Yk0eacnRgP7BKkW5dMJn8aHKxSGmhcpeGnfI1Rp4MorfjcwCz/gqgtoiqOY99SK
maij8J92E5sKpdMpZXhQfjZjVDb91oAW0TdUEXbVVBSjZ4Ku33QwFCm+Qzud/xaT
lQDrnYx05SITsBAAmOrwfBRo464WfU/0rlXziE2E4wDY4U6IYtbrAcQ&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="o"&gt;=&lt;/span&gt;aNWV
&lt;span class="nt"&gt;-----END&lt;/span&gt; PGP PUBLIC KEY BLOCK-----
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the binary format and the ASCII-armor format are representations of the same data, but you'll find that the ASCII-armor is much easier to share (for example you could copy/paste it in an email or a text message).&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Task 1: Sign a document (without encrypting it)
&lt;/h2&gt;

&lt;p&gt;Signing a document (or any piece of data) creates proof that the document was signed by the owner of a specific public key, and that the document was not tampered with after the signature. Note that the signature is completely agnostic to the data it is signing, meaning that we can use GPG to sign sound data, text data, image data, or any other type of data we want.&lt;/p&gt;

&lt;p&gt;The goal here is really to sign an &lt;em&gt;open&lt;/em&gt; document. There is no encryption of the document in this procedure. As such, the document is usable by anyone whether or not they can verify its signature.&lt;/p&gt;

&lt;p&gt;One of the common objectives of signatures is to attest that a specific version of the data is being stored or sent across a network. Once a piece of data has been signed, changing a single bit in the data will render the signature invalid. This is why, for example, the lead maintainer of the bitcoin core software will sign the downloadable releases of the software. Once downloaded, you can test for yourself whether his or her signature is valid on the download you just performed. If the signature is valid, you are in posession of the exact same piece of code that he or she signed. If the signature is invalid, the download was tampered with on its way to you. You can use this as an assurance that the software you download is trustworthy without having to trust the network you download it on. Note that it does not prevent the signator from writing bugs or malware into the software at all—it only attests to the fact that the version in your possession is the same as the version that was signed, bit for bit.&lt;/p&gt;

&lt;p&gt;Let us suppose you have a file called &lt;code&gt;message.txt&lt;/code&gt; in your current directory with the content &lt;code&gt;hello, world&lt;/code&gt;, and that you wish to sign it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gpg &lt;span class="nt"&gt;--sign&lt;/span&gt; message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;outputs a new file in the same directory called &lt;code&gt;message.txt.gpg&lt;/code&gt; that contains the signature &lt;em&gt;and&lt;/em&gt; the original message. The message is not encrypted. Anyone can retrieve the message from the file, whether they verify the signature or not.&lt;/p&gt;

&lt;p&gt;Notice that the signed file is in &lt;code&gt;.gpg&lt;/code&gt; format, a binary format. This can make it inconvenient to look at or share (try &lt;code&gt;cat message.txt.gpg&lt;/code&gt; to see what I mean). Another very common way to save the file is in ASCII-armor format using the &lt;code&gt;--armor&lt;/code&gt; switch like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gpg &lt;span class="nt"&gt;--sign&lt;/span&gt; &lt;span class="nt"&gt;--armor&lt;/span&gt; message.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output file will be &lt;code&gt;message.txt.asc&lt;/code&gt;, and will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;----------BEGIN&lt;/span&gt; PGP MESSAGE-----

owGbwMvMwME4vbHMPzN7+SPGNbJJ3LmpxcWJ6al6JRUlsVVLizNSc3LydRTK84ty
UjqOsDAwcjDoiSmyuPmV/RI73XDE80h8BUw7KxNIg4BMSX5JYo5Dem5iZo5ecn4u
AxenAExJ2E7mf/orZmwoeTJ1vspC35XrYvfoisoilj0k3uqnmdkfHX2t0eW0NeQN
TNi39keSVeD2utSvOzfZaoXsLUlekmUt7DGhg2lCRZmm53sHM58PFitPqmXNqOsU
+cjRljPzQHXgcfUbjDOmr3nvkbApSlp3yo53XS/e/0vgEFzFq+Y1oUwx4OqrJ+/9
&lt;span class="nv"&gt;eQA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="o"&gt;=&lt;/span&gt;c82p
&lt;span class="nt"&gt;----------END&lt;/span&gt; PGP MESSAGE-----

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then copy and paste this anywhere to share or store the signed message. For example you could post it publicly on a website, send it as a text, or even write it down on a piece of paper.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Task 2: Verify the signature of a document
&lt;/h2&gt;

&lt;p&gt;Let us assume someone has sent us a signed document (&lt;code&gt;message_from_Morpheus.txt.asc&lt;/code&gt;) and we wish to verify its signature. We can use the &lt;code&gt;--verify&lt;/code&gt; option to do so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--verify&lt;/span&gt; message_from_Morpheus.txt.asc
gpg: Signature made Sat 14 Sep 18:55:38 2019 EDT
gpg:                using RSA key 5F35BF8A88567B756A5F06898DFB48E3D8717A99
gpg:                issuer &lt;span class="s2"&gt;"morpheus@email.com"&lt;/span&gt;
gpg: Can&lt;span class="s1"&gt;'t check signature: No public key
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that if you simply have the file but not the public key of the signator in your keyring, the &lt;code&gt;--verify&lt;/code&gt; command will state that the signature could not be verified as valid.&lt;/p&gt;

&lt;p&gt;To validate the signature, we'll first have to import the public key of the signator on our GPG keyring. In this case let's assume your friend Morpheus just sent you his public key in ASCII-armor format (he knows how to export his key because he read Task 1 of this post), and the file is saved under &lt;code&gt;MorpheusPubKey.asc&lt;/code&gt;. You can import his key in your keyring using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--import&lt;/span&gt; morpheusPubKey.asc
gpg: key 06898DFB48E3D899: public key &lt;span class="s2"&gt;"Morpheus &amp;lt;morpheus@email.com&amp;gt;"&lt;/span&gt; imported
gpg: Total number processed: 1
gpg:               imported: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you list your keys, you'll find you have 2 keys in your keyring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gpg --list-keys
pub   rsa1024 2019-09-14 [SC]
      0E94A26389C660DFE8C2F31705D085D9893479A7
uid           [ultimate] MrAnderson &amp;lt;MrAnderson@email.com&amp;gt;
sub   rsa1024 2019-09-14 [E]

pub   rsa1024 2019-09-14 [SC]
      5F357B756A571BF8A88567AF06898DFB48E3D899
uid           [ unknown] Morpheus &amp;lt;morpheus@email.com&amp;gt;
sub   rsa1024 2019-09-14 [E]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When first importing a key, it won't be &lt;em&gt;certified&lt;/em&gt; by default (GPG does not assume you &lt;em&gt;really&lt;/em&gt; know that the key is the key from the person who it says it belongs to). The key could have been tampered with on its way to you, and anyone can create keys with anyone else's name. GPG does not, in any way, verify that you are the person who's name you are using to create a key.&lt;/p&gt;

&lt;p&gt;It is recommended that you check that the key in your keyring belongs to the person you think it does using multiple sources, or that you take the time to call or text the person to verify. Once that is done, you can sign that key &lt;em&gt;yourself&lt;/em&gt; with your own private key, adding a layer of protection to the validity of your friend's public key in your key ring.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gpg &lt;span class="nt"&gt;--sign-key&lt;/span&gt; Morpheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will identify the key as &lt;code&gt;[full]&lt;/code&gt; instead of &lt;code&gt;[unknown]&lt;/code&gt; in our keyring. Notice that we do not need to sign a public key to use it in signature verification; it simply is an added layer of certainty when verifying signatures.&lt;/p&gt;

&lt;p&gt;Once we have the public key of the signator of the piece of data we wish to verify in our keyring, we can verify the signature on any file in one line of code. GPG will print the result of the signature verification on the console. In the case of a valid signature, it would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gpg &lt;span class="nt"&gt;--verify&lt;/span&gt; message_from_Morpheus.txt.asc
gpg: Signature made Sat 14 Sep 18:55:38 2019 EDT
gpg:                using RSA key 5F357AF0687B756A571BF8A885698DFB48E3D899
gpg:                issuer &lt;span class="s2"&gt;"morpheus@email.com"&lt;/span&gt;
gpg: Good signature from &lt;span class="s2"&gt;"Morpheus &amp;lt;morpheus@email.com&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;full]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see the content of the message, you'll want to either print it to the console or create a new file with the original message in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--decrypt&lt;/span&gt; message.txt.asc
what&lt;span class="s1"&gt;'s up dog?
gpg: Signature made Sat 14 Sep 18:55:38 2019 EDT
gpg:                using RSA key 5F357AF0687B756A571BF8A885698DFB48E3D899
gpg:                issuer "morpheus@email.com"
gpg: Good signature from "Morpheus &amp;lt;morpheus@email.com&amp;gt;" [full]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will print it to the console, as well as print the result of the signature verifcation. You could have used &lt;code&gt;--decrypt&lt;/code&gt; on a message without the public key in your keyring and GPG would still have printed the message, but it would have told you that it could not verify the signature the same way as it did above with the &lt;code&gt;--verify&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Anyone in posession of the file can also output the original data to a file using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--decrypt&lt;/span&gt; message_from_Morpheus.txt.asc &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Morpheus_message_only.txt
gpg: Signature made Sat 14 Sep 18:55:38 2019 EDT
gpg:                using RSA key 5F357AF0687B756A571BF8A885698DFB48E3D899
gpg:                issuer &lt;span class="s2"&gt;"morpheus@email.com"&lt;/span&gt;
gpg: Good signature from &lt;span class="s2"&gt;"Morpheus &amp;lt;morpheus@email.com&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;full]

&lt;span class="c"&gt;# the cat command prints to console the contents of a file&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;Morpheus_message_only.txt
what&lt;span class="s1"&gt;'s up dog?
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print the verification of the signature to the console as the file is created, but the signature verification will not be included in the newly created file; the new file is now an exact replica of the original file.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Extras
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Clearsign
&lt;/h3&gt;

&lt;p&gt;Note that the signature file build in ASCII-armor format in &lt;em&gt;Task 1&lt;/em&gt; does not contain a human readable version of the original data (in this case the string &lt;code&gt;hello, world&lt;/code&gt;). The file is not encrypted, but you'll need gpg installed and a command to show its content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# create a signature file named message.txt.asc&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--sign&lt;/span&gt; &lt;span class="nt"&gt;--armor&lt;/span&gt; message.txt
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;message.txt.asc

&lt;span class="nt"&gt;----------BEGIN&lt;/span&gt; PGP MESSAGE-----

owGbwMvMwME4vbHMPzN7+SPGNbJJ3LmpxcWJ6al6JRUlsVXfJmak5uTk6yiU5xfl
pHQcYWFg5GDQE1NkcfMr+yV2uuGI55H4Cph2ViaQBgGZkvySxByH9NzEzBy95Pxc
Bi5OAZiSV+3M/11+bLr0h3OFrsyTxPfv3irrfRX0m/U3aOXlwwd/KCju8Od59+5J
uGP84n0KOQbvjGbnvv31R7Nh1tz3b1r3aQks4Fglndb581JBIMN0M/dPxaKejOzH
61tOVRS3Kepk1L77tnv/XkeftjcHA1mrm0WCrS9sOlX2YEOg2Kt3HuY5nop7g9Xn
nwMA
&lt;span class="o"&gt;=&lt;/span&gt;/jWM
&lt;span class="nt"&gt;----------END&lt;/span&gt; PGP MESSAGE-----
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;--clearsign&lt;/code&gt; command on ASCII-armor format to sign a document (rather than the &lt;code&gt;--sign&lt;/code&gt; command) will include the original data in the signed file. The resulting file keeps the signed message at the top, making it easy to parse, whether we verify the signature or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--clearsign&lt;/span&gt; &lt;span class="nt"&gt;--armor&lt;/span&gt; message.txt
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;message.txt.asc

&lt;span class="nt"&gt;----------BEGIN&lt;/span&gt; PGP SIGNED MESSAGE-----
Hash: SHA256

hello, world
&lt;span class="nt"&gt;----------BEGIN&lt;/span&gt; PGP SIGNATURE-----

iMQEAQEIAC4WIQRGTnb6FsuAxEnEX3iXgXZPaWun4gUCXXr2FxAcdG90YWxAZ21h
aWwuY29tAAoJEJeBdk9pa6fi038D/1dXYUkFlKak1jRH369BjU5UaSmjiJvoWM8j
tQW78RVuYlOosL7bRSzgXJK1YH4hiz4rj3UWlbwRmF6s+UEu9wfDisHssHEiijru
LqCZpsWEDkzihjVwKSRz1UNIm1Egg4jyEV2ZY32dJnveN6wCiB1ABMe1zvB3G2cI
72Y3KOLr
&lt;span class="o"&gt;=&lt;/span&gt;nKbM
&lt;span class="nt"&gt;----------END&lt;/span&gt; PGP SIGNATURE-----
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Detached Signatures
&lt;/h3&gt;

&lt;p&gt;A popular way to sign software is through the use of &lt;em&gt;detached signatures&lt;/em&gt;. A detached signature is a small file that sits &lt;em&gt;external&lt;/em&gt; to the actual softare or piece of data being signed.&lt;/p&gt;

&lt;p&gt;Detached signatures have one great advantage over normal signatures: because they are separate, they are not mandatory in order to use the data. The signatures for popular software (say, Python, for example), are downloaded separately, and therefore do not interfere with one's ability to simply download and start using the software. On the other hand, verifying the validity of such signatures is just as easy as it is with regular signatures.&lt;/p&gt;

&lt;p&gt;We create a detached signature using the following command, which will produce a small file called &lt;code&gt;mySoftware.dmg.sig&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--detach-sign&lt;/span&gt; mySoftware.dmg
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;mySoftware.dmg
mySoftware.dmg.sig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let us assume we have downloaded a piece of software and its associated signature (&lt;code&gt;python.dmg&lt;/code&gt; and &lt;code&gt;python.dmg.sig&lt;/code&gt;). Provided the files are in the same directory and have the same name, we can verify the signature like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;python.dmg
python.dmg.sig

&lt;span class="nv"&gt;$ &lt;/span&gt;gpg &lt;span class="nt"&gt;--verify&lt;/span&gt; software.dmg.sig
gpg: assuming signed data &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s1"&gt;'python.dmg'&lt;/span&gt;
gpg: Signature made Sat 14 Sep 19:28:09 2019 EDT
gpg:                using RSA key 0E49C5D08660D9A2638FE8C2F31705D9893479A7
gpg:                issuer &lt;span class="s2"&gt;"mranderson@email.com"&lt;/span&gt;
gpg: Good signature from &lt;span class="s2"&gt;"MrAnderson &amp;lt;MrAnderson@email.com&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;ultimate]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Check out &lt;a href=""&gt;part 2&lt;/a&gt; for how to encrypt and decrypt data using GPG.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>pgp</category>
      <category>commandline</category>
    </item>
    <item>
      <title>Deconstructing a bitcoin transaction</title>
      <dc:creator>thunderbiscuit</dc:creator>
      <pubDate>Thu, 06 May 2021 00:38:55 +0000</pubDate>
      <link>https://dev.to/thunderbiscuit/deconstructing-a-bitcoin-transaction-4l2n</link>
      <guid>https://dev.to/thunderbiscuit/deconstructing-a-bitcoin-transaction-4l2n</guid>
      <description>&lt;p&gt;Here is a hex dump of the raw bitcoin transaction &lt;a href="https://blockstream.info/tx/e778e8765fdbb60f62e267de4705789f526a5fe9bb0c0f5e56ab4f566c5240eb" rel="noopener noreferrer"&gt;&lt;code&gt;e778e8765fdbb60f62e267de4705789f526a5fe9bb0c0f5e56ab4f566c5240eb&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbn2zkebn9s1t8zhbgc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbn2zkebn9s1t8zhbgc1.png" alt="raw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You've seen bitcoin transactions expressed in this form if you've ever tried to dive into the more technical bitcoin books, or have just been lurking around in the bitcoin community for long enough. They seem arcane and of impenetrable complexity, but it turns out that that's not the case at all; the above transaction, for example, is composed of &lt;strong&gt;15 small building blocks&lt;/strong&gt;, each one easy to find and interpret.&lt;/p&gt;

&lt;p&gt;Studying the structure of bitcoin transactions in their "true" form is a valuable quest for all bitcoiners. This article is written with the curious bitcoiner in mind looking to get a high level understanding of what exactly gets passed around from node to node on the network. Let's get to it.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Priors
&lt;/h2&gt;

&lt;p&gt;To really understand all pieces of a transaction, a basic grasp of 4 foundational, non-bitcoin concepts are important: &lt;strong&gt;bytes and hexadecimal notation&lt;/strong&gt;, &lt;strong&gt;endianness&lt;/strong&gt;, &lt;strong&gt;variable-length fields&lt;/strong&gt;, and &lt;strong&gt;varints&lt;/strong&gt;. I address all 4 of those in the sections below, but if you are familiar with computer science in general you most definitely won't need this kind of entry level review. If you are not familiar with them, however, I think you'll find them enlightening, and they will most definitely serve you well on your journey into bitcoin.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Bytes and Hexes
&lt;/h4&gt;

&lt;p&gt;You might have seen "raw" bitcoin transactions printed in hexadecimal format (the transaction above is an example of that). But of course computers only speak the language of bits (0s and 1s). A bitcoin transaction in it's computer-understandable form is therefore a string of binary digits. Moreover, those 0s and 1s are always kept in small groups of 8 bits, called &lt;strong&gt;bytes&lt;/strong&gt;. Here is an example of a byte: &lt;code&gt;1101 0110&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The problem with binary code is that it's not easy for humans to parse. The transaction above (and it's not even a big one) written in binary format is exactly 1,800 digits (&lt;code&gt;10101011100101000101100000110100000011101...&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;But binary numbers are still numbers; if we write them for humans we can write them in any we want that works best. Decimal notation (our regular number system), Roman numerals, Korean characters. etc.&lt;/p&gt;

&lt;p&gt;Decimal notation would be an obvious candidate, but it turns out that it is not very convenient for working with bytes. Take for example the bytes &lt;code&gt;1101 0110&lt;/code&gt; and &lt;code&gt;0000 0010&lt;/code&gt;. In decimal notation, the first number is &lt;code&gt;214&lt;/code&gt;, whereas the second number is &lt;code&gt;3&lt;/code&gt;. In fact every byte, when written in decimal notation, will take between 1 and 3 digits. That's not convenient, because then you'd never know when a byte ends and when the next one starts—how many bytes is &lt;code&gt;21431042&lt;/code&gt;? There can be multiple interpretations.&lt;/p&gt;

&lt;p&gt;Instead, a preferred numbering system for writing bytes for human consumption is the &lt;a href="https://en.wikipedia.org/wiki/Hexadecimal" rel="noopener noreferrer"&gt;hexadecimal number notation&lt;/a&gt;. A full explanation is beyond the scope of this post, but you should know that the number of possible arrangements of 4 bits is 16, and that the hexadecimal notation system has exactly... 16 digits. They map out nice and tidy with &lt;strong&gt;half a byte&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fskphq0vhceix9nksikus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fskphq0vhceix9nksikus.png" alt="hexes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that we can now represent our bytes as two hexadecimal digits, for a clean notation. Each byte is two hexadecimal digits, like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1nmvucudqvbot4iimg2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1nmvucudqvbot4iimg2.png" alt="bytes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you're looking at a big hexadecimal string like the one at the beginning of this post, you're really just looking at a neat little representation of a bunch of bytes, with each block of two characters representing one byte.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Big-Endian and Little-Endian Ordering
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Endianness" rel="noopener noreferrer"&gt;Endianness&lt;/a&gt; refers to the order of bytes within a representation of a number. You can think of it as the "direction" in which bytes should be read for meaning, while understanding that either direction does not influence the ultimate meaning of the bytes. &lt;/p&gt;

&lt;p&gt;Take for example the number &lt;em&gt;one thousand five hundred and ninety two&lt;/em&gt;. You are probably used to reading a number like this in the following way: &lt;code&gt;1592&lt;/code&gt;. At the same time, if I told you that I had this weird habit of always writing my numbers from right to left, you would still know that &lt;code&gt;2951&lt;/code&gt; means &lt;em&gt;one thousand five hundred and ninety two&lt;/em&gt;, because you'd know me and understand my weird habit.&lt;/p&gt;

&lt;p&gt;It turns out that some computer architectures are more efficient when working with numbers if they are stored with the &lt;em&gt;least significant byte first&lt;/em&gt; (the equivalent of reading right to left), and so a lot of the numbers we use when communicating with computers are "translated" to that format. We call this computer version of a number &lt;em&gt;little-endian&lt;/em&gt;, because it starts with the &lt;em&gt;little end&lt;/em&gt;. When shifting from big-endian to little-endian, it's the &lt;em&gt;bytes&lt;/em&gt; that we shift. This means shifting the last two characters (remember that one byte equals two hexadecimal characters) for the two up front, and so on. &lt;/p&gt;

&lt;p&gt;The number 220,000 in hexadecimal notation is written &lt;code&gt;03 5b 60&lt;/code&gt;, but expressed in little-endian it becomes &lt;code&gt;60 5b 03&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A real life example would be our transaction id for this article, which if referred to within a bitcoin transaction will be written in its little-endian form, but if you try to look it up in a block explorer, you'll need it's "human", big-endian version:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvj9nhxaiz1ipixq2wn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvj9nhxaiz1ipixq2wn4.png" alt="endian"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  3. Variable-Length Fields
&lt;/h4&gt;

&lt;p&gt;You'll notice that the bytes in a transaction are all glued together in one big continuous blob. How does the software know where an input start and where it ends? How does it know if a certain byte belongs to the number of bitcoin transmitted or to the receiving address? The flexibility offered by bitcoin transactions implies that there are an extremely rich number of combinations possible, and that the scripts required for unlocking utxos vary greatly in length.&lt;/p&gt;

&lt;p&gt;One way to deal with this uncertainty would be to give every field a set length in bytes. This is what the version field does, for example: the version number of a transaction is always written in the first 4 bytes of a transaction (see the &lt;code&gt;01000000&lt;/code&gt; number that starts the transaction below). In a lot of cases, however the length needed to transmit the necessary data differs widely between transactions: unlocking scripts for a simple Pay to Public Key hash might be 106 bytes long like in the transaction we are using in this post, but they can easily be 5 times that size on complex multisig scripts. Giving a fixed length to that data section would not only be inefficient (a lot of transactions would not need that much space at all), it would also be limiting, because scripts would have to stay under that size.&lt;/p&gt;

&lt;p&gt;A better way to deal with this and keep both flexibility and efficiency is by using a small marker at the beginning of a variable-length section that will give the software an indication of the lenght of the  section to follow. Here is an example of a series of 6 bytes: &lt;code&gt;05 e7 78 e8 76 5a&lt;/code&gt;. If we knew that the first byte was a indicator byte for the length of the section, a plain-english reading of this would then look like this: &lt;/p&gt;

&lt;p&gt;byte 1: &lt;code&gt;05 &amp;gt;&amp;gt;&amp;gt; the following section is 5 bytes long&lt;/code&gt;&lt;br&gt;
bytes 2 to 6: &lt;code&gt;e7 78 e8 76 5a &amp;gt;&amp;gt;&amp;gt; data&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Bitcoin transactions use a mix of fixed-length fields and variable-length fields. I'll make note of which ones use which in the description of each parts.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  4. Varints
&lt;/h4&gt;

&lt;p&gt;Variable Integers (varints) are a way to write a very wide range of numbers in a way that minimizes their cost on transaction space.&lt;/p&gt;

&lt;p&gt;To understand how they accomplish this, first note that the bigger the number we need to write down, the more bytes it requires. &lt;em&gt;Three&lt;/em&gt; is written as &lt;code&gt;00000010&lt;/code&gt; (1 byte), whereas &lt;code&gt;two million twenty nine thousand five hundred and twelve&lt;/code&gt; is written as &lt;code&gt;000111101111011111001000&lt;/code&gt; (3 bytes).&lt;/p&gt;

&lt;p&gt;The problem we are faced with is that all bytes are glued together in one long string of 0s and 1s, and the software needs to know exactly where each of the fields pertinent to a transaction start and end. One way to deal with this is to give fields a never-changing length, so that we always know when they end. This is what the version field does, for example: the version number of a transaction is always written in the first 4 bytes of a transaction (see the &amp;lt;code &lt;code&gt;01000000&lt;/code&gt; number that starts the transaction below). The problem with this approach is that if we sometimes need to accommodate numbers of great size, we will need to give the field a length with the ability to accommodate all of those numbers (say 8 bytes dedicated to a particular field). But if in most cases we only use the field for very small numbers, then a lot of those bytes are just wasted, because those small numbers only need 1 byte. If this type of field is required in multiple places in a transaction, all that waste starts to add up. Rather, we need a solution that will use &lt;em&gt;only the space required&lt;/em&gt; for the number we wish to write. This is what varints do; they use 1 byte for most of our use cases, and up to 9 bytes for the really big numbers we don't expect often.&lt;/p&gt;

&lt;p&gt;The way this is achieved is simple. A single byte can normally be used to represent the numbers 0 to 255. If the number we need represented (say, the number of inputs in a transaction) is &lt;em&gt;below&lt;/em&gt; 253, we write it in the first byte, and the software knows that that's all there is to it. If the number is big enough that it needs a few more bytes to write, we instead write &lt;em&gt;253&lt;/em&gt; in that first byte, which will be interpreted by the software as "read the next two bytes as the actual number I need to communicate". If the number is even bigger, we use 254 instead, meaning "read the next 4 bytes for the actual number", and if our number needs even more, we use 255, which implies the next 8 bytes are the actual representation of our number. Easy and efficient; most varints used in transactions never need to be more than one byte, but they can all grow to accommodate incredibly large numbers.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  A Legacy Bitcoin Transaction
&lt;/h2&gt;

&lt;p&gt;A "legacy" bitcoin transaction is the name we give a transaction that does not implement &lt;em&gt;Segregated Witness&lt;/em&gt;, a newer form of transaction in which the "witness" data (fields 5 and 6 below) are put into their own special section (we say they are &lt;em&gt;segregated&lt;/em&gt;, hence the name).&lt;/p&gt;

&lt;p&gt;These legacy transactions are perfectly valid bitcoin transactions, but they are being used less and less because of the efficiency gains made by the segregated witness approach resulting in lower fees, as well as its fix of the &lt;em&gt;transaction malleability&lt;/em&gt; bug, enabling, among others, the creation of lightning channels. Legacy transactions are easy to identify because they involve unlocking utxo(s) belonging to addresses starting with a 1. &lt;/p&gt;

&lt;p&gt;This article breaks down a typical legacy transaction where one utxo is used and spent into two: one payment to a payee, and one payment to a change address. It contains 15 different fields, and I describe each of their purpose below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faeo7hh51zpi6gphyeh17.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faeo7hh51zpi6gphyeh17.png" alt="transaction"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd16rdos2vz1r74dh2llz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd16rdos2vz1r74dh2llz.png" alt="parts"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [1] transaction version number
&lt;/h4&gt;

&lt;p&gt;This field specifies the type of transaction being transmitted. It is of &lt;strong&gt;fixed-length&lt;/strong&gt; (4 bytes) and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;01000000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The field indicates that this transaction is of version 1.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [2] number of inputs
&lt;/h4&gt;

&lt;p&gt;This field expresses how many inputs will be unlocked by the transaction. Each of those inputs will need to be identified (here with fields 3 and 4), and unlocked (here with fields 5 and 6). In our case there is only one input, and so we only need to go through this loop once, but in the case where there are many inputs, we repeat the fields 3 to 6 as many times as there are inputs. This field is a &lt;strong&gt;varint&lt;/strong&gt;, is &lt;strong&gt;little-endian&lt;/strong&gt;, and can grow up to 9 bytes.&lt;/p&gt;
&lt;h6&gt;
  
  
  In Our Example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;01&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The byte indicates that the transaction unlocks only one UTXO.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [3] previous transaction id
&lt;/h4&gt;

&lt;p&gt;This field expresses the transaction which contains the output to be unlocked by the unlocking script in the coming fields 5 and 6. It is of &lt;strong&gt;fixed-length&lt;/strong&gt; (32 bytes), and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In Our Example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;04dde43b0e4724f1e3b45782a9bfbcc91ea764c7cb1c245fba1fefa175c3a5d0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Because the field is little-endian, if you wish to search for that transaction in a block explorer you'll need to convert it to big endian first: &lt;a href="https://blockstream.info/tx/d0a5c375a1ef1fba5f241ccbc764a71ec9bcbfa98257b4e3f124470e3be4dd04" rel="noopener noreferrer"&gt;&lt;code&gt;d0a5c375a1ef1fba5f241ccbc764a71ec9bcbfa98257b4e3f124470e3be4dd04&lt;/code&gt;&lt;/a&gt;. A look at that transaction will reveal that there were 2 outputs to it. Which one of those two is unlocked by the signature script is defined in the next field.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [4] ouput number in previous transaction
&lt;/h4&gt;

&lt;p&gt;Defining the transaction an output comes from is not precise enough—there might be more than one. We need to know which output from that transaction is being unlocked, and this field expresses that. It is of &lt;strong&gt;fixed-length&lt;/strong&gt; (4 bytes), and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In Our Example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;01000000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is the number 1 written in little endian, indicating that the output being unlocked is the second one in transaction &lt;a href="https://blockstream.info/tx/d0a5c375a1ef1fba5f241ccbc764a71ec9bcbfa98257b4e3f124470e3be4dd04" rel="noopener noreferrer"&gt;&lt;code&gt;d0a5c375a1ef1fba5f241ccbc764a71ec9bcbfa98257b4e3f124470e3be4dd04&lt;/code&gt;&lt;/a&gt;. It is valid for 300,000 satoshis, or 0.00300000 bitcoin.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [5] size of the unlocking script
&lt;/h4&gt;

&lt;p&gt;This field indicates the number of bytes taken by the unlocking script, the field that follows it. It is a &lt;strong&gt;varint&lt;/strong&gt;, and can take up to 9 bytes.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;6a&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This byte is the hexadecimal representation of 106, meaning our unlocking script (field 6) will be 106 bytes long (212 hex characters).&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [6] Unlocking script
&lt;/h4&gt;

&lt;p&gt;You can think of the unlocking script as the key that unlocks the utxo. If any of the unlocking scripts fail for any of the input utxos, the whole transaction fails. If all unlocking scripts succeed, the signer has proven they have ownership of the coins, and the transaction can move forward to the next steps. This field is of &lt;strong&gt;variable length&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;4730440220519f7867349790ee441e83e545afbd25b954a34e0733cd4da3b5f1e5588625050220166730d053c3672973bcb2bb1a977b747837023b647e3af2ac9c15728b0681da01210236ccb7ee3a9f154127f384a05870c4fd86a8727eab7316f1449a0b9e65bfd90d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The unlocking script is written in a language called &lt;em&gt;Script&lt;/em&gt;, a language unique to bitcoin. It is beyond the scope of this article to look at the exact unlocking script used in this transaction, but we know it was a valid script, since the transaction was indeed propagated by the network, and later on mined.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [7] sequence number
&lt;/h4&gt;

&lt;p&gt;The sequence number is a field initially designed for a purpose it never fulfilled. Nowadays it is often disabled  by setting it to &lt;code&gt;ffffffff&lt;/code&gt;. It can used to signal that a transction  is replace-by-fee enabled as per &lt;a href=""&gt;BIP 125&lt;/a&gt;, by setting the field equal to any number below &lt;code&gt;ffffffff -1&lt;/code&gt;. In some cases, the field is used to set timelocks (to enable this, verion 2 of a transaction must be declared in field 1). It is of &lt;strong&gt;fixed-length&lt;/strong&gt; (4 bytes) and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;ffffffff&lt;/code&gt;&lt;br&gt;
The field is disabled in this transaction.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [8] number of outputs
&lt;/h4&gt;

&lt;p&gt;This field expresses how many outputs the transaction will create. It is a &lt;strong&gt;varint&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;02&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The transaction has two outputs.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [9] amount going to output 0
&lt;/h4&gt;

&lt;p&gt;This field expresses the amount of bitcoin being locked in output 0, expressed in satoshis. It is a &lt;strong&gt;fixed-length&lt;/strong&gt; field of 8 bytes, and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;5d36010000000000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output 0 locks in 79,453 satoshis.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [10] size of locking script for output 0
&lt;/h4&gt;

&lt;p&gt;This field expresses the size of the locking script for output 0. It is a &lt;strong&gt;varint&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;19&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This byte is the hexadecimal representation of 25, meaning our locking script will be 25 bytes long (50 hex characters)&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [11] locking script for output 0
&lt;/h4&gt;

&lt;p&gt;This field is the locking script for output 0. It is a &lt;strong&gt;variable-length&lt;/strong&gt; field.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;76a91478364a559841329304188cd791ad9dabbb2a3fdb88ac&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can think of this field as a of lock we put on output 0. It is written in Script, bitcoin's own programming language.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [12] amount going to output 1
&lt;/h4&gt;

&lt;p&gt;This field expresses the amount of bitcoin being locked in output 1, expressed in satoshis. It is a &lt;strong&gt;fixed-length&lt;/strong&gt; field of 8 bytes, and is &lt;strong&gt;little-endian&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;605b030000000000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output 1 locks in 220,000 satoshis.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [13] size of locking script for output 1
&lt;/h4&gt;

&lt;p&gt;This field expresses the size of the locking script for output 1. It is a &lt;strong&gt;varint&lt;/strong&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;19&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This byte is the hexadecimal representation of 25, meaning our locking script will be 25 bytes long (50 hex characters).&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [14] locking script for output 1
&lt;/h4&gt;

&lt;p&gt;This field is the locking script for output 0. It is a variable-length field. This is a &lt;strong&gt;variable-length&lt;/strong&gt; field.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;76a914064e0aa817486573f4c2de09f927697e1e6f233f88ac&lt;/code&gt;&lt;br&gt;
We can think of this field as a of lock we put on output 1. It is written in Script, bitcoin's own programming language.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  [15] nLocktime
&lt;/h4&gt;

&lt;p&gt;nLocktime field allows for a transaction to be unspendable until a certain point in the future. If the field is set to &lt;code&gt;00000000&lt;/code&gt;, the transaction is spendable right away. If they field is any number &lt;em&gt;below&lt;/em&gt; 500 million, it is intepreted as a block height. If it is &lt;em&gt;above&lt;/em&gt; 500 million, it is interpreted as a Unix timestamp. Transactions with locktimes on them will not be propagated by nodes if they are not valid at the time a node see it, hence the sender must wait until the transaction is valid before broadcasting.&lt;/p&gt;
&lt;h6&gt;
  
  
  In our example:
&lt;/h6&gt;

&lt;p&gt;&lt;code&gt;00000000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In our example the nLocktime field is such that the transaction is spendable right away.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Some notes on the above transaction
&lt;/h2&gt;

&lt;p&gt;The example used here is a type of transaction known as a &lt;em&gt;Pay to Public Key Hash&lt;/em&gt;, or P2PKH. It is the simplest form of transactions we see nowadays. The transaction hex has 450 characters, and the transaction is therefore 225 bytes in size.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  TXID
&lt;/h2&gt;

&lt;p&gt;The txid (transaction identifier) is derived from hashing the transaction data twice using SHA256. You can test this with our example transaction right in your shell. The following command basically takes the hex dump of the transaction, converts it to binary, hashes it, then converts that result to binary again, and hashes it once more. It is then printed to console in little-endian, hex format. Notice that you'll need to convert it to big-endian if you want to use it in a block explorer!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;010000000104dde43b0e4724f1e3b45782a9bfbcc91ea764c7cb1c245fba1fefa175c3a5d0010000006a4730440220519f7867349790ee441e83e545afbd25b954a34e0733cd4da3b5f1e5588625050220166730d053c3672973bcb2bb1a977b747837023b647e3af2ac9c15728b0681da01210236ccb7ee3a9f154127f384a05870c4fd86a8727eab7316f1449a0b9e65bfd90dffffffff025d360100000000001976a91478364a559841329304188cd791ad9dabbb2a3fdb88ac605b0300000000001976a914064e0aa817486573f4c2de09f927697e1e6f233f88ac00000000 | xxd &lt;span class="nt"&gt;-revert&lt;/span&gt; &lt;span class="nt"&gt;-plain&lt;/span&gt; | &lt;span class="nb"&gt;sha256sum&lt;/span&gt; | xxd &lt;span class="nt"&gt;-revert&lt;/span&gt; &lt;span class="nt"&gt;-plain&lt;/span&gt; | &lt;span class="nb"&gt;sha256sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  On Signing and Broadcasting
&lt;/h2&gt;

&lt;p&gt;An often overlooked aspect of bitcoin transactions is how creating them and broadcasting them are two completely separate tasks, and that they can be done independently of each other. We mostly use wallets that construct and sign transactions and &lt;em&gt;also&lt;/em&gt; broadcast them for us, but it does not have to be so.&lt;/p&gt;

&lt;p&gt;This is what projects like &lt;a href="https://txtenna.com/" rel="noopener noreferrer"&gt;TxTenna&lt;/a&gt; and the Lightning Network are leveraging.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;I hope this article proved to be an interesting way to peel the first layer on bitcoin transactions if you had not seen them this way before. More to come!&lt;/p&gt;

</description>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
