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.
		
		
		
		
		
			
		
			
	
	
		
			83 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			83 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
| 
								 
											5 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 | 
							
								//  Copyright (c) 2019 Open Whisper Systems. All rights reserved.
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import Foundation
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								public extension String {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    var digitsOnly: String {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        return (self as NSString).digitsOnly()
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    func rtlSafeAppend(_ string: String) -> String {
							 | 
						||
| 
								 | 
							
								        return (self as NSString).rtlSafeAppend(string)
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    func substring(from index: Int) -> String {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        return String(self[self.index(self.startIndex, offsetBy: index)...])
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    func substring(to index: Int) -> String {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        return String(prefix(index))
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    enum StringError: Error {
							 | 
						||
| 
								 | 
							
								        case invalidCharacterShift
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// MARK: - Selector Encoding
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								private let selectorOffset: UInt32 = 17
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								public extension String {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    func caesar(shift: UInt32) throws -> String {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        let shiftedScalars: [UnicodeScalar] = try unicodeScalars.map { c in
							 | 
						||
| 
								 | 
							
								            guard let shiftedScalar = UnicodeScalar((c.value + shift) % 127) else {
							 | 
						||
| 
								 | 
							
								                throw StringError.invalidCharacterShift
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            return shiftedScalar
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return String(String.UnicodeScalarView(shiftedScalars))
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    var encodedForSelector: String? {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        guard let shifted = try? self.caesar(shift: selectorOffset) else {
							 | 
						||
| 
								 | 
							
								            return nil
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        guard let data = shifted.data(using: .utf8) else {
							 | 
						||
| 
								 | 
							
								            return nil
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return data.base64EncodedString()
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    var decodedForSelector: String? {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        guard let data = Data(base64Encoded: self) else {
							 | 
						||
| 
								 | 
							
								            return nil
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        guard let shifted = String(data: data, encoding: .utf8) else {
							 | 
						||
| 
								 | 
							
								            return nil
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return try? shifted.caesar(shift: 127 - selectorOffset)
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								public extension NSString {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    var encodedForSelector: String? {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        return (self as String).encodedForSelector
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    var decodedForSelector: String? {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								        return (self as String).decodedForSelector
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |