I want to verify a data on iOS 7. Now I have a function:
-(BOOL) PKCSVerifyBytesSHA256withRSA:(NSData*) plainData
withSign:(NSData*) signature
withKey:(SecKeyRef) public_Key{
size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);
const void* signedHashBytes = [signature bytes];
size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
uint8_t* hashBytes = malloc(hashBytesSize);
if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)){
return nil;
}
OSStatus status = SecKeyRawVerify(publicKey,
kSecPaddingPKCS1SHA256,
hashBytes,
hashBytesSize,
signedHashBytes,
signedHashBytesSize);
return status == errSecSuccess;
}
and a key:
NSString *public_key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCT2eIq8i/uAHzvwOWhFU9AuRHjPnGq8wD461ZH7N4/LjoRwPaPF3meyyENJgtDr4EyhV9KN77/3VT+87ZpT6QH9w6Q5XwDmM3jhU6bUhWyIPLzrd5XE2rQKRIMXixflz/8Q327VHKsLoKu44HuCh6XrB+uMWUjwVLCLOi2U0sYdQIDAQAB";
I was suggest a link here
so I add the - (SecKeyRef)getPublicKeyRef
and PKCSVerifyBytesSHA256withRSA
to a new class and call
RSA1 *rsa1 =[[RSA1 alloc]init]; if([rsa1 PKCSVerifyBytesSHA256withRSA:Ddata withSign:Dsig withKey:[rsa1 getPublicKeyRef]]){ NSLog(@"True");} else{ NSLog(@"nil");}
but I got an error at the line
size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);in the
PKCSVerifyBytesSHA256withRSA
function with the note: Thread 1: EXC_BAD_ACCESS(code=1, address=0x10)
Is there any way to solve this problems?