go-sendxmpp/helpers.go
2022-04-23 23:40:15 +02:00

62 lines
1.1 KiB
Go

// Copyright 2022 Martin Dosch.
// Use of this source code is governed by the BSD-2-clause
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/rand"
"fmt"
"log"
mrand "math/rand"
"os"
"time"
)
func readFile(path string) (*bytes.Buffer, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
buffer := new(bytes.Buffer)
_, err = buffer.ReadFrom(file)
if err != nil {
return nil, err
}
return buffer, nil
}
func getRpad() string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,;?!+-_§$%&/()=")
mrand.Seed(time.Now().UnixNano())
s := make([]rune, mrand.Intn(30)+20)
for i := range s {
s[i] = letters[mrand.Intn(len(letters))]
}
return string(s)
}
func getID() string {
b := make([]byte, 12)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
id := fmt.Sprintf("%x-%x-%x", b[0:4], b[4:8], b[8:])
return id
}
func getShortID() string {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
id := fmt.Sprintf("%x", b[0:4])
return id
}