github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/util/etag.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package util
     5  
     6  import (
     7  	"crypto/md5"
     8  	"encoding/hex"
     9  	"io"
    10  	"os"
    11  )
    12  
    13  // ComputeEtag returns etag for a file at path
    14  func ComputeEtag(path string) (string, error) {
    15  	var result []byte
    16  	file, err := os.Open(path)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	defer Close(file)
    21  
    22  	hash := md5.New()
    23  	if _, err := io.Copy(hash, file); err != nil {
    24  		return "", err
    25  	}
    26  
    27  	return hex.EncodeToString(hash.Sum(result)), nil
    28  }