Files
huso/rechner.go
2022-07-26 20:33:11 +02:00

79 lines
1.7 KiB
Go

package main
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/klauspost/compress/zstd"
)
func Sauce(malid int64, username string) string {
return fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.FormatInt(malid, 10)+username)))
}
func BytesToInt64(bytes []byte) (int64, error) {
yes, c := binary.Varint(bytes)
if c <= 0 {
return yes, fmt.Errorf("int64 decode error: %s", bytes)
}
return yes, nil
}
func Int64ToByte(yes int64) []byte {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, yes)
return buf[:n]
}
func BytesToInt64AndDate(bytes []byte) (int64, time.Time, error) {
split := strings.Split(string(bytes), AppointSplit)
if len(split) != 2 {
return 0, time.Time{}, fmt.Errorf("invalid appointment split %s", string(bytes))
}
num, err := strconv.ParseInt(split[0], 10, 64)
if err != nil {
return num, time.Time{}, err
}
appoint, err := time.Parse(time.RFC3339, split[1])
return num, appoint, err
}
func Int64AndDateToBytes(num int64, appoint time.Time) []byte {
return []byte(fmt.Sprintf("%d%s%s", num, AppointSplit, appoint.Format(time.RFC3339)))
}
func CompressZstd(src []byte) ([]byte, error) {
var buf bytes.Buffer
encoder, err := zstd.NewWriter(&buf)
if err != nil {
return nil, err
}
reader := bytes.NewReader(src)
_, err = io.Copy(encoder, reader)
if err != nil {
encoder.Close()
return nil, err
}
err = encoder.Close()
return buf.Bytes(), err
}
func DecompressZstd(src []byte) ([]byte, error) {
reader := bytes.NewReader(src)
decoder, err := zstd.NewReader(reader)
if err != nil {
return nil, err
}
defer decoder.Close()
var buf bytes.Buffer
_, err = io.Copy(&buf, decoder)
return buf.Bytes(), err
}