Skip to content

Commit

Permalink
SHA512 signing support
Browse files Browse the repository at this point in the history
- (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error
implemented
  • Loading branch information
“bill.chan” committed Apr 13, 2015
1 parent e5ec04f commit 89590ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions MIHCrypto/Core/MIHPrivateKey.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@
*/
- (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error;

- (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error;

@end
31 changes: 30 additions & 1 deletion MIHCrypto/RSA/MIHRSAPrivateKey.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ - (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error
if (error) *error = [NSError errorFromOpenSSL];
return nil;
}

NSMutableData *signature = [NSMutableData dataWithLength:(NSUInteger) RSA_size(_rsa)];
unsigned int signatureLength = 0;
if (RSA_sign(NID_sha256, messageDigest, SHA256_DIGEST_LENGTH, signature.mutableBytes, &signatureLength, _rsa) == 0) {
Expand All @@ -166,7 +166,36 @@ - (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error
return nil;
}
[signature setLength:(NSUInteger) signatureLength];

return signature;
}

- (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error
{
SHA512_CTX sha512Ctx;
unsigned char messageDigest[SHA512_DIGEST_LENGTH];
if (!SHA512_Init(&sha512Ctx)) {
if (error) *error = [NSError errorFromOpenSSL];
return nil;
}
if (!SHA512_Update(&sha512Ctx, message.bytes, message.length)) {
if (error) *error = [NSError errorFromOpenSSL];
return nil;
}
if (!SHA512_Final(messageDigest, &sha512Ctx)) {
if (error) *error = [NSError errorFromOpenSSL];
return nil;
}

NSMutableData *signature = [NSMutableData dataWithLength:(NSUInteger) RSA_size(_rsa)];
unsigned int signatureLength = 0;
if (RSA_sign(NID_sha512, messageDigest, SHA512_DIGEST_LENGTH, signature.mutableBytes, &signatureLength, _rsa) == 0) {
if (error)
*error = [NSError errorFromOpenSSL];
return nil;
}
[signature setLength:(NSUInteger) signatureLength];

return signature;
}

Expand Down

0 comments on commit 89590ee

Please sign in to comment.