Created
September 21, 2025 00:57
-
-
Save trickart/c5b7d6934f33302b8f9d0aacd5d61a25 to your computer and use it in GitHub Desktop.
Base64urlのSwiftコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Data { | |
| init?(base64URLEncoded input: String) { | |
| let replaced = input | |
| .replacingOccurrences(of: "-", with: "+") | |
| .replacingOccurrences(of: "_", with: "/") | |
| let padded = replaced + String(repeating: "=", count: (4 - (replaced.count % 4))) | |
| guard let data = Data(base64Encoded: padded) else { return nil } | |
| self = data | |
| } | |
| func base64URLEncodedString(options: Data.Base64EncodingOptions = []) -> String { | |
| base64EncodedString(options: options) | |
| .replacingOccurrences(of: "+", with: "-") | |
| .replacingOccurrences(of: "/", with: "_") | |
| .replacingOccurrences(of: "=", with: "") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment