• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Swift
  • Created over 9 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RSA Utility Functions for Swift

This project has been merged to https://github.com/btnguyen2k/swiftutils. This repository is no longer maintained!

Swift-RSAUtils

RSA encryption and decryption with Swift iOS.

This project is to solve the following problems (actually mine)

  • Server had already generated a pair of public/private key, for example: public key MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJh+/sdLdlVVcM5V5/j/RbwM8SL++Sc3dMqMK1nP73XYKhvO63bxPkWwaY0kwcUU40+QducwjueVOzcPFvHf+fECAwEAAQ==, and private key: MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAmH7+x0t2VVVwzlXn+P9FvAzxIv75Jzd0yowrWc/vddgqG87rdvE+RbBpjSTBxRTjT5B25zCO55U7Nw8W8d/58QIDAQABAkBCNqIZlsKCut6IOPTIQM7eoB/zuhIk3QdxCvunu4mV+OIv00b6lN02ZsQ64nblu6dP9UuhlyclFaGlXtwqfkABAiEA0XQlb0mT5cZ8VpNNOqojeWoyrvQIRPGhdBrq3VroT4ECIQC6YoVd0yaT6lUDV+tgKtNbQN8m9hVIMgE/awRT/aXicQIhAK+jIbEMlgTcSG+g3eYPveeWciHbaQPHS4g8+i3ciWoBAiBddJsEwaQ9VKlN5N67uJ2DyxJZediP+6rOfr2L08pCsQIhAJLmeidBF0uJxNZiBgnkIHlRQ167qE1D0s5SQ2j5217G and you (the iOS app) was given the public key as a base-64 text. You need to find a way to import this key pair into iOS keychain in order to use it.
  • There is a solution in Objective-C but I have found no solution in pure Swift (at the time I decided to write this small lib).
  • RSA encryption/decryption does not allow you to process a large amount of data in one go. You have to split data into small chunks and encrypt/decrypt one by one and merge into the final result. This lib solves this problem as well.

Functions

  • RSAUtils.encryptWithRSAKey(data: NSData, rsaKeyRef: SecKeyRef, padding: SecPadding) -> NSData? encrypt data with a RSAKey. Currently not working with private key T_T
  • RSAUtils.decryptWithRSAKey(encryptedData: NSData, rsaKeyRef: SecKeyRef, padding: SecPadding) -> NSData? decrypt data with a RSAKey (works with public & private key).
  • RSAUtils.deleteRSAKeyFromKeychain(tagName: String) delete any existing RSA key from keychain
  • RSAUtils.getRSAKeyFromKeychain(tagName: String) -> SecKeyRef? get a SecKeyRef from keychain
  • RSAUtils.addRSAPrivateKey(privkeyBase64: String, tagName: String) -> SecKeyRef? add a RSA private key to keychain and return its SecKeyRef (private key data is in base64 encoding, the data between "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----").
  • RSAUtils.addRSAPublicKey(pubkeyBase64: String, tagName: String) -> SecKeyRef? add a RSA pubic key to keychain and return its SecKeyRef (public key data is in base64 encoding, the data between "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----").
  • RSAUtils.encryptWithRSAPublicKey(data: NSData, pubkeyBase64: String, keychainTag: String) -> NSData? encrypt data with a RSA public key (public key data is in base64 encoding, the data between "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----").
  • RSAUtils.decryptWithRSAPublicKey(encryptedData: NSData, pubkeyBase64: String, keychainTag: String) -> NSData? decrypt an encrypted data with a RSA public key (public key data is in base64 encoding, the data between "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----").
  • RSAUtils.encryptWithRSAPrivateKey(data: NSData, privkeyBase64: String, keychainTag: String) -> NSData? encrypt data with a RSA private key (private key data is in base64 encoding, the data between "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----"). The function is there but currently it is NOT WORKING YET!
  • RSAUtils.decryptWithRSAPrivateKey(encryptedData: NSData, privkeyBase64: String, keychainTag: String) -> NSData? decrypt an encrypted data with a RSA private key (private key data is in base64 encoding, the data between "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----").

