mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-13 00:33:41 +08:00
feat: add example
This commit is contained in:
34
examples/hmac.go
Normal file
34
examples/hmac.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func GenerateSecretKey() (string, error) {
|
||||
// Create a byte slice to hold 32 random bytes
|
||||
key := make([]byte, 32)
|
||||
|
||||
// Fill the slice with secure random bytes
|
||||
_, err := rand.Read(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Encode the byte slice to a Base64 string
|
||||
secretKey := base64.StdEncoding.EncodeToString(key)
|
||||
|
||||
// Return the first 32 characters
|
||||
return secretKey[:32], nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
secretKey, err := GenerateSecretKey()
|
||||
if err != nil {
|
||||
log.Fatalf("Error generating secret key: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Generated Secret Key:", secretKey)
|
||||
}
|
Reference in New Issue
Block a user