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
		
	
	
	
		
			Objective-C
		
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Objective-C
		
	
| //
 | |
| //  Copyright (c) 2018 Open Whisper Systems. All rights reserved.
 | |
| //
 | |
| 
 | |
| #import "NSData+keyVersionByte.h"
 | |
| #import "AxolotlExceptions.h"
 | |
| #import <SignalCoreKit/SCKExceptionWrapper.h>
 | |
| #import <SignalCoreKit/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
 |