This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myFormatter = DateFormatter() | |
myFormatter.timeZone = TimeZone(identifier: "Asia/Hong_Kong")! | |
myFormatter.dateFormat = "Y-MM-dd HH:mm:ss Z" | |
if let theDay = myFormatter.date(from: "2017-09-26 16:20:00 -0700"){ | |
// It is tricky if u print the date(it will use +0000) | |
print("theDay: \(theDay)") | |
let hkCalendar = Calendar.current | |
let dateCompoents = hkCalendar.dateComponents([.year, .month, .day, .hour, .minute, .timeZone, .calendar,.weekday], from: theDay) | |
print("hour: \(dateCompoents.hour!)") //7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (SecKeyRef)getPrivateKeyRefrenceFromData:(NSData*)p12Data password:(NSString*)password { | |
SecKeyRef privateKeyRef = NULL; | |
NSMutableDictionary * options = [[NSMutableDictionary alloc] init]; | |
[options setObject: password forKey:(__bridge id)kSecImportExportPassphrase]; | |
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); | |
OSStatus securityError = SecPKCS12Import((__bridge CFDataRef) p12Data, (__bridge CFDictionaryRef)options, &items); | |
if (securityError == noErr && CFArrayGetCount(items) > 0) { | |
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0); | |
SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (SecKeyRef)getPublicKeyRefrenceFromeData: (NSData*)derData { | |
SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)derData); | |
SecPolicyRef myPolicy = SecPolicyCreateBasicX509(); | |
SecTrustRef myTrust; | |
OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust); | |
SecTrustResultType trustResult; | |
if (status == noErr) { | |
status = SecTrustEvaluate(myTrust, &trustResult); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (SecKeyRef)getPublicKeyFromCertificate:(NSData *)data | |
{ | |
//create certificate. | |
CFDataRef dataRef=CFDataCreate(kCFAllocatorDefault, [data bytes], (CFIndex)[data length]); | |
SecCertificateRef certiRef=SecCertificateCreateWithData(kCFAllocatorDefault, dataRef); | |
(CFRelease(dataRef)); | |
//evaluate certificate. | |
CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **) &certiRef, 1, NULL); | |
SecPolicyRef policy = SecPolicyCreateBasicX509(); | |
SecTrustRef trust; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)ecdsaAction:(id)sender { | |
NSData *privateKeyPEMData = [[NSFileManager defaultManager] contentsAtPath: [[NSBundle mainBundle] pathForResource:@"ec_private_key" ofType:@"p12" ]]; | |
if(privateKeyPEMData){ | |
NSLog(@"privateKeyPEMData NOT NULL"); | |
SecKeyRef privateKey = [self getPrivateKeyRefrenceFromData:privateKeyPEMData password:@"12345678"]; | |
if(privateKey){ | |
NSLog(@"private key is NOT NULL!!"); |