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

Former-commit-id: f0de208a4ae4cd0df9056443fe7a6a823882eca2 [formerly 3c65bde6f2dede145eca8f0d7c10a4d8124a01ef] [formerly 97100fe7d36d445babbc13a669d9c75577cb02d7 [formerly 6e04c97bb9d3e689e583f347588740d2d214e62b]] Former-commit-id: 30f9afb795307f67cd9bccd2f35742a21ee69fde [formerly bc8a04744ca91ad2f951f0888cc11b023d6a067a] Former-commit-id: 7397ffff5d09a3a56b29f7119d696ab4a4507893
51 lines
899 B
Go
51 lines
899 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(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
query := r.URL.Query().Get("checksum")
|
|
|
|
file, err := os.Open(c.fi.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
|
|
}
|