繁体   English   中英

将 base64URL 解码为 base64 -- Swift

[英]Decode base64URL to base64 -- Swift

我还没有找到如何在 swift 中将 base64URL 解码为 base64 格式的正确方法。

根据base64url到base64 hJQWHABDBjoPHorYF5xghQ (base64URL)应该是hJQWHABDBjoPHorYF5xghQ== (base64)。 这里可能会有更多差异。

stackoverflow 上没有解决方案。

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

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

这是Martin在Swift 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
        }
    }

}

这是目标 c 版本的 Base64URL 到 Base64

-(NSString *) base64urlToBase64:(NSString *)base64url {

 NSString * base64 = [base64url stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
base64 =  [base64 stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
if(base64.length  % 4 != 0)
{
    base64 = [base64 stringByAppendingString:[self stringWithRepeatString:"=" times:(4 - base64.length % 4)]];
}
return  base64;

}

-(NSString*)stringWithRepeatString:(char*)characters times:(unsigned int)repetitions; {

unsigned long stringLength = strlen(characters);
unsigned long repeatStringLength = stringLength * repetitions + 1;
char repeatString[repeatStringLength];

for (unsigned int i = 0; i < repetitions; i++) {
    unsigned int pointerPosition = i * repetitions;
    memcpy(repeatString + pointerPosition, characters, stringLength);
}

repeatString[repeatStringLength - 1] = 0;
return  [NSString stringWithCString:repeatString encoding:NSASCIIStringEncoding];

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM