Skip to content

Instantly share code, notes, and snippets.

@vzsg
Last active July 20, 2017 12:48
Show Gist options
  • Save vzsg/376750864a8ec3429f212c147976d8bb to your computer and use it in GitHub Desktop.
Save vzsg/376750864a8ec3429f212c147976d8bb to your computer and use it in GitHub Desktop.
Vapor JWT extension for parsing RSA keys from PEM, e.g. for Firebase Authentication
extension RSAKey {
init?(pem: String) {
let bio = BIO_new(BIO_s_mem())
defer {
BIO_free(bio)
}
_ = pem.withCString { key in
BIO_puts(bio, key)
}
guard let pkey = PEM_read_bio_PrivateKey(bio, nil, nil, nil) else {
return nil
}
defer {
EVP_PKEY_free(pkey)
}
guard let rsa = EVP_PKEY_get1_RSA(pkey) else {
return nil
}
self = .private(rsa)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment