大神可否问个问题,我用swift实现本地生成token,基于mac最终本来应当的encoded_signed之后的字符串是28位,我用oc的sdk方法生成的是57位,求大神有空帮我看看错在哪里啊:
func generateToken(accessKey: NSString!, secretKey: NSString!, bucketName: NSString!) -> String{
// 确定上传策略
// 第二步:
// 将上传策略序列化成为json格式:
// {"scope":"test-scope","deadline":1477997128}
// 第三步:
// 对json序列化后的上传策略进行URL安全的Base64编码,得到如下encoded:
// eyJzY29wZSI6InRlc3Qtc2NvcGUiLCJkZWFkbGluZSI6MTQ3Nzk5NzEyOH0=
// 第四步:
// 用SecretKey对编码后的上传策略进行HMAC-SHA1加密,并且做URL安全的Base64编码,得到如下的encoded_signed:
// lHglRYz+TLhmFxb6LkI2ekXn0Cc=
// 第五步:
// 将 AccessKey、encode_signed 和 encoded 用 “:” 连接起来,得到如下的UploadToken:
// 33HnfbHNaNgwZbIsuEO3gkXmhEZtUzDhFfHaK2YK:lHglRYz-TLhmFxb6LkI2ekXn0Cc=:eyJzY29wZSI6InRlc3Qtc2NvcGUiLCJkZWFkbGluZSI6MTQ3Nzk5NzEyOH0=
let deadline = NSDate().timeIntervalSince1970
print(deadline)
print(Int(deadline))
let policyDict: Dictionary<AnyHashable, Any> = ["scope": bucketName, "deadline": Int(deadline) + 3600]
let policyJSONData = try! JSONSerialization.data(withJSONObject: policyDict, options: .prettyPrinted)
let policyJSON = NSString(data: policyJSONData, encoding: String.Encoding.utf8.rawValue)
print(policyJSON)
let policyData = policyJSON?.data(using: String.Encoding.utf8.rawValue)
let encodedPolicy = QN_GTM_Base64.string(byWebSafeEncoding: policyData, padded: true)
print(encodedPolicy)
let hmacStr = encodedPolicy?.hmac(algorithm: .SHA1, key: secretKey as String)
let hmacData = hmacStr?.data(using: .utf8)
let encodedSigned = QN_GTM_Base64.string(byWebSafeEncoding: hmacData, padded: true)
print(encodedSigned)
let token = "\(accessKey!):\(encodedSigned!):\(encodedPolicy!)"
return token
}
不要在客户端生成 token 呀,从服务端获取。