Skip to content

Instantly share code, notes, and snippets.

@jpbochi
Last active October 10, 2018 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbochi/a1723214452eeccaca0de1728bc0c14b to your computer and use it in GitHub Desktop.
Save jpbochi/a1723214452eeccaca0de1728bc0c14b to your computer and use it in GitHub Desktop.
bash SSH encrypt/decrypt/sign
#!/usr/bin/env sh
set -eu
# some inspiration from https://raymii.org/s/tutorials/Sign_and_verify_text_files_to_public_keys_via_the_OpenSSL_Command_Line.html
MESSAGE=$1
ID_FILE=$(ssh -G git@github.com | grep identityfile | cut -d' ' -f2 | xargs -I % sh -c 'test -r % && echo % || true' | head)
echo >&2 '>>> decrypting with this identity file:' $ID_FILE
set -o pipefail
printf $MESSAGE | base64 --decode | openssl rsautl -decrypt -inkey $ID_FILE
#!/usr/bin/env sh
set -eu
# some inspiration from https://raymii.org/s/tutorials/Sign_and_verify_text_files_to_public_keys_via_the_OpenSSL_Command_Line.html
# MESSAGE=$(pbpaste | tr -d '\n')
MESSAGE=$1
ID_FILE=$(ssh -G git@github.com | grep identityfile | cut -d' ' -f2 | xargs -I % sh -c 'test -r % && echo % || true' | head)
echo >&2 '>>> encrypting with this identity file:' $ID_FILE
set -o pipefail
printf $MESSAGE | openssl rsautl -encrypt -inkey ./le-github-app.2018-05-11.private-key.pem | base64 | tr -d '\n'
#!/usr/bin/env sh
set -eu
# some inspiration from https://raymii.org/s/tutorials/Sign_and_verify_text_files_to_public_keys_via_the_OpenSSL_Command_Line.html
MESSAGE=$1
ID_FILE=$(ssh -G git@github.com | grep identityfile | cut -d' ' -f2 | xargs -I % sh -c 'test -r % && echo % || true' | head)
echo >&2 '>>> signing with this identity file:' $ID_FILE
set -o pipefail
SIGNATURE=$(printf $MESSAGE | openssl dgst -sha256 -sign $ID_FILE | base64 | tr -d '\n')
ESCAPED_MESSAGE=$(printf $MESSAGE | sed 's/"/\\"/g')
echo '{"message":"'$ESCAPED_MESSAGE'","signature":"'$SIGNATURE'"}'
@jpbochi
Copy link
Author

jpbochi commented Oct 10, 2018

more complete version at https://github.com/jpbochi/id-check

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