mirror of
https://github.com/pikami/cosmium.git
synced 2024-11-25 15:07:35 +00:00
27 lines
692 B
Go
27 lines
692 B
Go
package authentication
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
|
|
func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string {
|
|
payload := fmt.Sprintf(
|
|
"%s\n%s\n%s\n%s\n%s\n",
|
|
strings.ToLower(verb),
|
|
strings.ToLower(resourceType),
|
|
resourceId,
|
|
strings.ToLower(date),
|
|
"")
|
|
|
|
masterKeyBytes, _ := base64.StdEncoding.DecodeString(masterKey)
|
|
hash := hmac.New(sha256.New, masterKeyBytes)
|
|
hash.Write([]byte(payload))
|
|
signature := base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
|
return signature
|
|
}
|