Add IsThisALinkData(data) (bool, error) func to communication package

This commit is contained in:
qowevisa 2024-07-12 20:33:22 +03:00
parent 02b56273e2
commit 94cfec15f0

View File

@ -5,6 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base32" "encoding/base32"
"encoding/gob" "encoding/gob"
"errors"
"git.qowevisa.me/Qowevisa/gotell/gmyerr" "git.qowevisa.me/Qowevisa/gotell/gmyerr"
) )
@ -158,6 +159,18 @@ func (r *RegisteredUser) GenerateLink(count uint16) (Link, error) {
return l, nil return l, nil
} }
func IsThisALinkData(data string) (bool, error) {
dst := make([]byte, len([]byte(data)))
n, err := base32.StdEncoding.Decode(dst, []byte(data))
if err != nil {
return false, err
}
if n != LINK_LEN_V1 {
return false, errors.New("Link len is not standard")
}
return true, nil
}
func (l *Link) Bytes() ([]byte, error) { func (l *Link) Bytes() ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
encoder := gob.NewEncoder(&buf) encoder := gob.NewEncoder(&buf)