|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +// |
| 15 | +// This source file is part of the Vapor open source project |
| 16 | +// |
| 17 | +// Copyright (c) 2017-2020 Vapor project authors |
| 18 | +// Licensed under MIT |
| 19 | +// |
| 20 | +// See LICENSE for license information |
| 21 | +// |
| 22 | +// SPDX-License-Identifier: MIT |
| 23 | +// |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +import Foundation |
| 27 | + |
| 28 | +// Source: https://github.com/vapor/jwt-kit/blob/master/Sources/JWTKit/Utilities/Base64URL.swift |
| 29 | + |
| 30 | +extension DataProtocol { |
| 31 | + func base64URLDecodedBytes() -> Data? { |
| 32 | + var data = Data(self) |
| 33 | + data.base64URLUnescape() |
| 34 | + return Data(base64Encoded: data) |
| 35 | + } |
| 36 | + |
| 37 | + func base64URLEncodedBytes() -> Data { |
| 38 | + var data = Data(self).base64EncodedData() |
| 39 | + data.base64URLEscape() |
| 40 | + return data |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension Data { |
| 45 | + /// Converts base64-url encoded data to a base64 encoded data. |
| 46 | + /// |
| 47 | + /// https://tools.ietf.org/html/rfc4648#page-7 |
| 48 | + mutating func base64URLUnescape() { |
| 49 | + for i in 0 ..< self.count { |
| 50 | + switch self[i] { |
| 51 | + case 0x2D: self[self.index(self.startIndex, offsetBy: i)] = 0x2B |
| 52 | + case 0x5F: self[self.index(self.startIndex, offsetBy: i)] = 0x2F |
| 53 | + default: break |
| 54 | + } |
| 55 | + } |
| 56 | + /// https://stackoverflow.com/questions/43499651/decode-base64url-to-base64-swift |
| 57 | + let padding = count % 4 |
| 58 | + if padding > 0 { |
| 59 | + self += Data(repeating: 0x3D, count: 4 - padding) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Converts base64 encoded data to a base64-url encoded data. |
| 64 | + /// |
| 65 | + /// https://tools.ietf.org/html/rfc4648#page-7 |
| 66 | + mutating func base64URLEscape() { |
| 67 | + for i in 0 ..< self.count { |
| 68 | + switch self[i] { |
| 69 | + case 0x2B: self[self.index(self.startIndex, offsetBy: i)] = 0x2D |
| 70 | + case 0x2F: self[self.index(self.startIndex, offsetBy: i)] = 0x5F |
| 71 | + default: break |
| 72 | + } |
| 73 | + } |
| 74 | + self = split(separator: 0x3D).first ?? .init() |
| 75 | + } |
| 76 | +} |
0 commit comments