Skip to content

Instantly share code, notes, and snippets.

@kraftb
Created April 1, 2014 16:49
Show Gist options
  • Save kraftb/9918106 to your computer and use it in GitHub Desktop.
Save kraftb/9918106 to your computer and use it in GitHub Desktop.
Generate public/private keypair and output to stdout
#!/bin/bash
BITS=2048
# In one line:
# rm -f temp.key && ssh-keygen -t rsa -b 2048 -f temp.key -N "" -q && ssh-keygen -e -f temp.key -m PKCS8 | tr "\n" " " && echo && cat temp.key | tr "\n" " " && echo
# In multiple lines:
rm -f temp.key
ssh-keygen -t rsa -b $BITS -f temp.key -N "" -q
echo
ssh-keygen -e -f temp.key -m PKCS8 | tr "\n" " "
echo
echo
cat temp.key | tr "\n" " "
echo
echo
@sanmai-NL
Copy link

@allisonkarlitskaya Thanks for your contribution. Note that much of the trickery here, that is, all solution other than using openssl or your first Python solution, will fail if you work on a read-only filesystem.

@cjshearer
Copy link

Here's a practical example of this for encrypting the ssh key with sops and age in a taskfile:

version: "3"

gen-key:
  desc: Generate encrypted ssh key
  silent: true
  status:
    - test -f ssh.sops.key
  cmds:
    - mkfifo key key.pub
    - defer: rm key key.pub
    - cat key | sops -e /dev/stdin > ssh.sops.key &
    - 'printf "Your public key:\n$(cat key.pub)\n" &'
    - yes | ssh-keygen -t ed25519 -f key > /dev/null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment