<p>A new package <a href="https://cran.r-project.org/web/packages/gpg/index.html">gpg</a> has appeared on CRAN. From the package description:</p>
<blockquote>
<p>Bindings to GnuPG for working with OpenGPG (RFC4880) cryptographic methods. Includes utilities for public key encryption, creating and verifying digital signatures, and managing your local keyring. Note that some functionality depends on the version of GnuPG that is installed on the system. In particular GnuPG 2 mandates the use of 'gpg-agent' for entering passphrases, which only works if R runs in a terminal session.</p>
</blockquote>
<p>The package features a beautiful <a href="https://cran.r-project.org/web/packages/gpg/vignettes/intro.html">vignette</a> to get you started with using GPG in R. Some highlights from the vignette below. </p>
<h2>Example: encryption</h2>
<p>Suppose we want to send an email Glenn Greenwald containing top secret information. His <a href="https://theintercept.com/staff/glenn-greenwald/">homepage at the intercept</a> shows Greenwalds GPG fingerprint. Let's import his public key:</p>
<div><pre><code><span>library</span><span>(</span>gpg<span>)</span>
glenn <span>&lt;-</span> <span>'734A3680A438DD45AF6F5B99A4A928C769CD6E44'</span>
gpg_recv<span>(</span>glenn<span>)</span>
</code></pre></div>
<p>We can now encrypt messages for this key:</p>
<div><pre><code><span>writeLines</span><span>(</span><span>&quot;This is a secret message&quot;</span><span>,</span> <span>&quot;secret.txt&quot;</span><span>)</span>
<span>cat</span><span>(</span>gpg_encrypt<span>(</span><span>&quot;secret.txt&quot;</span><span>,</span> receiver <span>=</span> glenn<span>))</span>
</code></pre></div><div><pre><code>-----BEGIN PGP MESSAGE-----

hQIMAzCzOshC83uFARAAqg4mYp/6HTx/6xJjMNo5WaVKCtCv+yqr4tA4qZ7Mtsxz
Cxud4hgGfuqZ45S3J6qsbFuoWJBlmJPYrBoTNjO+dkdi5H5oURLkOo5l+TInhSoL
w/9ClxjG7HLFxKWp7rG5tdHQT7bGxr6Q0YkrI1OX/tTh/NracciQeCDZdcVRHuvO
uaok4r44mWlRavGncL6PHDAleWEky784hal3uxgUGo9M75jgQayDFx9bNA50dEzu
VBTIiLYH/wN90S4o5vTIWRsL2DVkUv9ZVYvHZ+kYO2zO4eoPNeqaA+ajYCIqGvKS
8NcNhF0cZUiKvtsqCuenCr1T9e/h4V9JGnz+1tdThn76J4Lf8cTcdTIqZ34D4SHz
HsjvceOV0m+pUcBL0HcOorZIKF1sPjsyX5LZqgccIecD7ouznF7qqYgahTjHCWIW
Da3EWth/ihxngmDrxbgbYSzGTXt9HLbd+Sil4lh7Tm8aS73JBZvEpDQyLVVeOxuB
PiDU1Iz0rS5Hlw2/PEszjL312W5RLjgho5CiXPyENiqJUGdVV5BvvzSh292vjv9h
m10zmUqs1B3WFgEI/yOcOzBx/1Vpn7xOsIpcWY7pj7QfAEMxwWiuig0nYWbOHXj9
heqNgw1QzEK/lulz+6OyR4JlYfrOY0+LhAATSzoCchIIJD++w+YApdBp7Y4pdTfS
VQHK9T1ynrRz5IpLpwnBOVxWq6ngNiaIzCcX6SvogiWcUcec55eKsDqxuMWcUPrH
3jKHYqMqEzvbLlpo2lSjdmwMb3j+EusS/kN64HBaMqwvVsfyCaI=
=lkbq
-----END PGP MESSAGE-----
</code></pre></div>
<p>You can safely send this message over any channel (email, twitter, etc). Nobody in the world (not even ourselves) will be able to decipher this message, except for Glenn Greenwald.</p>
<h2>Example: digital signatures</h2>
<p>GPG is most widely used for digital signatures. For example the <a href="https://cran.r-project.org/bin/linux/debian/">Debian page on CRAN</a> explains that the backports archives on CRAN are signed with the key of Johannes Ranke with key fingerprint <strong>6212 B7B7 931C 4BB1 6280 BA13 06F9 0DE5 381B A480</strong>. Let’s import this key:</p>
<div><pre><code><span># take out the spaces</span>
johannes <span>&lt;-</span> <span>gsub</span><span>(</span><span>&quot; &quot;</span><span>,</span> <span>&quot;&quot;</span><span>,</span> <span>&quot;6212 B7B7 931C 4BB1 6280 BA13 06F9 0DE5 381B A480&quot;</span><span>)</span>
gpg_recv<span>(</span>johannes<span>)</span>
</code></pre></div><div><pre><code>considered   imported  unchanged 
         1          1          0  
</code></pre></div>
<p>We can now verify the <a href="https://cran.r-project.org/bin/linux/debian/jessie-cran3/Release">Release</a> file, which contains checksums for all files in the repository.</p>
<div><pre><code><span># Verify the file</span>
<span>library</span><span>(</span>curl<span>)</span>
curl_download<span>(</span><span>'https://cran.r-project.org/bin/linux/debian/jessie-cran3/Release'</span><span>,</span> <span>'Release'</span><span>)</span>
curl_download<span>(</span><span>'https://cran.r-project.org/bin/linux/debian/jessie-cran3/Release.gpg'</span><span>,</span> <span>'Release.gpg'</span><span>)</span>
gpg_verify<span>(</span><span>'Release'</span><span>,</span> <span>'Release.gpg'</span><span>)</span>
</code></pre></div><div><pre><code>                               fingerprint           timestamp hash pubkey success
1 6212B7B7931C4BB16280BA1306F90DE5381BA480 2016-06-22 09:26:03 SHA1    DSA    TRUE
</code></pre></div>
<p>Looking good! We can trust the checksums in the Release file to be legitimate.</p>
<p>Check out the package <a href="https://cran.r-project.org/web/packages/gpg/vignettes/intro.html">vignette</a> for more examples!</p>