代码拉取完成,页面将自动刷新
//Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//Licensed under the Universal Permissive License (UPL) Version 1.0 as shown at http://oss.oracle.com/licenses/upl.
package suid
import (
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"os"
"regexp"
"strings"
"sync/atomic"
"time"
log "github.com/sirupsen/logrus"
)
// SUID is the type of Speedle Unique ID
type SUID []byte
var sequenceID uint32
var hostname = getHostname()
var hashAlg = sha1.New()
// New returns a new SUID instance
func New() SUID {
// Get current timestamp
tb := make([]byte, 8)
binary.BigEndian.PutUint64(tb[:], uint64(time.Now().UnixNano()))
pidb := make([]byte, 4)
binary.BigEndian.PutUint32(pidb[:], uint32(os.Getpid()))
seqb := make([]byte, 4)
binary.BigEndian.PutUint32(seqb[:], atomic.AddUint32(&sequenceID, 1))
hashAlg.Write(tb[:])
hashAlg.Write(hostname)
hashAlg.Write(pidb[:])
hashAlg.Write(seqb[:])
ret := make([]byte, 13)
copy(ret[:], hashAlg.Sum(nil))
return ret
}
func (s SUID) String() string {
// Omit last char
return strings.ToLower(base32.StdEncoding.EncodeToString(s)[:20])
}
func getHostname() []byte {
hostName, err := os.Hostname()
if err != nil {
// This error should not happen
panic(err)
}
return []byte(hostName)
}
// ParseSUID extracts the first SUID from the src string
func ParseSUID(src string) string {
if len(src) == 0 {
return ""
}
re := regexp.MustCompile(`([A-Za-z0-9]{20})`)
match := re.FindStringSubmatch(src)
if nil != match {
return match[0]
}
log.Errorf("Failed to parse SUID from %s", src)
return src
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。