1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-07-12 23:00:55 +02:00
Files
imgproxy/crypt.go

32 lines
573 B
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
)
2018-10-06 02:29:55 +06:00
var (
errInvalidToken = errors.New("Invalid token")
errInvalidURLEncoding = errors.New("Invalid token encoding")
)
2017-06-20 16:58:55 +03:00
func validatePath(token, path string) error {
messageMAC, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
2018-10-06 02:29:55 +06:00
return errInvalidURLEncoding
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
mac := hmac.New(sha256.New, conf.Key)
mac.Write(conf.Salt)
2017-06-20 16:58:55 +03:00
mac.Write([]byte(path))
expectedMAC := mac.Sum(nil)
if !hmac.Equal(messageMAC, expectedMAC) {
2018-10-06 02:29:55 +06:00
return errInvalidToken
2017-06-20 16:58:55 +03:00
}
return nil
}