swift3 - CFDictionaryCreate crashes in Xcode 8 swift 3 -
i have following code retrieving , storing rsakeydata
fileprivate func retrieveandstorersakeydata(ispublic: bool) throws -> cfdata { var resultcode = noerr var result: cfdata let value = { if ispublic { let t = self.publictag print(t) } else { let s = self.privatetag print(s) } } var keycallbacks = kcftypedictionarykeycallbacks var valuecallbacks = kcftypedictionaryvaluecallbacks let keys = [unmanaged.passunretained(ksecclass).toopaque(), unmanaged.passunretained(ksecattrapplicationtag).toopaque(), unmanaged.passunretained(ksecattrkeytype).toopaque(), unmanaged.passunretained(ksecreturndata).toopaque()] let values = [unmanaged.passunretained(ksecclasskey).toopaque(), unmanaged<anyobject>.passunretained(value anyobject).toopaque(), unmanaged.passunretained(ksecattrkeytypersa).toopaque(), unmanaged.passunretained(kcfbooleantrue).toopaque()] let querykey = cfdictionarycreate(kcfallocatordefault,unsafemutablepointer.allocate(capacity: keys.count),unsafemutablepointer.allocate(capacity: values.count), 4, &keycallbacks, &valuecallbacks) // key. var item: anyobject? resultcode = secitemcopymatching(querykey!, &item) if(resultcode != noerr) { try generatekeypair() /* recurcively call retrieval again after keys have been generated */ result = try retrieveandstorersakeydata(ispublic: ispublic) } else { result = item as! cfdata } return result }
the cfdictionarycreate fails , code crashes in line in
let querykey = cfdictionarycreate(kcfallocatordefault,unsafemutablepointer.allocate(capacity: keys.count),unsafemutablepointer.allocate(capacity: values.count), 4, &keycallbacks, &valuecallbacks)
can me out on this. in advance
to generate key pair
func generatekeypair(_ publictag: string, privatetag: string, keysize: int)->bool { let privateattributes = [string(ksecattrispermanent): true, string(ksecattrapplicationtag): privatetag] [string : any] let publicattributes = [string(ksecattrispermanent): true, string(ksecattrapplicationtag): publictag] [string : any] let pairattributes = [string(ksecattrkeytype): ksecattrkeytypersa, string(ksecattrkeysizeinbits): keysize, string(ksecpublickeyattrs): publicattributes, string(ksecprivatekeyattrs): privateattributes] [string : any] var publicref: seckey? var privateref: seckey? switch seckeygeneratepair(pairattributes cfdictionary, &publicref, &privateref) { case noerr: return true default: return false } }
and few helper functions
func obtainkey(_ tag: string) -> seckey? { var keyref: anyobject? let query: dictionary<string, anyobject> = [ string(ksecattrkeytype): ksecattrkeytypersa, string(ksecreturnref): kcfbooleantrue cfboolean, string(ksecclass): ksecclasskey cfstring, string(ksecattrapplicationtag): tag cfstring, ] let status = secitemcopymatching(query cfdictionary, &keyref) switch status { case noerr: if let ref = keyref { return (ref as! seckey) } default: break } return nil } func obtainkeydata(_ tag: string) -> data? { var keyref: anyobject? let query: dictionary<string, anyobject> = [ string(ksecattrkeytype): ksecattrkeytypersa, string(ksecreturndata): kcfbooleantrue cfboolean, string(ksecclass): ksecclasskey cfstring, string(ksecattrapplicationtag): tag cfstring, ] let result: data? switch secitemcopymatching(query cfdictionary, &keyref) { case noerr: result = keyref as? data default: result = nil } return result } func insertpublickey(_ publictag: string, data: data) -> seckey? { let query: dictionary<string, anyobject> = [ string(ksecattrkeytype): ksecattrkeytypersa, string(ksecclass): ksecclasskey cfstring, string(ksecattrapplicationtag): publictag cfstring, string(ksecvaluedata): data cfdata, string(ksecreturnpersistentref): true cfboolean] var persistentref: anyobject? let status = secitemadd(query cfdictionary, &persistentref) if status != noerr && status != errsecduplicateitem { return nil } return obtainkey(publictag) } func deletekey(_ tag: string) -> bool { let query: dictionary<string, anyobject> = [ string(ksecattrkeytype): ksecattrkeytypersa, string(ksecclass): ksecclasskey cfstring, string(ksecattrapplicationtag): tag cfstring] return secitemdelete(query cfdictionary) == noerr } func updatekey(_ tag: string, data: data) -> bool { let query: dictionary<string, anyobject> = [ string(ksecattrkeytype): ksecattrkeytypersa, string(ksecclass): ksecclasskey cfstring, string(ksecattrapplicationtag): tag cfstring] return secitemupdate(query cfdictionary, [string(ksecvaluedata): data] cfdictionary) == noerr }
Comments
Post a Comment