[绍棠_Swift] Swift4.0如何实现字符串的HMAC_SHA1加密
2017-10-12 17:02
330 查看
一、 首先创建桥接头文件bridge.h来包含需要引用的Objective-C头文件,并在项目中配置
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
二、 建立一个封装类,对String进行HMAC扩展
import Foundation
// MARK: - 加密 HMAC_SHA1/MD5/SHA1/SHA224......
/** 需在桥接文件导入头文件 ,因为C语言的库
* #import <CommonCrypto/CommonDigest.h>
* #import <CommonCrypto/CommonHMAC.h>
*/
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:加密方法
* 参数1:加密方式; 参数2:加密的key
*/
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)
}
}
注意: swift4.0中不支持同名函数了
df7d
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
二、 建立一个封装类,对String进行HMAC扩展
import Foundation
// MARK: - 加密 HMAC_SHA1/MD5/SHA1/SHA224......
/** 需在桥接文件导入头文件 ,因为C语言的库
* #import <CommonCrypto/CommonDigest.h>
* #import <CommonCrypto/CommonHMAC.h>
*/
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:加密方法
* 参数1:加密方式; 参数2:加密的key
*/
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)
}
}
注意: swift4.0中不支持同名函数了
df7d
相关文章推荐
- Swift - 如何实现字符串的HMAC_SHA1加密
- Swift - 如何实现字符串的HMAC_SHA1加密
- HMAC_SHA1加密-PKCS5_PBKDF2_HMAC_SHA1的方法实现
- Objective-C的字符串MD5、SHA1、SHA256、AES加密的实现
- Qt 中实现 hmac Sha1 加密并以 base64编码输出
- 如何对中文字符串 进行 sha1加密
- swift 实现websocket与后台通信(swift 如何构建简单的json字符串)
- java 实现 hmac_sha1加密
- NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密
- Java实现HmacSHA1加密
- asp.net2.0如何加密数据库联接字符串
- php基于mcrypt_encrypt和mcrypt_decrypt实现字符串加密解密的方法
- go1.5 动态密码,最简单的实现使用hmac加密
- ASP.NET基础教程-实现MD5和SHA1口令加密
- JAVA如何实现字符串的颠倒
- java读取文件以字节流的形式读取 然后转化为字节流数组,最后经过SHA1加密生成返回16进制字符串存入数据库
- [导入]asp.net2.0如何加密数据库联接字符串
- 在vs2010中用c#实现连接mysql数据库以及sha1数据加密
- asp.net2.0如何加密数据库联接字符串
- 如何手动实现C语言中的字符串操作