Skip to content

Instantly share code, notes, and snippets.

@mdelete
mdelete / publicKeyHexModulus
Created May 23, 2015 21:39
Silly but effective public key modulus extraction in Swift
func publicKeyHexModulus(key: SecKeyRef) -> String {
let s = String(format: "\(key)")
let r1 = s.rangeOfString("modulus: ")!
let r2 = Range<String.Index>(start: advance(r1.startIndex, count(r1)), end: advance(r1.endIndex, (SecKeyGetBlockSize(key) * 2)))
return s.substringWithRange(r2)
}
@mdelete
mdelete / gist:d9dbc320d5de347c2a85
Created May 23, 2015 20:11
Swift iOS SSL public key pinning
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
var localTrust: Unmanaged<SecTrust>?
let serverTrust = challenge.protectionSpace.serverTrust!
let serverPublicKey = SecTrustCopyPublicKey(serverTrust).takeRetainedValue();
let certificateData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("pinning-certificate", ofType: "der")!)
let localCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, certificateData).takeRetainedValue();
let policy = SecPolicyCreateBasicX509().takeRetainedValue()
if SecTrustCreateWithCertificates(localCertificate, policy, &localTrust) == errSecSuccess {
@mdelete
mdelete / Makefile
Created July 12, 2014 20:00
Makefile for libsodium from andrewtj, with the Xcode ranlib arm64 bug fixed
.PHONY: all clean osx ios
export CFLAGS LDFLAGS
OSX_SDK_VERSION = 10.9
OSX_MIN_VERSION = 10.9
OSX_ARCH = MacOSX-x86_64
OSX_TARGET = $(patsubst %,build/%/lib/libsodium.a,$(OSX_ARCH))
IOS_SDK_VERSION = 7.1
IOS_MIN_VERSION = 7.1
IOS_ARCH = iPhoneSimulator-i386 iPhoneSimulator-x86_64 iPhoneOS-armv7 iPhoneOS-armv7s iPhoneOS-arm64