1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/examples/signature.go

46 lines
1.0 KiB
Go
Raw Normal View History

2017-07-04 14:17:20 +02:00
package main
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
)
func main() {
key := "943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881"
salt := "520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5"
var keyBin, saltBin []byte
var err error
if keyBin, err = hex.DecodeString(key); err != nil {
2019-01-18 13:32:19 +02:00
log.Fatal("Key expected to be hex-encoded string")
2017-07-04 14:17:20 +02:00
}
if saltBin, err = hex.DecodeString(salt); err != nil {
2019-01-18 13:32:19 +02:00
log.Fatal("Salt expected to be hex-encoded string")
2017-07-04 14:17:20 +02:00
}
resize := "fill"
width := 300
height := 300
gravity := "no"
enlarge := 1
extension := "png"
url := "http://img.example.com/pretty/image.jpg"
encodedURL := base64.RawURLEncoding.EncodeToString([]byte(url))
2021-03-15 13:26:38 +02:00
path := fmt.Sprintf("/rs:%s:%d:%d:%d/g:%s/%s.%s", resize, width, height, enlarge, gravity, encodedURL, extension)
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)
}