OLMKit: Add [OLMUtility sha256:]
This commit is contained in:
parent
4a2aac5800
commit
a9be04fa4b
2 changed files with 28 additions and 1 deletions
|
@ -10,6 +10,14 @@
|
||||||
|
|
||||||
@interface OLMUtility : NSObject
|
@interface OLMUtility : NSObject
|
||||||
|
|
||||||
|
/**
|
||||||
|
Calculate the SHA-256 hash of the input and encodes it as base64.
|
||||||
|
|
||||||
|
@param message the message to hash.
|
||||||
|
@return the base64-encoded hash value.
|
||||||
|
*/
|
||||||
|
- (NSString*)sha256:(NSData*)message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Verify an ed25519 signature.
|
Verify an ed25519 signature.
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,25 @@
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (NSString *)sha256:(NSData *)message {
|
||||||
|
size_t length = olm_sha256_length(_utility);
|
||||||
|
|
||||||
|
NSMutableData *shaData = [NSMutableData dataWithLength:length];
|
||||||
|
if (!shaData) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t result = olm_sha256(_utility, message.bytes, message.length, shaData.mutableBytes, shaData.length);
|
||||||
|
if (result == olm_error()) {
|
||||||
|
const char *error = olm_utility_last_error(_utility);
|
||||||
|
NSAssert(NO, @"olm_sha256 error: %s", error);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *sha = [[NSString alloc] initWithData:shaData encoding:NSUTF8StringEncoding];
|
||||||
|
return sha;
|
||||||
|
}
|
||||||
|
|
||||||
- (BOOL)verifyEd25519Signature:(NSString*)signature key:(NSString*)key message:(NSData*)message error:(NSError**)error {
|
- (BOOL)verifyEd25519Signature:(NSString*)signature key:(NSString*)key message:(NSData*)message error:(NSError**)error {
|
||||||
|
|
||||||
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
|
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
|
@ -61,7 +80,7 @@
|
||||||
signatureData.bytes, signatureData.length
|
signatureData.bytes, signatureData.length
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result < 0 || result == (size_t)-1) {
|
if (result == olm_error()) {
|
||||||
if (error) {
|
if (error) {
|
||||||
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: [NSString stringWithUTF8String:olm_utility_last_error(_utility)]};
|
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: [NSString stringWithUTF8String:olm_utility_last_error(_utility)]};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue