在Swift 3.1中使用CommonCrypto的HMAC SHA512 [英] HMAC SHA512 using CommonCrypto in Swift 3.1

查看:124
本文介绍了在Swift 3.1中使用CommonCrypto的HMAC SHA512的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to encrypt data to send to the API.

API要求将数据作为hmac_sha512加密散列发送.

The API requires the data to be sent as hmac_sha512 encrypted hash.

我已经找到了许多示例说明如何对sha1和其他(而不是sha512)以及较旧版本的Swift进行此操作.

I've found various examples of how it possibly could have been done for sha1 and others (not sha512) and also in older versions of Swift.

我尝试用于swift 3.1

在正确方向上的任何帮助将不胜感激.

Any help in the right direction will be appreciated.

在PHP中,我使用以下命令成功发送了该邮件:

In PHP, I successfully send it using:

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

did add briding header,我不知道下一步该怎么做!如后面的代码示例所示,它不适用于swift 3.1:(

I did add briding header, I don't know what to do next! As the code examples followed after that don't work for swift 3.1 :(

修改3:

已解决!.猜猜我在错误地创建桥接头! :(

Solved! Guess what, I was creating briding header incorrectly! :(

P.S我正在尝试避免使用CryptoSwift,而将注意力放在CommonCrypto上.

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

The answer given below is not proper, as it doesn't allow hmac to get a key for encryption. I did research and finally got it working. This post contains the working example project for hmac: https://github.com/nabtron/hmacTest

推荐答案

我认为最好的方法是使用加密 pod,它是普通加密货币的包装.如果要直接使用commonCrypto,则应将桥接头添加到项目中,并使用以下命令导入通用加密:#import <CommonCrypto/CommonCrypto.h>

I think the best thing to do is using Crypto pod which is a wrapper for common crypto. In case you want to use directly commonCrypto you should add the bridging header to the project and import common crypto using: #import <CommonCrypto/CommonCrypto.h>

编辑1

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

Create a swift class and add the following code to it:

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类,该类提供了将哈希用作字符串类中的函数的功能.

then use it simply by stringName.sha512
this class extends the String class which gives the ability to use the hashing as a function in the string class.

这篇关于在Swift 3.1中使用CommonCrypto的HMAC SHA512的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