Skip to content

Instantly share code, notes, and snippets.

@jeroen
Created December 7, 2016 14:02
Show Gist options
  • Save jeroen/1ffbfbdad40f4aad6657c337a4924f0e to your computer and use it in GitHub Desktop.
Save jeroen/1ffbfbdad40f4aad6657c337a4924f0e to your computer and use it in GitHub Desktop.
Encrypt using Github public key
github_pubkey <- function(user){
url <- sprintf("https://api.github.com/users/%s/keys", user)
keys <- jsonlite::fromJSON(url)
lapply(keys$key, openssl::read_pubkey)
}
# Get pubkey from Gabor
gabor <- github_pubkey('gaborcsardi')
pubkey <- gabor[[1]] #has 3 keys, use first one
# Example of old repo
buf <- openssl::rsa_encrypt(charToRaw("My secret token"), pubkey)
openssl::base64_encode(buf)
@jennybc
Copy link

jennybc commented Dec 9, 2016

Gabor used the above workflow to send me a GitHub access token we needed to share. This token played the role of "My secret token" and he looked up and encrypted with my public key from GitHub.

Here's how I got the token back out again!

library(openssl)

## here's where I put the base64-encoded, encrypted token Gabor sent me
enc_enc_token <- "BLAH BLAH BLAH"

## decode the token
enc_token <- base64_decode(enc_enc_token)

## optional: load my private key explicitly, then decrypt ... my original approach
#pvt_key <- read_key("~/.ssh/id_rsa")
#raw_token <- rsa_decrypt(enc_token, pvt_key)

## decrypt the token ... streamlined with Jeroen's tip re: my_key()
raw_token <- rsa_decrypt(enc_token, my_key())

## convert from bytes to character
token <- rawToChar(raw_token)
token ## ... and we're done!

@jeroen
Copy link
Author

jeroen commented Dec 10, 2016

Note that you can use my_key() instead of read_key("~/.ssh/id_rsa").

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