Code Monkey home page Code Monkey logo

swiftrsa's Introduction

SwiftRSA

codebeat badge

SwiftRSA is a simple wrapper of Apple Security API which makes RSA encryption and decryption easy to use. It is motivated by creating the app of boxueio.com.

Features

  • Fully tested.
  • Inituitive interface.
  • Updated to Swift 5.

SwiftRSA 101

Create a public / private key

With a PEM encoded string

let pemPrivate = """
  -----BEGIN RSA PRIVATE KEY-----
  MIIEowIBAAKCAQEA0HRZppSvYCAFTo+ie5z7EXmzFJ9rSpuUJUAAvikOq/lGVqaK
  ...
  2bhAIUk7eWJTorZYujzXO+HmDt+8/ha+RBAtgQPDFPGHG/QaZik8
  -----END RSA PRIVATE KEY-----
  """
let pemPublic = """
  -----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0HRZppSvYCAFTo+ie5z7
  ...
  mwIDAQAB
  -----END PUBLIC KEY-----
  """

guard let privateKey = PrivateKey(pemEncoded: pem) else {
  // Invalid pem string
}

guard let publicKey = PublicKey(pemEncoded: pemPublic) else {
  // Invalid pem string
}

With a DER encoded string

guard let privateKey = PrivateKey(der: data) else {
  // Invalid DER data
}

guard let publicKey = PublicKey(der: data) else {
  // Invalid DER data
}

Encrypt with a public key

let ct = ClearText(string: "Hello world")
let encrypted = try ct.encrypted(with: publicKey, by: .rsaEncryptionOAEPSHA512)
/// `encrypted` is an `EncryptedText` object.

Decrypt with a private key

/// `originText` is a `ClearText` object
let originText = try encrypted.decrypted(with: privateKey)

Get data from an EncryptedText object

let data = encrypted.data

Get string from a ClearText object

let string = originText.stringValue

Installation

To integrate SwiftRSA into your Xcode project using Carthage, speicify the following line in your Cartfile:

github "puretears/SwiftRSA" ~> 0.1

Requirements

  • iOS 10.0+
  • Swift 4.0+

Next Steps

  • Cocoapods and SPM support;
  • More RSA algorithm support;
  • Add X.509 certificate support;
  • Add sign and verify support;

Release History

  • 0.1
    • Initial release

License

SwiftRSA is released under the MIT license. See LICENSE for details.

swiftrsa's People

Contributors

puretears avatar morningstarj avatar cheungbo-mong avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.