How can I generate an RSA key pair in Swift on Linux?

Viewed 401

I know that when using Swift on iOS or macOS you can use SecKeyGeneratePair, but the Security library is unavailable on Linux. Short of falling back on Process to use the the OpenSSL CLI interface, is there any way to generate an RSA key pair in Swift.

FYI, I'm using Vapor 3 to build a web API.

2 Answers

You can use the cross-platform library Themis (Apache 2) + example Swift wrappers. The nice thing is that you can use the same API regardless of programming language or location (back/front).

You can generate both RSA & EC keys. Tested on Vapor + Ubuntu 16.04

So this is many months later, but I thought I would at least update for any others. While I looked into Andrei's suggestion above to use Themis, in the end it really was simplest to simply use Process that I was avoiding originally.

I had to call some local scripts anyway, so I wrote a dead simple Shell class that wrapped Swift's Process. It's not multithreaded, so it's not great for performance. But it works for what I need. I just called OpenSSL's CLI and grabbed the output.

Shell Gist: https://gist.github.com/mattmaddux/9979ec992c2b9744b669bd1728c28c19

Related