Skip to content

Instantly share code, notes, and snippets.

@willfarrell
Last active June 17, 2018 22:14
Show Gist options
  • Save willfarrell/e9b7553367f5edca0ac7e0b8e9647a04 to your computer and use it in GitHub Desktop.
Save willfarrell/e9b7553367f5edca0ac7e0b8e9647a04 to your computer and use it in GitHub Desktop.
Generate all SSH key pairs
#! /bin/bash
# Use Examples
# ./ssh-keygen Additional comments
# ./ssh-keygen "(Work)"
ROUNDS=100
if hash networksetup 2>/dev/null; then
# Mac only: Computer Name
COMMENT="$(networksetup -getcomputername) $@"
else
COMMENT="$@"
fi
# remove leading and trailing spaces
COMMENT="$(echo "$COMMENT" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
echo "-----> Generating SSH Keys ($COMMENT)"
if [ ! -f ~/.ssh/id_rsa ]; then
echo -e 'y\n'|ssh-keygen -q -t rsa -b 4096 -o -a ${ROUNDS} -N '' -C "$COMMENT" -f ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa
echo "~/.ssh/id_rsa"
else
echo "~/.ssh/id_rsa Skipped!"
fi
if [ ! -f ~/.ssh/id_ecdsa ]; then
echo -e 'y\n'|ssh-keygen -q -t ecdsa -b 521 -o -a ${ROUNDS} -N '' -C "$COMMENT" -f ~/.ssh/id_ecdsa
ssh-add ~/.ssh/id_ecdsa
echo "~/.ssh/id_ecdsa"
else
echo "~/.ssh/id_ecdsa Skipped!"
fi
if [ ! -f ~/.ssh/id_ed25519 ]; then
echo -e 'y\n'|ssh-keygen -q -t ed25519 -o -a ${ROUNDS} -N '' -C "$COMMENT" -f ~/.ssh/id_ed25519
ssh-add ~/.ssh/id_ed25519
echo "~/.ssh/id_ed25519"
else
echo "~/.ssh/id_ed25519 Skipped!"
fi
echo "-----> Generating Secure Enclave Key ($COMMENT)"
if hash sekey 2>/dev/null; then
if [ ! -f ~/.ssh/id_ecdsa256.pub ]; then
sekey --generate-keypair "$COMMENT"
keyline=$(sekey --list-keys | grep "$COMMENT")
keyarr=($keyline)
keyarrlen=${#keyarr[@]}
key=${keyarr[((keyarrlen-2))]}
echo $key
sekey --export-key $key > ~/.ssh/id_ecdsa256.pub
echo "~/.ssh/id_ecdsa256.pub (Private key is stored in the Secure Enclave)"
else
echo "~/.ssh/id_ecdsa256 (Secure Enclave) Skipped!"
fi
else
echo "SeKey not installed. (https://github.com/ntrippar/sekey)"
echo "1. Ensure you have TouchId built-in to your Mac"
echo "2. $ brew cask install sekey"
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment