package utils import ( "crypto/ed25519" "crypto/rand" "crypto/sha256" "encoding/base64" "fmt" "log" "git.nutfactory.org/hoernschen/Matrix/config" ) func CreateToken() (err error, token string) { b := make([]byte, 8) _, err = rand.Read(b) if err != nil { log.Fatal(err) return } token = fmt.Sprintf("%x", b) return } func CreateUUID() (err error, uuid string) { b := make([]byte, 16) _, err = rand.Read(b) if err != nil { log.Fatal(err) return } uuid = fmt.Sprintf("%x_%x_%x_%x_%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) return } func Hash(s []byte) (err error, hashString string) { h := sha256.New() _, err = h.Write(s) if nil != err { return } hash := h.Sum(nil) hashString = base64.StdEncoding.EncodeToString(hash) return } func GenerateKeyPair() (publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey, err error) { publicKey, privateKey, err = ed25519.GenerateKey(nil) return } func Sign(message []byte) []byte { return ed25519.Sign(config.PrivateKey, message) } func SignContent(content []byte) (signatures map[string]map[string]string) { signatures = make(map[string]map[string]string) signatures[config.Homeserver] = make(map[string]string) signatures[config.Homeserver][config.KeyId] = string(Sign(content)) return } func VerifySignature(publicKey []byte, message []byte, signature []byte) bool { return ed25519.Verify(publicKey, message, signature) }