-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99ac942
commit 675ffed
Showing
6 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 FishGoddess. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hmac | ||
|
||
import ( | ||
stdhmac "crypto/hmac" | ||
"fmt" | ||
"hash" | ||
) | ||
|
||
func hmac(hashFunc func() hash.Hash, key []byte, bs []byte) ([]byte, error) { | ||
hm := stdhmac.New(hashFunc, key) | ||
|
||
n, err := hm.Write(bs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if n != len(bs) { | ||
return nil, fmt.Errorf("cryptox: hmac written n %d != len(bs) %d", n, len(bs)) | ||
} | ||
|
||
return hm.Sum(nil), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 FishGoddess. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hmac | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
// go test -v -cover -run=^TestHMAC$ | ||
func TestHMAC(t *testing.T) { | ||
testCases := map[string]string{ | ||
"": "63530468a04e386459855da0063b6596", | ||
"123": "52851cb05258c8d98da1672d95729e53", | ||
"你好,世界": "e76d8f84103533dc5d22a6e00cef74f3", | ||
} | ||
|
||
key := []byte("key") | ||
for input, expect := range testCases { | ||
sum, err := hmac(md5.New, key, []byte(input)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
sumHex := hex.EncodeToString(sum) | ||
if sumHex != expect { | ||
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2024 FishGoddess. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hmac | ||
|
||
import ( | ||
"crypto/md5" | ||
|
||
"github.com/FishGoddess/cryptox/bytes" | ||
) | ||
|
||
func MD5(key []byte, bs []byte) ([]byte, error) { | ||
return hmac(md5.New, key, bs) | ||
} | ||
|
||
func MD5Hex(key []byte, bs []byte) (string, error) { | ||
hm, err := hmac(md5.New, key, bs) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return bytes.Hex(hm), nil | ||
} | ||
|
||
func MD5Base64(key []byte, bs []byte) (string, error) { | ||
hm, err := hmac(md5.New, key, bs) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return bytes.Base64(hm), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 FishGoddess. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package hmac | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
// go test -v -cover -run=^TestMD5$ | ||
func TestMD5(t *testing.T) { | ||
testCases := map[string]string{ | ||
"": "63530468a04e386459855da0063b6596", | ||
"123": "52851cb05258c8d98da1672d95729e53", | ||
"你好,世界": "e76d8f84103533dc5d22a6e00cef74f3", | ||
} | ||
|
||
key := []byte("key") | ||
for input, expect := range testCases { | ||
sum, err := MD5(key, []byte(input)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
sumHex := hex.EncodeToString(sum) | ||
if sumHex != expect { | ||
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect) | ||
} | ||
} | ||
} |