Last active
July 20, 2017 12:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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