SecurityExtensions

SecurityExtensions for Swift

This framework intends to make it easier to use some of Apple’s Security framework APIs from Swift. I tested this on iOS. It may also work on OS X, didn’t look into that. You’re welcome to create pull requests :)

This framework requires Swift version 3.

Overview

This framework provides the following primitives. There are unit tests; have a look at them for examples.

privateKey

SecKey Extensions

Please note that encrypting only works if the key is a public key, and decrypting only works if the key is a private key. This is not verified by this framework, your application logic has to make sure it is done correctly.

encrypt()

Encrypt data or a string using the key. Make sure that the key is a public key, not a private key! Encryption uses PKCS1 padding.

loadFromKeychain(tag:)

SequenceType

This framework also provides an extension to SequenceType that allows creating a hexadecimal string representation of a byte array.

hexString()

Creates a string representation of a byte array ([UInt8]) by concatenating the hexadecimal representation of all bytes. The string does not include the prefix ‘0x’ that is commonly used to indicate hexadecimal representations. This is the inverse function of String.hexByteArray().

String

The inverse function of hexString is implemented in an extension of String.

hexByteArray()

Converts a string containing the hexadecimal representation of a byte to a byte array. The string must not contain anything else. It may optionally start with the prefix ‘0x’. Conversion is case insensitive. This is the inverse function of [UInt8].hexString().

Usage

The easiest way to use this library is using CocoaPods. Just put something like this in your Podfile:

pod 'SecurityExtensions', '~> 1.0'

License

The MIT License (MIT)
Copyright (c) 2016 Stefan van den Oord

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.