Skip to content

Instantly share code, notes, and snippets.

@IamSaurav
IamSaurav / SSLPinning.swift
Created April 26, 2020 17:31
This function is where we need to verify Server by extracting SSL certificate of server/api and validating against pinned ssl certificate.
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust else {return}
guard let serverTrustInfo = challenge.protectionSpace.serverTrust,
let certificate = serverSSLCertificate(serverTrustInfo) else {return}
// Either of the below methods "Public key pinning" or "Certificate pinning" can be done
// guard let publicKeySha256 = sha256(certificate) else {return}
// if pinnedPublicKeyHash == sha256(serverSSlPublicKey) {
// completionHandler(.useCredential, URLCredential.init(trust: serverTrustInfo))
// }else{
@IamSaurav
IamSaurav / SSLPinning.swift
Created April 26, 2020 17:15
Load the pinned SSL certificate in SecCertificate fomat.
func pinnedCertificate() -> SecCertificate? {
guard let certPath = Bundle.main.path(forResource: "bitmountn", ofType: "crt") else { return .none }
guard let fileData = try? Data(contentsOf: URL(fileURLWithPath: certPath)) else {return .none}
let certificate = SecCertificateCreateWithData(.none, fileData as CFData)
return certificate
}
@IamSaurav
IamSaurav / SSLPinning.swift
Created April 26, 2020 17:00
Create sha256 hash from different formats like Data, String, SecCertificate.
func sha256(_ certificate: SecCertificate) -> String? {
let certData = SecCertificateCopyData(certificate) as Data
let certStr = certData.base64EncodedString()
return sha256(certStr)
}
func sha256(_ data: Data) -> Data? {
guard let dataBuffer = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }
CC_SHA256((data as NSData).bytes, CC_LONG(data.count), dataBuffer.mutableBytes.assumingMemoryBound(to: UInt8.self))
return dataBuffer as Data
}
@IamSaurav
IamSaurav / SSLPinning.swift
Created April 26, 2020 16:55
Extracts public key from SSL certificate.
func publicKey(_ certificate: SecCertificate) -> String? {
// Got public key in SecKey format, for equality check we need in Data/String format.
// Because our pinned public key hash is in String format.
let serverSSLPublicKey: SecKey? = SecCertificateCopyKey(certificate)
var publicKeyError: UnsafeMutablePointer<Unmanaged<CFError>?>?
guard let serverSSLPublicKeyData = SecKeyCopyExternalRepresentation(serverSSLPublicKey!, publicKeyError ) as Data?, publicKeyError == .none else {return .none}
// This is the final public key string.
let serverSSLPublicKeyString = serverSSLPublicKeyData.base64EncodedString()
return serverSSLPublicKeyString
}
func test() {
let response = self.verifyMyNumber(num: 5)
switch response {
case .success(let number):
print(number)
break
case .failure(let error) :
print(error.localizedDescription)
break
import Foundation
class Customer {
weak var creditCard: CreditCard?
func buyCreditCard(creditCard: CreditCard) {
self.creditCard = creditCard
}
deinit {
print("Customer is getting deallocated")
}
}
//Flat Map
let nonFlatArr = [[1],[2,2],[3,3,3],[4,4,4,4]]
let flatMapped = nonFlatArr.flatMap { $0 }
print("flatMapped \(flatMapped)")
//OUTPUT
flatMapped [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
@IamSaurav
IamSaurav / CicularShadowView.swift
Created September 6, 2019 07:06
Circular View with Shadow in iOS
func circularViewWithShadow() {
let view = UIView()
view.frame = CGRect.init(x: 10, y: 100, width: 100, height: 100)
view.backgroundColor = UIColor.blue
view.layer.cornerRadius = view.bounds.height/2
view.layer.shadowPath = UIBezierPath.init(roundedRect: view.bounds, cornerRadius: view.layer.cornerRadius).cgPath
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = CGSize.init(width: 0, height: 10)
view.layer.masksToBounds = true
func registerPushNotification() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
if settings.authorizationStatus != .authorized {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound,.badge]) { success, error in
print("Permission granted: \(success)")
}
}else{
UIApplication.shared.registerForRemoteNotifications()
}
}
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface BlocksEx : NSObject
typedef void (^CountBlock)(int);
typedef int (^CompletionBlock)(void);
-(void)sampleBlock: (void(^)(void)) block;
-(void)countBlock: (CountBlock) block;
-(void)completionBlock: (CompletionBlock) block;
-(void)multiParamBlock: (void(^)(int, float)) block;
@end