Skip to content

Commit 1af2e05

Browse files
committed
Add Post
Signed-off-by: BY <qiubaiying@gmail.com>
1 parent 10b5fc9 commit 1af2e05

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
layout: post
3+
title: Swift的HMAC和SHA1加密
4+
subtitle: swift中利用HMAC的SHA1对文本进行加密
5+
date: 2017-07-04
6+
author: BY
7+
header-img: img/post-bg-coffee.jpeg
8+
catalog: true
9+
tags:
10+
- Swift
11+
---
12+
13+
>HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code)。 HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。也就是说HMAC通过将哈希算法(SHA1, MD5)与密钥进行计算生成摘要。
14+
15+
## Objectice-C
16+
17+
在上个 Objectice-C 项目中,使用的 HMAC 和 SHA1 进行加密。如下代码:
18+
19+
```objc
20+
+ (NSString *)hmacsha1:(NSString *)text key:(NSString *)secret {
21+
22+
NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
23+
NSData *clearTextData = [text dataUsingEncoding:NSUTF8StringEncoding];
24+
unsigned char result[20];
25+
// SHA1加密
26+
CCHmac(kCCHmacAlgSHA1, [secretData bytes], [secretData length], [clearTextData bytes], [clearTextData length], result);
27+
char base64Result[32];
28+
size_t theResultLength = 32;
29+
// 转为Base64
30+
Base64EncodeData(result, 20, base64Result, &theResultLength,YES);
31+
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
32+
NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
33+
return base64EncodedResult;
34+
}
35+
```
36+
37+
38+
39+
## swift
40+
41+
最近用 swift 重构项目,用 Swift [重写了](https://stackoverflow.com/questions/26970807/implementing-hmac-and-sha1-encryption-in-swift?rq=1) HMAC 的 SHA1 加密方式。
42+
43+
### 使用
44+
45+
```swift
46+
// 使用HMAC和SHA加密
47+
let hmacResult:String = "myStringToHMAC".hmac(HMACAlgorithm.SHA1, key: "myKey")
48+
```
49+
50+
### 代码
51+
52+
使用下面代码时,需要在 OC 桥接文件`xxx-Bridging-Header``#import <CommonCrypto/CommonHMAC.h>`
53+
54+
```swift
55+
extension String {
56+
func hmac(algorithm: HMACAlgorithm, key: String) -> String {
57+
let cKey = key.cStringUsingEncoding(NSUTF8StringEncoding)
58+
let cData = self.cStringUsingEncoding(NSUTF8StringEncoding)
59+
var result = [CUnsignedChar](count: Int(algorithm.digestLength()), repeatedValue: 0)
60+
CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, strlen(cKey!), cData!, strlen(cData!), &result)
61+
var hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
62+
var hmacBase64 = hmacData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding76CharacterLineLength)
63+
return String(hmacBase64)
64+
}
65+
}
66+
67+
enum HMACAlgorithm {
68+
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
69+
70+
func toCCHmacAlgorithm() -> CCHmacAlgorithm {
71+
var result: Int = 0
72+
switch self {
73+
case .MD5:
74+
result = kCCHmacAlgMD5
75+
case .SHA1:
76+
result = kCCHmacAlgSHA1
77+
case .SHA224:
78+
result = kCCHmacAlgSHA224
79+
case .SHA256:
80+
result = kCCHmacAlgSHA256
81+
case .SHA384:
82+
result = kCCHmacAlgSHA384
83+
case .SHA512:
84+
result = kCCHmacAlgSHA512
85+
}
86+
return CCHmacAlgorithm(result)
87+
}
88+
89+
func digestLength() -> Int {
90+
var result: CInt = 0
91+
switch self {
92+
case .MD5:
93+
result = CC_MD5_DIGEST_LENGTH
94+
case .SHA1:
95+
result = CC_SHA1_DIGEST_LENGTH
96+
case .SHA224:
97+
result = CC_SHA224_DIGEST_LENGTH
98+
case .SHA256:
99+
result = CC_SHA256_DIGEST_LENGTH
100+
case .SHA384:
101+
result = CC_SHA384_DIGEST_LENGTH
102+
case .SHA512:
103+
result = CC_SHA512_DIGEST_LENGTH
104+
}
105+
return Int(result)
106+
}
107+
}
108+
109+
110+
```
111+

0 commit comments

Comments
 (0)