Skip to content

Instantly share code, notes, and snippets.

@trickart
Created September 21, 2025 00:57
Show Gist options
  • Select an option

  • Save trickart/c5b7d6934f33302b8f9d0aacd5d61a25 to your computer and use it in GitHub Desktop.

Select an option

Save trickart/c5b7d6934f33302b8f9d0aacd5d61a25 to your computer and use it in GitHub Desktop.
Base64urlのSwiftコード
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