|
| 1 | +package crypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "regexp" |
| 6 | + |
| 7 | + // #nosec G501 -- md5 is supported by Artifactory. |
| 8 | + "crypto/md5" |
| 9 | + // #nosec G505 -- sha1 is supported by Artifactory. |
| 10 | + "crypto/sha1" |
| 11 | + "fmt" |
| 12 | + "hash" |
| 13 | + "io" |
| 14 | + "os" |
| 15 | + |
| 16 | + ioutils "github.com/jfrog/gofrog/io" |
| 17 | + "github.com/minio/sha256-simd" |
| 18 | +) |
| 19 | + |
| 20 | +type Algorithm int |
| 21 | + |
| 22 | +const ( |
| 23 | + MD5 Algorithm = iota |
| 24 | + SHA1 |
| 25 | + SHA256 |
| 26 | +) |
| 27 | + |
| 28 | +var algorithmFunc = map[Algorithm]func() hash.Hash{ |
| 29 | + // Go native crypto algorithms: |
| 30 | + MD5: md5.New, |
| 31 | + SHA1: sha1.New, |
| 32 | + // sha256-simd algorithm: |
| 33 | + SHA256: sha256.New, |
| 34 | +} |
| 35 | + |
| 36 | +type Checksum struct { |
| 37 | + Sha1 string `json:"sha1,omitempty"` |
| 38 | + Md5 string `json:"md5,omitempty"` |
| 39 | + Sha256 string `json:"sha256,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +func (c *Checksum) IsEmpty() bool { |
| 43 | + return c.Md5 == "" && c.Sha1 == "" && c.Sha256 == "" |
| 44 | +} |
| 45 | + |
| 46 | +// If the 'other' checksum matches the current one, return true. |
| 47 | +// 'other' checksum may contain regex values for sha1, sha256 and md5. |
| 48 | +func (c *Checksum) IsEqual(other Checksum) (bool, error) { |
| 49 | + match, err := regexp.MatchString(other.Md5, c.Md5) |
| 50 | + if !match || err != nil { |
| 51 | + return false, err |
| 52 | + } |
| 53 | + match, err = regexp.MatchString(other.Sha1, c.Sha1) |
| 54 | + if !match || err != nil { |
| 55 | + return false, err |
| 56 | + } |
| 57 | + match, err = regexp.MatchString(other.Sha256, c.Sha256) |
| 58 | + if !match || err != nil { |
| 59 | + return false, err |
| 60 | + } |
| 61 | + |
| 62 | + return true, nil |
| 63 | +} |
| 64 | + |
| 65 | +func GetFileChecksums(filePath string, checksumType ...Algorithm) (checksums map[Algorithm]string, err error) { |
| 66 | + file, err := os.Open(filePath) |
| 67 | + if err != nil { |
| 68 | + return |
| 69 | + } |
| 70 | + defer ioutils.Close(file, &err) |
| 71 | + return CalcChecksums(file, checksumType...) |
| 72 | +} |
| 73 | + |
| 74 | +// CalcChecksums calculates all hashes at once using AsyncMultiWriter. The file is therefore read only once. |
| 75 | +func CalcChecksums(reader io.Reader, checksumType ...Algorithm) (map[Algorithm]string, error) { |
| 76 | + hashes, err := calcChecksums(reader, checksumType...) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + results := sumResults(hashes) |
| 81 | + return results, nil |
| 82 | +} |
| 83 | + |
| 84 | +// CalcChecksumsBytes calculates hashes like `CalcChecksums`, returns result as bytes |
| 85 | +func CalcChecksumsBytes(reader io.Reader, checksumType ...Algorithm) (map[Algorithm][]byte, error) { |
| 86 | + hashes, err := calcChecksums(reader, checksumType...) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + results := sumResultsBytes(hashes) |
| 91 | + return results, nil |
| 92 | +} |
| 93 | + |
| 94 | +func calcChecksums(reader io.Reader, checksumType ...Algorithm) (map[Algorithm]hash.Hash, error) { |
| 95 | + hashes := getChecksumByAlgorithm(checksumType...) |
| 96 | + var multiWriter io.Writer |
| 97 | + pageSize := os.Getpagesize() |
| 98 | + sizedReader := bufio.NewReaderSize(reader, pageSize) |
| 99 | + var hashWriter []io.Writer |
| 100 | + for _, v := range hashes { |
| 101 | + hashWriter = append(hashWriter, v) |
| 102 | + } |
| 103 | + multiWriter = ioutils.AsyncMultiWriter(pageSize, hashWriter...) |
| 104 | + _, err := io.Copy(multiWriter, sizedReader) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + return hashes, nil |
| 109 | +} |
| 110 | + |
| 111 | +func sumResults(hashes map[Algorithm]hash.Hash) map[Algorithm]string { |
| 112 | + results := map[Algorithm]string{} |
| 113 | + for k, v := range hashes { |
| 114 | + results[k] = fmt.Sprintf("%x", v.Sum(nil)) |
| 115 | + } |
| 116 | + return results |
| 117 | +} |
| 118 | + |
| 119 | +func sumResultsBytes(hashes map[Algorithm]hash.Hash) map[Algorithm][]byte { |
| 120 | + results := map[Algorithm][]byte{} |
| 121 | + for k, v := range hashes { |
| 122 | + results[k] = v.Sum(nil) |
| 123 | + } |
| 124 | + return results |
| 125 | +} |
| 126 | + |
| 127 | +func getChecksumByAlgorithm(checksumType ...Algorithm) map[Algorithm]hash.Hash { |
| 128 | + hashes := map[Algorithm]hash.Hash{} |
| 129 | + if len(checksumType) == 0 { |
| 130 | + for k, v := range algorithmFunc { |
| 131 | + hashes[k] = v() |
| 132 | + } |
| 133 | + return hashes |
| 134 | + } |
| 135 | + |
| 136 | + for _, v := range checksumType { |
| 137 | + hashes[v] = algorithmFunc[v]() |
| 138 | + } |
| 139 | + return hashes |
| 140 | +} |
| 141 | + |
| 142 | +func CalcChecksumDetails(filePath string) (checksum Checksum, err error) { |
| 143 | + file, err := os.Open(filePath) |
| 144 | + if err != nil { |
| 145 | + return |
| 146 | + } |
| 147 | + defer ioutils.Close(file, &err) |
| 148 | + |
| 149 | + checksums, err := CalcChecksums(file) |
| 150 | + if err != nil { |
| 151 | + return Checksum{}, err |
| 152 | + } |
| 153 | + checksum = Checksum{Md5: checksums[MD5], Sha1: checksums[SHA1], Sha256: checksums[SHA256]} |
| 154 | + return |
| 155 | +} |
| 156 | + |
| 157 | +type FileDetails struct { |
| 158 | + Checksum Checksum |
| 159 | + Size int64 |
| 160 | +} |
| 161 | + |
| 162 | +func GetFileDetails(filePath string, includeChecksums bool) (details *FileDetails, err error) { |
| 163 | + details = new(FileDetails) |
| 164 | + if includeChecksums { |
| 165 | + details.Checksum, err = CalcChecksumDetails(filePath) |
| 166 | + if err != nil { |
| 167 | + return |
| 168 | + } |
| 169 | + } else { |
| 170 | + details.Checksum = Checksum{} |
| 171 | + } |
| 172 | + |
| 173 | + fileInfo, err := os.Stat(filePath) |
| 174 | + if err != nil { |
| 175 | + return |
| 176 | + } |
| 177 | + details.Size = fileInfo.Size() |
| 178 | + return |
| 179 | +} |
0 commit comments