{"id":86,"date":"2010-03-27T19:07:04","date_gmt":"2010-03-27T23:07:04","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/djcp\/?p=86"},"modified":"2010-07-30T13:49:27","modified_gmt":"2010-07-30T17:49:27","slug":"a-simple-bash-password-safe","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/djcp\/2010\/03\/a-simple-bash-password-safe\/","title":{"rendered":"A simple bash gpg &#8220;password safe&#8221;"},"content":{"rendered":"<p>This is definitely not military quality &#8211; but if you need a simple way to manage a GPG-encrypted file containing info you want to protect this works pretty well for me.  I use this to manage a set of passwords on a trusted machine that I can ssh to.<\/p>\n<p>It assumes you&#8217;re using a bash-like shell and have a trusted private key available in the account you&#8217;re running it on.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\r\n#!\/bin\/bash\r\n\r\nKEYS=&quot;&quot;\r\n\r\nif &#x5B; ! -e &quot;$HOME\/private&quot; ]\r\nthen\r\n    mkdir -m 700 &quot;$HOME\/private&quot;\r\n    umask 77 &quot;$HOME\/private\/&quot;\r\nfi\r\n\r\n# Always delete the unencrypted file at the end of the session. We DO NOT want this hanging around.\r\ntrap &quot;rm -f \\&quot;$HOME\/private\/${USER}_private_store.txt\\&quot;; chmod 600 \\&quot;$HOME\/private\/${USER}_private_store.txt\\&quot;*; exit&quot; INT TERM EXIT\r\n\r\ntouch &quot;$HOME\/private\/${USER}_private_store.txt&quot;\r\nchmod 600 &quot;$HOME\/private\/${USER}_private_store.txt&quot;\r\n\r\ngpg --decrypt &quot;$HOME\/private\/${USER}_private_store.txt.asc&quot; &gt; &quot;$HOME\/private\/${USER}_private_store.txt&quot;\r\nvim &quot;$HOME\/private\/${USER}_private_store.txt&quot;\r\n\r\nmd5sum  &quot;$HOME\/private\/${USER}_private_store.txt.md5sum.new&quot;\r\n\r\nif &#x5B; -e &quot;$HOME\/private\/${USER}_private_store.txt.md5sum&quot; ]\r\nthen\r\n    if &#x5B; &quot;`cmp &quot;$HOME\/private\/${USER}_private_store.txt.md5sum.new&quot; &quot;$HOME\/private\/${USER}_private_store.txt.md5sum&quot;`&quot; == &quot;&quot; ]\r\n    then\r\n        clear\r\n        rm -f &quot;$HOME\/private\/${USER}_private_store.txt.md5sum.new&quot;\r\n        echo &#039;No changes, not re-encrypting&#039;\r\n        exit\r\n    fi\r\nfi\r\n\r\nmv &quot;$HOME\/private\/${USER}_private_store.txt.md5sum.new&quot; &quot;$HOME\/private\/${USER}_private_store.txt.md5sum&quot;\r\n\r\necho &#039;File has changed. Re-encrypting. . .&#039;\r\ngpg -a --encrypt -r $KEYS &quot;$HOME\/private\/${USER}_private_store.txt&quot;\r\nclear\r\n\r\n<\/pre>\n<p>First time it runs it&#8217;ll create a private directory, start vim, and encrypt the text you enter into vim. On subsequent runs it&#8217;ll prompt you for your private key passphrase and repeat the cycle. It won&#8217;t re-encrypt if there haven&#8217;t been any changes.<\/p>\n<p>I&#8217;m betting wordpress messes up the code, so here&#8217;s the text file: <a href=\"http:\/\/blogs.law.harvard.edu\/djcp\/files\/2010\/03\/edit_password_safe.sh\">edit_password_safe.sh<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is definitely not military quality &#8211; but if you need a simple way to manage a GPG-encrypted file containing info you want to protect this works pretty well for me. I use this to manage a set of passwords &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/djcp\/2010\/03\/a-simple-bash-password-safe\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1984,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3484,4,15084,5039],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bash","tag-code","tag-gpg","tag-infosec"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/users\/1984"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":4,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/86\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}