/* * Copyright 2020 The Dragonfly Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package digestutils import ( "bufio" "crypto/md5" "crypto/sha256" "encoding/hex" "hash" "io" "os" "strings" "d7y.io/dragonfly/v2/internal/constants" "d7y.io/dragonfly/v2/pkg/unit" "d7y.io/dragonfly/v2/pkg/util/fileutils" ) func Sha256(values ...string) string { if len(values) == 0 { return "" } h := sha256.New() for _, content := range values { if _, err := h.Write([]byte(content)); err != nil { return "" } } return ToHashString(h) } func Md5Reader(reader io.Reader) string { h := md5.New() if _, err := io.Copy(h, reader); err != nil { return "" } return ToHashString(h) } func Md5Bytes(bytes []byte) string { h := md5.New() h.Write(bytes) return ToHashString(h) } // HashFile computes hash value corresponding to hashType, // hashType is from constants.Md5Hash and constants.Sha256Hash. func HashFile(file string, hashType string) string { if !fileutils.IsRegular(file) { return "" } f, err := os.Open(file) if err != nil { return "" } defer f.Close() var h hash.Hash if hashType == constants.Md5Hash { h = md5.New() } else if hashType == constants.Sha256Hash { h = sha256.New() } else { return "" } r := bufio.NewReaderSize(f, int(4*unit.MB)) _, err = io.Copy(h, r) if err != nil { return "" } return ToHashString(h) } func ToHashString(h hash.Hash) string { return hex.EncodeToString(h.Sum(nil)) } func Parse(digest string) []string { digest = strings.Trim(digest, " ") return strings.Split(digest, ":") }