-
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
Showing
3 changed files
with
130 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package crypto | ||
|
||
import ( | ||
"bufio" | ||
"crypto/md5" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
/* | ||
File name : md5.go | ||
Author : miaoyc | ||
Create date : 2021/12/2 4:34 下午 | ||
Description : 加密相关 | ||
*/ | ||
|
||
var ( | ||
readFileBufferSize = 65536 | ||
) | ||
|
||
// Md5 md5加密,input: 需加密的字符串, return:加密之后的32个字符的数据 | ||
func Md5(data string) (md5str string) { | ||
h := md5.New() | ||
h.Write([]byte(data)) | ||
md5str = fmt.Sprintf("%x", h.Sum(nil)) | ||
return md5str | ||
} | ||
|
||
// MD5sumFromFile 计算文件的md5 | ||
func MD5sumFromFile(filename string) (string, error) { | ||
if info, err := os.Stat(filename); err != nil { | ||
return "", err | ||
} else if info.IsDir() { | ||
return "", nil | ||
} | ||
|
||
file, err := os.Open(filename) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
hash := md5.New() | ||
for buf, reader := make([]byte, readFileBufferSize), bufio.NewReader(file); ; { | ||
n, err := reader.Read(buf) | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return "", err | ||
} | ||
|
||
hash.Write(buf[:n]) | ||
} | ||
|
||
checksum := fmt.Sprintf("%x", hash.Sum(nil)) | ||
return checksum, 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,51 @@ | ||
package crypto | ||
|
||
import "testing" | ||
|
||
func TestMd5(t *testing.T) { | ||
type args struct { | ||
data string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantMd5str string | ||
}{ | ||
// TODO: Add test cases. | ||
{"md5", args{"miaoyc"}, "ece751dcb8e591181c83e121689cc6ba"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if gotMd5str := Md5(tt.args.data); gotMd5str != tt.wantMd5str { | ||
t.Errorf("Md5() = %v, want %v", gotMd5str, tt.wantMd5str) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMD5sumFromFile(t *testing.T) { | ||
type args struct { | ||
filename string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{"clac file md5", args{"./md5.go"}, "34173e9e3d1bd8952b2f8beba2083d91", false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := MD5sumFromFile(tt.args.filename) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("MD5sumFromFile() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("MD5sumFromFile() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |