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

32 lines
579 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 (
2018-10-05 23:01:54 +02:00
errInvalidToken = errors.New("Invalid token")
errInvalidTokenEncoding = errors.New("Invalid token encoding")
2018-10-05 22:29:55 +02:00
)
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 23:01:54 +02:00
return errInvalidTokenEncoding
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
}