-
Notifications
You must be signed in to change notification settings - Fork 1
/
Md5.go
65 lines (52 loc) · 1.05 KB
/
Md5.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package part
import (
"sync"
"io"
"crypto/md5"
"fmt"
"os"
"time"
)
type md5l struct{sync.Mutex}
func Md5() *md5l{
return &md5l{}
}
func (this *md5l) Md5String(str string) string {
this.Lock()
defer this.Unlock()
w := md5.New()
io.WriteString(w, str)
md5str := fmt.Sprintf("%x", w.Sum(nil))
return md5str
}
func (this *md5l) Md5File(path string) (string, error) {
this.Lock()
defer this.Unlock()
file, err := os.Open(path)
defer file.Close()
if err != nil {
return "",err
}
h := md5.New()
_, err = io.Copy(h,file)
if err != nil {
return "",err
}
return fmt.Sprintf("%x",h.Sum(nil)), nil
}
func (this *md5l) GetFileModTime(path string) (error,int64) {
this.Lock()
defer this.Unlock()
f, err := os.Open(path)
if err != nil {
fmt.Println("open file error")
return err,time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
fmt.Println("stat fileinfo error")
return err,time.Now().Unix()
}
return nil,fi.ModTime().Unix()
}