mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Matlab
		
	
| 
											5 years ago
										 | // | ||
|  | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | #import "NSData+keyVersionByte.h" | ||
|  | #import "AxolotlExceptions.h" | ||
|  | #import <SessionProtocolKit/SCKExceptionWrapper.h> | ||
|  | #import <SessionProtocolKit/OWSAsserts.h> | ||
|  | 
 | ||
|  | @implementation NSData (keyVersionByte) | ||
|  | 
 | ||
|  | const Byte DJB_TYPE = 0x05; | ||
|  | 
 | ||
|  | - (instancetype)prependKeyType { | ||
|  |     if (self.length == 32) { | ||
|  |         NSMutableData *data = [NSMutableData dataWithBytes:&DJB_TYPE length:1]; | ||
|  |         [data appendData:self.copy]; | ||
|  |         return data; | ||
|  |     } else { | ||
|  |         OWSLogDebug(@"key length: %lu", (unsigned long)self.length); | ||
|  |     } | ||
|  |     return self; | ||
|  | } | ||
|  | 
 | ||
|  | - (nullable instancetype)removeKeyTypeAndReturnError:(NSError **)outError | ||
|  | { | ||
|  |     @try { | ||
|  |         return self.throws_removeKeyType; | ||
|  |     } @catch (NSException *exception) { | ||
|  |         *outError = SCKExceptionWrapperErrorMake(exception); | ||
|  |         return nil; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | - (instancetype)throws_removeKeyType | ||
|  | { | ||
|  |     if (self.length == 33) { | ||
|  |         if ([[self subdataWithRange:NSMakeRange(0, 1)] isEqualToData:[NSData dataWithBytes:&DJB_TYPE length:1]]) { | ||
|  |             return [self subdataWithRange:NSMakeRange(1, 32)]; | ||
|  |         } else{ | ||
|  |             @throw [NSException exceptionWithName:InvalidKeyException reason:@"Key type is incorrect" userInfo:@{}]; | ||
|  |         } | ||
|  |     } else { | ||
|  |         OWSLogDebug(@"key length: %lu", (unsigned long)self.length); | ||
|  |         return self; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | @end |