# 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
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.
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
.