1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/crypt.go
printercu 7aae98e087 Option to use truncated signature (#88)
* CircleCI

* Fix docs resize_type -> resizing_type (#91)

As noted in my issue...

* Option to use truncated signature

* Don't use 0 as no-limit for SignatureSize
2018-11-02 17:10:20 +06:00

38 lines
721 B
Go

package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
)
var (
errInvalidToken = errors.New("Invalid token")
errInvalidTokenEncoding = errors.New("Invalid token encoding")
)
func validatePath(token, path string) error {
messageMAC, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return errInvalidTokenEncoding
}
if !hmac.Equal(messageMAC, signatureFor(path)) {
return errInvalidToken
}
return nil
}
func signatureFor(str string) []byte {
mac := hmac.New(sha256.New, conf.Key)
mac.Write(conf.Salt)
mac.Write([]byte(str))
expectedMAC := mac.Sum(nil)
if conf.SignatureSize < 32 {
return expectedMAC[:conf.SignatureSize]
}
return expectedMAC
}