mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 11:42:57 +00:00

Former-commit-id: 6f9843613e6abfe0b19e6e43f9299afb98645477 [formerly 805fa39b073401c946b559e0e967fdc553f16295] [formerly f9e2de337abc0cbeb5ccdb8b6962871b0bd3ee0e [formerly 1d26b8e95e73a94ba92673861c4e83dfded0d92e]] Former-commit-id: 259a1ba8cdc71e7d32add58f9487e5827aa6685e [formerly 161a1a49bc1158f11f07fc9ff638506dafb6c918] Former-commit-id: 67fe1fef4602baf4ddae40b28e65ac9a2a42ca9d
51 lines
905 B
Go
51 lines
905 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(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
query := r.URL.Query().Get("checksum")
|
|
|
|
file, err := os.Open(ctx.Info.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
|
|
}
|