最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中计算js的Base64.encode

Swift crifan 2351浏览 0评论

需要Swift中去计算,类似于如下js中的Base64.encode的值:

function make_base_auth(user, password) { 
  var tok = user + ‘:’ + pass; 
  var hash = Base64.encode(tok); 
  return "Basic " + hash; 
}  
var auth = make_basic_auth(‘user-59e68fc0-39c3-4fb3-afef-7a7244d0e87d’,’password1′);

搜:

swift Base64 encode

Base64 Encode and Decode in Swift [iOS developer:tips];

How to encode string to base64 in swift – Stack Overflow

ios – Swift 2 base64 Encoding Yield Different Results – Stack Overflow

Base64 Decoding in iOS 7 Objective-C and iOS8 Swift – iOS-Blog

代码:

import UIKit

let userId = "user-59e68fc0-39c3-4fb3-afef-7a7244d0e87d"
let password = "111111"

let tokenStr = userId + ":" + password
print("tokenStr=\(tokenStr)")

let tokenData = tokenStr.dataUsingEncoding(NSUTF8StringEncoding)
print("tokenData=\(tokenData)")

//        let base64Str = tokenData?.base64EncodedDataWithOptions([])
let base64Str:String = (tokenData?.base64EncodedStringWithOptions([]))!

print("base64Str=\(base64Str)")

输出:

tokenStr=user-59e68fc0-39c3-4fb3-afef-7a7244d0e87d:111111
tokenData=Optional(<75736572 2d353965 36386663 302d3339 63332d34 6662332d 61666566 2d376137 32343464 30653837 643a3131 31313131>)
base64Str=dXNlci01OWU2OGZjMC0zOWMzLTRmYjMtYWZlZi03YTcyNDRkMGU4N2Q6MTExMTEx

效果:

转载请注明:在路上 » [已解决]swift中计算js的Base64.encode

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.295 seconds, using 22.15MB memory