在Swift 3.1中使用CommonCrypto的HMAC SHA512 [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试加密数据以发送到API。

API要求将数据作为hmac_sha512加密哈希发送。

我已经找到了各种各样的例子,说明如何为sha1和其他人(不是sha512)以及旧版本的Swift完成它。

我试过的所有例子都没有为swift 3.1工作

任何正确方向的帮助将不胜感激。

编辑:

在PHP中,我使用以下方法成功发送:

    $sign = hash_hmac('sha512', $post_data, $this->secret);

编辑2:

did add briding header,我不知道下一步该做什么!由于之后的代码示例不适用于swift 3.1 :(

编辑3:

解决了!猜猜看,我正在错误地创建桥接头! :(

P.S我试图避免使用CryptoSwift,专注于CommonCrypto。

下面给出的答案是不正确的,因为它不允许hmac获得加密密钥。我做了研究,终于搞定了。这篇文章包含了hmac:https://github.com/nabtron/hmacTest的工作示例项目

swift xcode hmac sha512 commoncrypto
1个回答
6
投票

我认为最好的办法是使用Crypto pod,它是普通加密的包装器。如果你想直接使用commonCrypto,你应该将桥接头添加到项目中并使用以下命令导入公共加密:#import <CommonCrypto/CommonCrypto.h>

编辑1

创建一个swift类并向其添加以下代码:

import Foundation

extension String {
    var md5: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.MD5)
    }

    var sha1: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.SHA1)
    }

    var sha224: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.SHA224)
    }

    var sha256: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.SHA256)
    }

    var sha384: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.SHA384)
    }

    var sha512: String {
        return HMAC.hash(inp: self, algo: HMACAlgo.SHA512)
    }
}

public struct HMAC {

    static func hash(inp: String, algo: HMACAlgo) -> String {
        if let stringData = inp.data(using: String.Encoding.utf8, allowLossyConversion: false) {
            return hexStringFromData(input: digest(input: stringData as NSData, algo: algo))
        }
        return ""
    }

    private static func digest(input : NSData, algo: HMACAlgo) -> NSData {
        let digestLength = algo.digestLength()
        var hash = [UInt8](repeating: 0, count: digestLength)
        switch algo {
        case .MD5:
            CC_MD5(input.bytes, UInt32(input.length), &hash)
            break
        case .SHA1:
            CC_SHA1(input.bytes, UInt32(input.length), &hash)
            break
        case .SHA224:
            CC_SHA224(input.bytes, UInt32(input.length), &hash)
            break
        case .SHA256:
            CC_SHA256(input.bytes, UInt32(input.length), &hash)
            break
        case .SHA384:
            CC_SHA384(input.bytes, UInt32(input.length), &hash)
            break
        case .SHA512:
            CC_SHA512(input.bytes, UInt32(input.length), &hash)
            break
        }
        return NSData(bytes: hash, length: digestLength)
    }

    private static func hexStringFromData(input: NSData) -> String {
        var bytes = [UInt8](repeating: 0, count: input.length)
        input.getBytes(&bytes, length: input.length)

        var hexString = ""
        for byte in bytes {
            hexString += String(format:"%02x", UInt8(byte))
        }

        return hexString
    }
}

enum HMACAlgo {
    case MD5, SHA1, SHA224, SHA256, SHA384, SHA512

    func digestLength() -> Int {
        var result: CInt = 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)
    }
}

然后简单地用stringName.sha512来使用它 此类扩展了String类,该类可以将散列作为字符串类中的函数使用。

© www.soinside.com 2019 - 2024. All rights reserved.