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

38 lines
747 B
Go

package examples
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
)
const (
key = "943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881"
salt = "520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5"
)
func SignURL() {
var keyBin, saltBin []byte
var err error
if keyBin, err = hex.DecodeString(key); err != nil {
log.Fatal(err)
}
if saltBin, err = hex.DecodeString(salt); err != nil {
log.Fatal(err)
}
path := "/rs:fit:300:300/plain/http://img.example.com/pretty/image.jpg"
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)
}