base64编码产生不同的结果

base64编码产生不同的结果

本文介绍了Swift 2 base64编码产生不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

雨燕1.2

let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil);

雨燕2

let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions());

这两种方法产生不同的结果.在Swift 2中,我们无法再传入nil;

apiLoginData.base64EncodedStringWithOptions(nil);

我也尝试在Swift 2中传递NSDataBase64EncodingOptions.Encoding64CharacterLineLength,但是结果仍然不同

有什么想法吗?

更新2:Swift 1.2或Swift 2都不是问题.奇怪的是,这2打印出了不同的结果.

更新3:结果就像马丁指出的那样,我的API_USERNAME变量中有一些不可见的字符

class RequestHelper: NSObject {
static var API_USERNAME: NSString = "⁠⁠⁠abc";
static var API_PASSWORD: NSString = "123";
static var USERNAME: NSString = "abc"
static var PASSWORD: NSString = "123"


class func signUpUser() {
    //Base64
    var loginString = NSString(format: "%@:%@", USERNAME, PASSWORD);
    var loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!;
    var base64LoginString = loginData.base64EncodedStringWithOptions([]);

    print("BASE 64 \(base64LoginString)");//RESULT YWJjOjEyMw==

    let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
    let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
    var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([]);

    print("BASE 64 \(base64ApiLoginString)");// RESULT IS 4oGg4oGg4oGgYWJjOjEyMw==
}
解决方案

类似于 Swift 2.0日历组件错误 Swift 2.0-二进制运算符"|"不能应用于两个UIUserNotificationType操作数
NSDataBase64EncodingOptions是Swift 2中的OptionSetType.它提供了一个类似于set的接口,可以从数组中进行初始化文字.

因此有效的选项例如:

base64EncodedStringWithOptions([]) // no options
base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
base64EncodedStringWithOptions([.Encoding64CharacterLineLength, .EncodingEndLineWithCarriageReturn])


完整示例:

let apiLoginString = NSString(format: "%@:%@", "user", "password")
let apiLoginData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([])

print(base64ApiLoginString)
// dXNlcjpwYXNzd29yZA==

Swift 1.2

let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil);

Swift 2

let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions());

Those 2 approach yield different results. In Swift 2 we cannot pass in nil anymore;

apiLoginData.base64EncodedStringWithOptions(nil);

I have also tried passed in NSDataBase64EncodingOptions.Encoding64CharacterLineLength in Swift 2. But the results is still differemt

Any thoughts?

UPDATE 2: It is not the problem with Swift 1.2 or Swift 2. Oddly enough those 2 prints out different results.. What the F!!

UPDATE 3: Turns out that as Martin pointed out, I have some invisible character in my API_USERNAME variable

class RequestHelper: NSObject {
static var API_USERNAME: NSString = "⁠⁠⁠abc";
static var API_PASSWORD: NSString = "123";
static var USERNAME: NSString = "abc"
static var PASSWORD: NSString = "123"


class func signUpUser() {
    //Base64
    var loginString = NSString(format: "%@:%@", USERNAME, PASSWORD);
    var loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!;
    var base64LoginString = loginData.base64EncodedStringWithOptions([]);

    print("BASE 64 \(base64LoginString)");//RESULT YWJjOjEyMw==

    let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
    let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
    var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([]);

    print("BASE 64 \(base64ApiLoginString)");// RESULT IS 4oGg4oGg4oGgYWJjOjEyMw==
}
解决方案

Similarly as in Swift 2.0 calendar components error or Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands,
NSDataBase64EncodingOptions is an OptionSetType in Swift 2.It offers a set-like interface and can be initialized from an arrayliteral.

So valid options would be for example:

base64EncodedStringWithOptions([]) // no options
base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
base64EncodedStringWithOptions([.Encoding64CharacterLineLength, .EncodingEndLineWithCarriageReturn])


Full example:

let apiLoginString = NSString(format: "%@:%@", "user", "password")
let apiLoginData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([])

print(base64ApiLoginString)
// dXNlcjpwYXNzd29yZA==

这篇关于Swift 2 base64编码产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:49