swift3上传图片到七牛云怎么本地生成token?

新手上路,请多包涵

大神可否问个问题,我用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
    
}
阅读 2.7k
1 个回答

不要在客户端生成 token 呀,从服务端获取。

推荐问题
logo
七牛云问答
子站问答
访问
宣传栏