先寫(xiě)一個(gè)方法按照七牛技術(shù)文檔說(shuō)的拼接token
func generateToken(accessKey: NSString!, secretKey: NSString!, bucketName: NSString!) -> String{
let deadline = NSDate().timeIntervalSince1970
print(deadline)
print(Int(deadline))
let policyDict: Dictionary<AnyHashable, Any> = ["scope": bucketName, "deadline": Int(deadline) + 3600]
print(policyDict)
let policyJSONData = try! JSONSerialization.data(withJSONObject: policyDict, options: .prettyPrinted)
let policyJSON = NSString(data: policyJSONData, encoding: String.Encoding.utf8.rawValue)
print(policyJSON as! String)
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!)"
print(token)
return token
}
加密擴(kuò)展類(lèi)的寫(xiě)法String+HMAC.swift
import Foundation
enum CryptoAlgorithm {
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
var HMACAlgorithm: CCHmacAlgorithm {
var result: Int = 0
switch self {
case .MD5: result = kCCHmacAlgMD5
case .SHA1: result = kCCHmacAlgSHA1
case .SHA224: result = kCCHmacAlgSHA224
case .SHA256: result = kCCHmacAlgSHA256
case .SHA384: result = kCCHmacAlgSHA384
case .SHA512: result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}
var digestLength: Int {
var result: Int32 = 0
switch self {
case .MD5: result = CC_MD5_DIGEST_LENGTH
case .SHA1: result = CC_SHA1_DIGEST_LENGTH
case .SHA224: result = CC_SHA224_DIGEST_LENGTH
case .SHA256: result = CC_SHA256_DIGEST_LENGTH
case .SHA384: result = CC_SHA384_DIGEST_LENGTH
case .SHA512: result = CC_SHA512_DIGEST_LENGTH
}
return Int(result)
}
}
extension String {
func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
let str = self.cString(using: String.Encoding.utf8)
let strLen = Int(self.lengthOfBytes(using: String.Encoding.utf8))
let digestLen = algorithm.digestLength
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
let keyStr = key.cString(using: String.Encoding.utf8)
let keyLen = Int(key.lengthOfBytes(using: String.Encoding.utf8))
CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)
let digest = stringFromResult(result: result, length: digestLen)
result.deallocate(capacity: digestLen)
return digest
}
private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
let hash = NSMutableString()
for i in 0..<length {
hash.appendFormat("%02x", result[i])
}
return String(hash)
}
}
調(diào)用方法
//上傳騎牛
let access_key = "你的access_key"
let secretKey = "你的secretKey"
let name:NSString = "你的bucketName"
let token = generateToken(accessKey:access_key as NSString!, secretKey:secretKey as NSString!, bucketName:name)
let upManager = QNUploadManager()
upManager?.putFile(filePath, key:nil, token: token, complete: { (info, key, resp) in
if (info?.isOK)!{
//上傳完畢
SVProgressHUD.showSuccess(withStatus: "上傳成功")
print(key)
print(info)
//
print(resp)
}else{
//失敗
print(key)
print(info)
print(resp)
}
}, option: nil)
完畢結(jié)果發(fā)現(xiàn)被后臺(tái)給坑了顷扩,這玩意千萬(wàn)不能本地生成凡纳,要從后臺(tái)拿~~
···