Files
ipe/utils/utils.go
T
claudemiro 1de947cedc The random Seed should be executed only once.
If two users connect to the system in the same second both would receive the same id. This fix this error.
2016-01-18 00:37:55 -02:00

46 lines
979 B
Go

// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package utils
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"math/rand"
"regexp"
)
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
func HashMAC(message, key []byte) string {
mac := hmac.New(sha256.New, key)
mac.Write(message)
expected := mac.Sum(nil)
return hex.EncodeToString(expected)
}
// GenerateSessionID Generate a new random Hash
func GenerateSessionID() string {
MAX := 999999999
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
}
// IsChannelNameValid Verify if the channel name is valid
func IsChannelNameValid(channelName string) bool {
matched, err := regexp.MatchString("^[A-Za-z0-9_\\-=@,.;]+$", channelName)
if err != nil {
return false
}
if matched {
return true
}
return false
}