Decode base64URL to base64 — Swift

“base64url” differs from the standard Base64 encoding in two aspects:

  • different characters are used for index 62 and 63 (- and _ instead
    of + and /)
  • no mandatory padding with = characters to make the string length
    a multiple of four.

(compare https://en.wikipedia.org/wiki/Base64#Variants_summary_table).

Here is a possible conversion function:

func base64urlToBase64(base64url: String) -> String {
    var base64 = base64url
        .replacingOccurrences(of: "-", with: "+")
        .replacingOccurrences(of: "_", with: "https://stackoverflow.com/")
    if base64.characters.count % 4 != 0 {
        base64.append(String(repeating: "=", count: 4 - base64.characters.count % 4))
    }
    return base64
}

Example:

let base64url = "hJQWHABDBjoPHorYF5xghQ"
let base64 = base64urlToBase64(base64url: base64url)
print(base64) // hJQWHABDBjoPHorYF5xghQ==

if let data = Data(base64Encoded: base64) {
    print(data as NSData) // <8494161c 0043063a 0f1e8ad8 179c6085>
}

For the sake of completeness, this would be the opposite conversion:

func base64ToBase64url(base64: String) -> String {
    let base64url = base64
        .replacingOccurrences(of: "+", with: "-")
        .replacingOccurrences(of: "https://stackoverflow.com/", with: "_")
        .replacingOccurrences(of: "=", with: "")
    return base64url
}

Update for Swift 4:

func base64urlToBase64(base64url: String) -> String {
    var base64 = base64url
        .replacingOccurrences(of: "-", with: "+")
        .replacingOccurrences(of: "_", with: "https://stackoverflow.com/")
    if base64.count % 4 != 0 {
        base64.append(String(repeating: "=", count: 4 - base64.count % 4))
    }
    return base64
}

Leave a Comment