mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 03:32:56 +00:00

Former-commit-id: b52f339d7a13e12d6e7d194a4e5ce4dabae7b61c [formerly 8ee8ebe7d1133b8ca7c46854a31a0a711b5579fc] [formerly 05100c5f169e072f21204537f4d8ddaf2f821377 [formerly 3c821a541373fc8e6b0323bb6abe5a0447931694]] Former-commit-id: e5d5c4ac1d7518d8032ace12bf5efe26d0502a6b [formerly ea15d87d509cc85d2865d9a249acd15975dfa43d] Former-commit-id: 08848510019b280a91a61156e88f5b914d4f8d88
51 lines
890 B
Go
51 lines
890 B
Go
package filemanager
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
e "errors"
|
|
"hash"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// checksum calculates the hash of a file. Supports MD5, SHA1, SHA256 and SHA512.
|
|
func checksum(w http.ResponseWriter, r *http.Request, i *fileInfo) (int, error) {
|
|
query := r.URL.Query().Get("checksum")
|
|
|
|
file, err := os.Open(i.Path)
|
|
if err != nil {
|
|
return errorToHTTP(err, true), err
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
var h hash.Hash
|
|
|
|
switch query {
|
|
case "md5":
|
|
h = md5.New()
|
|
case "sha1":
|
|
h = sha1.New()
|
|
case "sha256":
|
|
h = sha256.New()
|
|
case "sha512":
|
|
h = sha512.New()
|
|
default:
|
|
return http.StatusBadRequest, e.New("Unknown HASH type")
|
|
}
|
|
|
|
_, err = io.Copy(h, file)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
val := hex.EncodeToString(h.Sum(nil))
|
|
w.Write([]byte(val))
|
|
return http.StatusOK, nil
|
|
}
|