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

38 lines
747 B
Go
Raw Normal View History

2022-09-02 20:54:11 +02:00
package examples
2017-07-04 14:17:20 +02:00
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
2019-01-18 13:32:19 +02:00
"log"
2017-07-04 14:17:20 +02:00
)
2022-09-02 20:54:11 +02:00
const (
key = "943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881"
salt = "520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5"
)
2017-07-04 14:17:20 +02:00
2022-09-02 20:54:11 +02:00
func SignURL() {
2017-07-04 14:17:20 +02:00
var keyBin, saltBin []byte
var err error
if keyBin, err = hex.DecodeString(key); err != nil {
2022-09-02 20:54:11 +02:00
log.Fatal(err)
2017-07-04 14:17:20 +02:00
}
if saltBin, err = hex.DecodeString(salt); err != nil {
2022-09-02 20:54:11 +02:00
log.Fatal(err)
2017-07-04 14:17:20 +02:00
}
2022-09-02 20:54:11 +02:00
path := "/rs:fit:300:300/plain/http://img.example.com/pretty/image.jpg"
2017-07-04 14:17:20 +02:00
mac := hmac.New(sha256.New, keyBin)
mac.Write(saltBin)
mac.Write([]byte(path))
signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
fmt.Printf("/%s%s", signature, path)
}