Skip to content

Commit

Permalink
====feat: add md5====
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoyc666 committed Aug 2, 2023
1 parent 54c5125 commit 68dd4d6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 15 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,28 @@
1. [aes](aes/aes.go), aes ecb加解密示例,包含base64转换,对应的python版本[aes.py](https://github.com/miaoyc666/pyArsenal/blob/master/aes.py)
2. [array](array/array.go), 数组相关操作
3. [chan](chan/chan.go), chan
4. [download](download/download.go), download file
5. [file](file/file.go),文件操作相关
6. [flag](flag/flag.go), 获取命令行参数
7. [http](http/http.go): http客户端
8. [json](json), json操作示例,包含多种json库(标准json库、fastjson和gjson)的解析与生成示例
9. [mac addr](network/network.go),获取mac地址列表,获取pci总线上真实的网卡顺序
10. [mongo](mongo/main.go), mongodb写入与读取
11. [orm](orm/README.md), orm
12. [panic](panic/main.go), panic
13. [qps test](qps/qps.go),限制qps测试
14. [re](re/re.go), regexp and regexp2
15. [service register](serviceRegister/serviceRegister.go), 接口对象注册注册,可用于业务逻辑抽象,逻辑层与io层分离
16. [system](system/system.go),系统命令调用
17. [udp](udp/udpClient.go), udp程序示例
18. [version diff](versionDiff/versionDiff.go),版本号比较
4. [crypto](crypto/md5.go), 字符串加密
5. [download](download/download.go), download file
6. [file](file/file.go),文件操作相关
7. [flag](flag/flag.go), 获取命令行参数
8. [http](http/http.go): http客户端
9. [json](json), json操作示例,包含多种json库(标准json库、fastjson和gjson)的解析与生成示例
10. [mac addr](network/network.go),获取mac地址列表,获取pci总线上真实的网卡顺序
11. [mongo](mongo/main.go), mongodb写入与读取
12. [orm](orm/README.md), orm
13. [panic](panic/main.go), panic
14. [qps test](qps/qps.go),限制qps测试
15. [re](re/re.go), regexp and regexp2
16. [service register](serviceRegister/serviceRegister.go), 接口对象注册注册,可用于业务逻辑抽象,逻辑层与io层分离
17. [system](system/system.go),系统命令调用
18. [udp](udp/udpClient.go), udp程序示例
19. [version diff](versionDiff/versionDiff.go),版本号比较

### 函数列表
#### crypt函数列表
- Md5 获取字符串md5
- MD5sumFromFile 计算文件md5

#### logger函数列表
- Setup 设置日志模块配置
- Debug/Info/Warn/Error...
Expand Down
59 changes: 59 additions & 0 deletions crypto/md5.go
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
}
51 changes: 51 additions & 0 deletions crypto/md5_test.go
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)
}
})
}
}

0 comments on commit 68dd4d6

Please sign in to comment.