@@ -19,117 +19,122 @@ package digest
1919import (
2020 "bufio"
2121 "crypto/md5"
22+ "crypto/sha1"
2223 "crypto/sha256"
24+ "crypto/sha512"
2325 "encoding/hex"
26+ "errors"
27+ "fmt"
2428 "hash"
2529 "io"
2630 "os"
2731 "strings"
28-
29- "github.com/opencontainers/go-digest"
30-
31- "d7y.io/dragonfly/v2/pkg/unit"
3232)
3333
3434const (
35- Sha256Hash digest.Algorithm = "sha256"
36- Md5Hash digest.Algorithm = "md5"
37- )
38-
39- var (
40- // Algorithms is used to check if an algorithm is supported.
41- // If algo is not supported, Algorithms[algo] will return empty string.
42- // Please don't use digest.Algorithm() to convert a string to digest.Algorithm.
43- Algorithms = map [string ]digest.Algorithm {
44- Sha256Hash .String (): Sha256Hash ,
45- Md5Hash .String (): Md5Hash ,
46- }
47- )
48-
49- func Sha256 (values ... string ) string {
50- if len (values ) == 0 {
51- return ""
52- }
35+ // AlgorithmSHA1 is sha1 algorithm name of hash.
36+ AlgorithmSHA1 = "sha1"
5337
54- h := sha256 .New ()
55- for _ , content := range values {
56- if _ , err := h .Write ([]byte (content )); err != nil {
57- return ""
58- }
59- }
38+ // AlgorithmSHA256 is sha256 algorithm name of hash.
39+ AlgorithmSHA256 = "sha256"
6040
61- return ToHashString ( h )
62- }
41+ // AlgorithmSHA512 is sha512 algorithm name of hash.
42+ AlgorithmSHA512 = "sha512"
6343
64- func Md5Reader (reader io.Reader ) string {
65- h := md5 .New ()
66- if _ , err := io .Copy (h , reader ); err != nil {
67- return ""
68- }
44+ // AlgorithmMD5 is md5 algorithm name of hash.
45+ AlgorithmMD5 = "md5"
46+ )
6947
70- return ToHashString (h )
71- }
48+ // Digest provides digest operation function.
49+ type Digest struct {
50+ // Algorithm is hash algorithm.
51+ Algorithm string
7252
73- func Md5Bytes (bytes []byte ) string {
74- h := md5 .New ()
75- h .Write (bytes )
76- return ToHashString (h )
53+ // Encoded is hash encode.
54+ Encoded string
7755}
7856
79- // HashFile computes hash value corresponding to hashType,
80- // hashType is from digestutils.Md5Hash and digestutils.Sha256Hash.
81- func HashFile (path string , hashType digest.Algorithm ) string {
82- file , err := os .Stat (path )
83- if err != nil {
84- return ""
85- }
86-
87- if ! file .Mode ().IsRegular () {
88- return ""
89- }
90-
57+ // HashFile computes hash value corresponding to algorithm.
58+ func HashFile (path string , algorithm string ) (string , error ) {
9159 f , err := os .Open (path )
9260 if err != nil {
93- return ""
61+ return "" , err
9462 }
9563 defer f .Close ()
9664
9765 var h hash.Hash
98- if hashType == Md5Hash {
99- h = md5 .New ()
100- } else if hashType == Sha256Hash {
66+ switch algorithm {
67+ case AlgorithmSHA1 :
68+ h = sha1 .New ()
69+ case AlgorithmSHA256 :
10170 h = sha256 .New ()
102- } else {
103- return ""
71+ case AlgorithmSHA512 :
72+ h = sha512 .New ()
73+ case AlgorithmMD5 :
74+ h = md5 .New ()
75+ default :
76+ return "" , fmt .Errorf ("unsupport digest method: %s" , algorithm )
10477 }
10578
106- r := bufio .NewReaderSize (f , int (4 * unit .MB ))
107-
79+ r := bufio .NewReader (f )
10880 _ , err = io .Copy (h , r )
10981 if err != nil {
110- return ""
82+ return "" , err
11183 }
11284
113- return ToHashString ( h )
85+ return hex . EncodeToString ( h . Sum ( nil )), nil
11486}
11587
116- func ToHashString (h hash.Hash ) string {
117- return hex .EncodeToString (h .Sum (nil ))
88+ // Parse uses to parse digest string to algorithm and encoded.
89+ func Parse (digest string ) (* Digest , error ) {
90+ values := strings .Split (digest , ":" )
91+ if len (values ) == 2 {
92+ return & Digest {
93+ Algorithm : values [0 ],
94+ Encoded : values [1 ],
95+ }, nil
96+ }
97+
98+ if len (values ) == 1 {
99+ return & Digest {
100+ Algorithm : AlgorithmMD5 ,
101+ Encoded : values [0 ],
102+ }, nil
103+ }
104+
105+ return nil , errors .New ("invalid digest" )
118106}
119107
120- func Parse (digest string ) []string {
121- digest = strings .Trim (digest , " " )
122- return strings .Split (digest , ":" )
108+ // Sha256 computes the SHA256 checksum with multiple data.
109+ func Sha256 (data ... string ) string {
110+ if len (data ) == 0 {
111+ return ""
112+ }
113+
114+ h := sha256 .New ()
115+ for _ , s := range data {
116+ if _ , err := h .Write ([]byte (s )); err != nil {
117+ return ""
118+ }
119+ }
120+
121+ return hex .EncodeToString (h .Sum (nil ))
123122}
124123
125- func CreateHash (hashType string ) hash.Hash {
126- algo := Algorithms [hashType ]
127- switch algo {
128- case Sha256Hash :
129- return sha256 .New ()
130- case Md5Hash :
131- return md5 .New ()
132- default :
133- return nil
124+ // Md5Reader computes the MD5 checksum with io.Reader.
125+ func Md5Reader (reader io.Reader ) string {
126+ h := md5 .New ()
127+ r := bufio .NewReader (reader )
128+ if _ , err := io .Copy (h , r ); err != nil {
129+ return ""
134130 }
131+
132+ return hex .EncodeToString (h .Sum (nil ))
133+ }
134+
135+ // Md5Bytes computes the MD5 checksum with []byte.
136+ func Md5Bytes (bytes []byte ) string {
137+ h := md5 .New ()
138+ h .Write (bytes )
139+ return hex .EncodeToString (h .Sum (nil ))
135140}
0 commit comments