0001 // 0002 // String+BASE64.swift 0003 // Swifter 0004 // 0005 // Copyright © 2016 Damian Kołakowski. All rights reserved. 0006 // 0007 0008 #if os(Linux) 0009 import Glibc 0010 #else 0011 import Foundation 0012 #endif 0013 0014 0015 extension String { 0016 0017 private static let CODES= [UInt8]("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".utf8) 0018 0019 public static func toBase64
String+BASE64.swift:28 result.append(CODES[Int(tmp)])String+BASE64.swift:32 result.append(CODES[Int(tmp)]);String+BASE64.swift:36 result.append(CODES[Int(tmp)]);String+BASE64.swift:38 result.append(CODES[Int(tmp)]);String+BASE64.swift:40 result.append(CODES[Int(tmp)]);String+BASE64.swift:44 result.append(CODES[Int(tmp)]);(data: [UInt8]) -> String { 0020 0021 // Based on: https://en.wikipedia.org/wiki/Base64#Sample_Implementation_in_Java 0022 0023 var result = [UInt8]() 0024 var tmp: UInt8 0025 for index in 0.stride(to: data.count, by: 3) { 0026 let byte = data[index] 0027 tmp = (byte & 0xFC) >> 2; 0028 result.append(CODES[Int(tmp)]) 0029 tmp = (byte & 0x03) << 4; 0030 if index + 1 < data.count { 0031 tmp |= (data[index + 1] & 0xF0) >> 4; 0032 result.append(CODES[Int(tmp)]); 0033 tmp = (data[index + 1] & 0x0F) << 2; 0034 if (index + 2 < data.count) { 0035 tmp |= (data[index + 2] & 0xC0) >> 6; 0036 result.append(CODES[Int(tmp)]); 0037 tmp = data[index + 2] & 0x3F; 0038 result.append(CODES[Int(tmp)]); 0039 } else { 0040 result.append(CODES[Int(tmp)]); 0041 result.appendContentsOf([UInt8]("=".utf8)); 0042 } 0043 } else { 0044 result.append(CODES[Int(tmp)]); 0045 result.appendContentsOf([UInt8]("==".utf8)); 0046 } 0047 } 0048 return String.fromUInt8(result) 0049 } 0050 } 0051
HttpHandlers+WebSockets.swift:41 let secWebSocketAccept = String.toBase64((secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").SHA1())