transx/utils/tunnelid.go

24 lines
406 B
Go

package utils
import (
"bytes"
"crypto/md5"
"encoding/hex"
"strconv"
"sync/atomic"
"time"
)
var seed int32
func init() {
seed = 0
}
func TunnelID() string {
nowString := time.Now().String() + strconv.Itoa(int(seed))
atomic.AddInt32(&seed, 1) //避免多线程情况下获得的种子相同
md5Byte := md5.Sum(bytes.NewBufferString(nowString).Bytes())
return hex.EncodeToString(md5Byte[:])
}