Skip to content

Instantly share code, notes, and snippets.

@walski
Last active August 29, 2015 13:57
Show Gist options
  • Save walski/9829681 to your computer and use it in GitHub Desktop.
Save walski/9829681 to your computer and use it in GitHub Desktop.

Generate keys

# Generate THE key
$ openssl genrsa -out mykey.pem 2048
# Generate the private key for the backend
$ openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem -out private_key.pem -nocrypt
# Generate the public key for iOS
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der

Prepare the public key for iOS

Fire up IRB:

require 'base64'
public_key_string = Base64.encode64(File.read('public_key.der'))
# easy copy & paste hack on iOS
`echo '#{public_key_string}'|pbcopy`

Pass that string to the iOS team.

Sign a message in the backend

First we need to have the private key set as a Base64 encoded string in the env

require 'base64'
key = File.read('private_key.pem')
key_string = Base64.encode64(key)

Then set the key_string as the MESSAGE_SIGNING_KEY environment variable.

After that we can actually sign messages like this:

require 'base64'
require 'openssl'
require 'digest'

message = "Hallo Dorsch"
signing_key = OpenSSL::PKey::RSA.new(Base64.decode64(ENV['MESSAGE_SIGNING_KEY']))
signature = signing_key.sign(OpenSSL::Digest::SHA256.new, message)
signature_string = Base64.encode64(signature.to_s)

Then pass the signature_string to iOS alongside with message.

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