2017-04-19 178 views
3

我還沒有找到正確的方法如何解碼base64URL到base64格式在迅速。解碼base64URL到base64 - 斯威夫特

根據base64url to base64hJQWHABDBjoPHorYF5xghQ(base64URL)應該是hJQWHABDBjoPHorYF5xghQ==(base64)。這可能會有更多的差異。

在stackoverflow上沒有解決方案。

+0

「相關」部分向右下,有一個鏈接關於通過JavaScript的Base64URL解碼。快速瀏覽一下會給你一個不同的描述。 – rmaddy

回答

7

「base64url」 從標準Base64編碼的不同在兩個方面:

  • 不同的字符被用於索引62和63(-_代替+/
  • 沒有強制性的填充與=個字符使字符串長度爲 爲四的倍數。

(比較https://en.wikipedia.org/wiki/Base64#Variants_summary_table)。

這裏是一個可能的轉換函數:

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

實施例:

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> 
} 

爲了完整性的緣故,這將是相反的轉換:

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

更新斯威夫特4:

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

Mandrill自定義跟蹤域,跟蹤參數p也可以這樣解碼。爲像我這樣的人提供:) – ergunkocak

0

以下是對馬丁發佈了雨燕4延伸內的清理版本:

import Foundation 

/// Extension for making base64 representations of `Data` safe for 
/// transmitting via URL query parameters 
extension Data { 

    /// Instantiates data by decoding a base64url string into base64 
    /// 
    /// - Parameter string: A base64url encoded string 
    init?(base64URLEncoded string: String) { 
     self.init(base64Encoded: string.toggleBase64URLSafe(on: false)) 
    } 

    /// Encodes the string into a base64url safe representation 
    /// 
    /// - Returns: A string that is base64 encoded but made safe for passing 
    ///   in as a query parameter into a URL string 
    func base64URLEncodedString() -> String { 
     return self.base64EncodedString().toggleBase64URLSafe(on: true) 
    } 

} 

extension String { 

    /// Encodes or decodes into a base64url safe representation 
    /// 
    /// - Parameter on: Whether or not the string should be made safe for URL strings 
    /// - Returns: if `on`, then a base64url string; if `off` then a base64 string 
    func toggleBase64URLSafe(on: Bool) -> String { 
     if on { 
      // Make base64 string safe for passing into URL query params 
      let base64url = self.replacingOccurrences(of: "/", with: "_") 
       .replacingOccurrences(of: "+", with: "-") 
       .replacingOccurrences(of: "=", with: "") 
      return base64url 
     } else { 
      // Return to base64 encoding 
      var base64 = self.replacingOccurrences(of: "_", with: "/") 
       .replacingOccurrences(of: "-", with: "+") 
      // Add any necessary padding with `=` 
      if base64.count % 4 != 0 { 
       base64.append(String(repeating: "=", count: 4 - base64.count % 4)) 
      } 
      return base64 
     } 
    } 

}