1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/crypt.go

32 lines
573 B
Go
Raw Normal View History

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