History

2015-09-26

  • Code & Function cleanup, works with XCode 7 & Swift 2
  • New functionality: decrypt with private key

2015-07-10

  • Encrypt & Decrypt data with public key.

Licence

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

More Repositories

1

swiftutils

Utility library in Swift
Swift
72
star
2

goadmin.g8

Giter8 template to build Admin Control Panel for Go.
JavaScript
72
star
3

govueadmin.g8

AdminCP Giter8 template for GoLang with VueJS-based frontend
Vue
40
star
4

go-module-template

Template to quickly spin up a Go module project
Go
39
star
5

dotnet-webapi-template

Template to quickly spin up .NET Web API projects
C#
27
star
6

gocosmos

Go database/sql driver for Azure Cosmos DB SQL API
Go
22
star
7

docms

Content Management System where its content is built through CI/CD pipeline
CSS
19
star
8

goyai

Yet Another I18n package for Go
Go
18
star
9

exter

An identity gateway that allows application to authenticate users using various identity sources.
Go
17
star
10

consu

Commons utilities and tools in Golang
Go
14
star
11

codeql-container

GitHub CodeQL packaged and precompiled in a container.
Shell
12
star
12

godal

Generic Database Access Layer implementation in Golang.
Go
11
star
13

azp-k8s-agents

Azure Pipelines agents as containers on Kubernetes cluster
Shell
8
star
14

prista

A log collector service
Go
7
star
15

prom

Utility library to manage shared connection in Golang
Go
7
star
16

goapi.g8

Go template for microservices using Echo framework.
Go
7
star
17

olaf

Twitter Snowflake implemented in Go
Go
6
star
18

godynamo

Go database/sql driver for AWS DynamoDB
Go
4
star
19

id-server

Server to generate IDs
Java
4
star
20

action-semrelease

GitHub Action to publish releases using tags, following sematic versioning
JavaScript
4
star
21

queue-server

Queue service that unifies various queue backends into a single API set.
Java
4
star
22

WebTemplates

Free web template collection
CSS
4
star
23

go-giter8-sample.g8

Sample template to demonstrate https://github.com/btnguyen2k/go-giter8
HTML
3
star
24

txapi

Build your own Transformers API server
Python
2
star
25

npm-package-template

Template to quickly spin up a NPM package project
JavaScript
2
star
26

akka-scheduledjob-seed.g8

Java template - Scheduled Jobs using Akka: use "sbt new btnguyen2k/akka-scheduledjob-seed.g8"
Java
2
star
27

henge

NoSQL-style universal data access layer lib for Go (Golang).
Go
1
star
28

btnguyen2k

Capturing my learning adventure as I explore the world of tech and programming.
1
star
29

mpn

My Personal Notes
Dockerfile
1
star
30

js-mdr

Render Markdown to HTML with rich extensions.
JavaScript
1
star
31

oaiaux

OpenAI helper
Go
1
star
32

llm_fetch_data

Python
1
star
33

js-checksum

Javascript module to calculate checksum from any value.
JavaScript
1
star
34

v-ushop

Automatically exported from code.google.com/p/v-ushop
PHP
1
star
35

plurimos

Many-to-one mapping service
Go
1
star
36

gc-stop-of-the-world

How does GC affect Java application?
Shell
1
star
37

docker-scribe-ubuntu

Dockerfile to build a Facebook Scribe server (based on Ubuntu)
Shell
1
star
38

id-jclient

Java client for id-server (https://github.com/btnguyen2k/id-server)
Java
1
star
39

png

Push Notification Gateway (currently support only iOS)
JavaScript
1
star
40

singu

A message queue library implemented in Golang.
Go
1
star
41

microservices-undertow-seed.g8

Java template for microservices using Undertow framework: use "sbt new btnguyen2k/microservices-undertow-seed.g8"
Java
1
star
42

mpps

Memcached Protocol Proxy Server
Java
1
star