go-sendxmpp/helpers.go

56 lines
995 B
Go
Raw Normal View History

2022-04-09 03:00:38 +00:00
// 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"
2022-04-09 03:00:38 +00:00
"crypto/rand"
"fmt"
"log"
"os"
2022-04-09 03:00:38 +00:00
)
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(messageLength int) string {
length := 100 - messageLength%100
2022-05-02 13:07:58 +00:00
rpad := make([]byte, length)
_, err := rand.Read(rpad)
if err != nil {
log.Fatal(err)
}
2022-05-02 13:07:58 +00:00
return fmt.Sprintf("%x", rpad)
}
2022-04-09 03:00:38 +00:00
func getID() string {
2022-05-02 14:13:00 +00:00
id := make([]byte, 12)
_, err := rand.Read(id)
2022-04-09 03:00:38 +00:00
if err != nil {
log.Fatal(err)
}
2022-05-02 14:13:00 +00:00
return fmt.Sprintf("%x-%x-%x", id[0:4], id[4:8], id[8:])
2022-04-09 03:00:38 +00:00
}
func getShortID() string {
2022-05-02 14:13:00 +00:00
id := make([]byte, 4)
_, err := rand.Read(id)
if err != nil {
log.Fatal(err)
}
2022-05-02 14:13:00 +00:00
return fmt.Sprintf("%x", id[0:4])
}