Skip to content

Commit 3e4b280

Browse files
committed
feat: add hmac-sha256
1 parent f9f06f8 commit 3e4b280

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A functions wrapping of OpenSSL library for symmetric and asymmetric encryption
99
- [DES](#DES)
1010
- [3DES](#DES)
1111
- [RSA](#RSA)
12+
- [HMAC-SHA](#HMAC-SHA)
1213

1314
## Installation
1415

@@ -98,6 +99,28 @@ openssl.RSASign(src []byte, priKey []byte) ([]byte, error)
9899
openssl.RSAVerify(src, sign, pubKey []byte) error
99100
```
100101

102+
### HMAC-SHA
103+
104+
```
105+
// Sha1 Calculate the sha1 hash of a string
106+
Sha1(str string) []byte
107+
108+
// HmacSha1 Calculate the sha1 hash of a string using the HMAC method
109+
HmacSha1(key string, data string) []byte
110+
111+
// HmacSha1ToString Calculate the sha1 hash of a string using the HMAC method, outputs lowercase hexits
112+
HmacSha1ToString(key string, data string) string
113+
114+
// Sha256 Calculate the sha256 hash of a string
115+
Sha256(str string) []byte
116+
117+
// HmacSha256 Calculate the sha256 hash of a string using the HMAC method
118+
HmacSha256(key string, data string) []byte
119+
120+
// HmacSha256ToString Calculate the sha256 hash of a string using the HMAC method, outputs lowercase hexits
121+
HmacSha256ToString(key string, data string) string
122+
```
123+
101124
## License
102125

103126
This project is licensed under the [Apache 2.0 license](LICENSE).

sha.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package openssl
33
import (
44
"crypto/hmac"
55
"crypto/sha1"
6+
"crypto/sha256"
7+
"encoding/hex"
68
)
79

810
// Sha1 Calculate the sha1 hash of a string
@@ -12,9 +14,35 @@ func Sha1(str string) []byte {
1214
return h.Sum(nil)
1315
}
1416

17+
// HmacSha1 Calculate the sha1 hash of a string using the HMAC method
1518
func HmacSha1(key string, data string) []byte {
1619
mac := hmac.New(sha1.New, []byte(key))
1720
_, _ = mac.Write([]byte(data))
1821

1922
return mac.Sum(nil)
2023
}
24+
25+
// HmacSha1ToString Calculate the sha1 hash of a string using the HMAC method, outputs lowercase hexits
26+
func HmacSha1ToString(key string, data string) string {
27+
return hex.EncodeToString(HmacSha1(key, data))
28+
}
29+
30+
// Sha256 Calculate the sha256 hash of a string
31+
func Sha256(str string) []byte {
32+
h := sha256.New()
33+
_, _ = h.Write([]byte(str))
34+
return h.Sum(nil)
35+
}
36+
37+
// HmacSha256 Calculate the sha256 hash of a string using the HMAC method
38+
func HmacSha256(key string, data string) []byte {
39+
mac := hmac.New(sha256.New, []byte(key))
40+
_, _ = mac.Write([]byte(data))
41+
42+
return mac.Sum(nil)
43+
}
44+
45+
// HmacSha256ToString Calculate the sha256 hash of a string using the HMAC method, outputs lowercase hexits
46+
func HmacSha256ToString(key string, data string) string {
47+
return hex.EncodeToString(HmacSha256(key, data))
48+
}

sha_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package openssl
22

33
import (
4+
"encoding/hex"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,3 +12,20 @@ func TestSha1(t *testing.T) {
1112
dst := Sha1(src)
1213
assert.Equal(t, dst, []byte{0xd0, 0xbe, 0x2d, 0xc4, 0x21, 0xbe, 0x4f, 0xcd, 0x1, 0x72, 0xe5, 0xaf, 0xce, 0xea, 0x39, 0x70, 0xe2, 0xf3, 0xd9, 0x40})
1314
}
15+
16+
func TestHmacSha1ToString(t *testing.T) {
17+
src := "apple"
18+
dst := HmacSha1ToString("secret", src)
19+
assert.Equal(t, dst, "2651783bdc7367acd2dde6f830ca0b7104368911")
20+
}
21+
22+
func TestSha256(t *testing.T) {
23+
src := "apple"
24+
dst := Sha256(src)
25+
assert.Equal(t, hex.EncodeToString(dst), "3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b")
26+
}
27+
28+
func TestHmacSha256ToString(t *testing.T) {
29+
dst := HmacSha256ToString("secret", "apple")
30+
assert.Equal(t, dst, "37431003b2d14b6bddb9334c7ec2ff0ea0c65f96ec650952384e56cae83c398f")
31+
}

0 commit comments

Comments
 (0)