如何使用Swift将字符串加密到base64中的sha1?

ste*_*hen 2 php base64 hmacsha1 ios swift

我希望使用Swift将privateKey和publicKey加载到base64中的sha1,但是输出不是我在Codeclemy中尝试的PHP urlencode base64_encode中看到的:" https://www.codecademy.com/courses/web-beginner- en-StaFQ/0/3?curriculum_id = 5124ef4c78d510dd89003eb8 ".

请参阅Swift和Codecademy中的以下代码:

迅速:

//pls see func dataFromHexadecimalString() details here "http://stackoverflow.com/questions/26501276/convert-string-to-hex-string-in-swift/26502285#26502285" 

extension String {

func dataFromHexadecimalString() -> NSData? {
    let trimmedString = self.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<> ")).stringByReplacingOccurrencesOfString(" ", withString: "")

    var error: NSError?
    let regex = NSRegularExpression(pattern: "^[0-9a-f]*$", options: .CaseInsensitive, error: &error)
    let found = regex?.firstMatchInString(trimmedString, options: nil, range: NSMakeRange(0, count(trimmedString)))
    if found == nil || found?.range.location == NSNotFound || count(trimmedString) % 2 != 0 {
        return nil
    }

    let data = NSMutableData(capacity: count(trimmedString) / 2)

    for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = index.successor().successor() {
        let byteString = trimmedString.substringWithRange(Range<String.Index>(start: index, end: index.successor().successor()))
        let num = UInt8(byteString.withCString { strtoul($0, nil, 16) })
        data?.appendBytes([num] as [UInt8], length: 1)
    }

    return data
  }
}

func URLEcodekey() -> String {
    let appid="a1b2c34d5e"
    let privateKey="ef7d6s0d"
    let areaid="101020900"
    let time="201507191254"
    let publicKey="http://open.weather.com.cn/data/?areaid=\(areaid)&type=forecast_v&date=\(time)&appid=\(appid)"

    let cPrivateKey=privateKey.dataUsingEncoding(NSUTF8StringEncoding)!
    let cPublicKey=publicKey.dataUsingEncoding(NSUTF8StringEncoding)!
    var cHMAC = [CUnsignedChar](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), cPublicKey.bytes, Int(cPublicKey.length), cPrivateKey.bytes, Int(cPrivateKey.length), &cHMAC)

    let hexKeyString=NSMutableString(capacity: Int(CC_SHA1_DIGEST_LENGTH))
    for byte in cHMAC{
        hexKeyString.appendFormat("%02hhx", byte)
    }
    println("hexKeyString:\(encryptedKey)")

    let binaryData = hexKeyString.dataFromHexadecimalString()
    let base64String = binaryData?.base64EncodedStringWithOptions(nil)
    println("base64String:\(base64String)")

    var urlEncodeKey=base64String!.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
    println("urlEncodeKey:\(urlEncodeKey)")
    return urlEncodeMessage
}
Run Code Online (Sandbox Code Playgroud)

产出是:

hexKeyString:d4433d42b1505c00a4aa80205171d0d04754d254

base64String:1EM9QrFQXACkqoAgUXHQ0EdU0lQ =

urlEncodeKey:1EM9QrFQXACkqoAgUXHQ0EdU0lQ =

Codecademy中的PHP:

echo urlencode(base64_encode(hash_hmac('sha1', " http://open.weather.com.cn/data/?areaid=101020900&type=forecast_v&date=201507191254&appid=a1b2c34d5e", "ef7d6s0d", TRUE)));
Run Code Online (Sandbox Code Playgroud)

输出是:

A5O59Y%2BFbGjhVwaI9JNB7DkcX%2F4%3D //输出很像API中的例子,我认为它可能是正确的.

那么,如何在PHP中为我的privateKey和publicKey 接收正确的urlEncodeKey

非常感谢你提前!

zrz*_*zka 6

您应该阅读有关加密和散列的更多信息.在你的情况下,没有公钥,私钥,...... SHA代表安全哈希算法,你想要获得的是基于哈希的身份验证代码.检查有关HMAC,SHA-1,公钥的维基百科文章......我强烈建议您阅读更多相关内容,否则如果您误解了它可能会造成更多伤害.

回到你的问题.这是一个字符:

  • SWIFT代码 - let publicKey="http://open.weather.com.cn...
  • PHP代码 - hash_hmac('sha1', " http://open.weather.com.cn...

你看到问题出在哪里了吗?在你的PHP代码中, character just before http.此字符不在您的Swift代码中.

老实说,我没有检查你的整个代码,因为我不知道你为什么要尝试从十六进制字符串转换它等等.只使用一些部分并为你重新编写它.这是工作示例:

func URLEncodedKey() -> String? {
  let appid = "a1b2c34d5e"
  let time = "201507191254"
  let areaid = "101020900"

  let key = "ef7d6s0d"
  let string = " http://open.weather.com.cn/data/?areaid=\(areaid)&type=forecast_v&date=\(time)&appid=\(appid)"
  //            ^  <- in your PHP example, there's space

  guard let keyData = key.dataUsingEncoding(NSUTF8StringEncoding),
    stringData = string.dataUsingEncoding(NSUTF8StringEncoding),
    outputData = NSMutableData(length: Int(CC_SHA1_DIGEST_LENGTH)) else {
    return nil
  }
  outputData.length = Int(CC_SHA1_DIGEST_LENGTH)

  CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1),
    keyData.bytes, keyData.length,
    stringData.bytes, stringData.length,
    outputData.mutableBytes)

  return outputData
    .base64EncodedStringWithOptions([])
    .stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
}
Run Code Online (Sandbox Code Playgroud)

返回值是:

`Optional("A5O59Y+FbGjhVwaI9JNB7DkcX/4=")`
Run Code Online (Sandbox Code Playgroud)

这是解码PHP输出时得到的结果.

只需替换URLQueryAllowedCharacterSet以下任何字符集:

class func URLUserAllowedCharacterSet() -> NSCharacterSet    
class func URLPasswordAllowedCharacterSet() -> NSCharacterSet
class func URLHostAllowedCharacterSet() -> NSCharacterSet    
class func URLPathAllowedCharacterSet() -> NSCharacterSet
class func URLQueryAllowedCharacterSet() -> NSCharacterSet
class func URLFragmentAllowedCharacterSet() -> NSCharacterSet
Run Code Online (Sandbox Code Playgroud)

取决于您的用例.IOW您希望使用哪个URL部分.