add auth package to hash requests

This commit is contained in:
Tevildo 2024-04-14 21:07:24 +01:00
parent e57412778d
commit b1104cc004
No known key found for this signature in database
GPG key ID: 780413157E8C9E35

57
auth/auth.go Normal file
View file

@ -0,0 +1,57 @@
package auth
import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
"time"
)
var (
saltChars = "0123456789ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz"
baseUrl = "https://api.nearlyfreespeech.net"
authHeaderName = "X-NFSN-Authentication"
maxSalt big.Int
)
func init() {
maxSalt.SetInt64(int64(len(saltChars)))
}
func Salt() (string, error) {
salt := make([]string, 16)
for i := 0; i < 16; i++ {
rand_index, err := rand.Int(rand.Reader, &maxSalt)
if err != nil {
return "", err
}
salt[i] = string(saltChars[rand_index.Int64()])
}
return strings.Join(salt, ""), nil
}
func Timestamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
func ComputeHash(data string) string {
result := sha1.Sum([]byte(data))
return hex.EncodeToString(result[:])
}
func GetAuthHeader(login string, apiKey string, url string, body string) (string, error) {
timestamp := Timestamp()
salt, err := Salt()
if err != nil {
return "", err
}
bodyHash := ComputeHash(body)
hash := ComputeHash(fmt.Sprintf("%s;%s;%s;%s;%s;%s", login, timestamp, salt, apiKey, url, bodyHash))
return hash, nil
}