本文整理匯總了Golang中encoding/hex.Encode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Encode函數的具體用法?Golang Encode怎麽用?Golang Encode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Encode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: HandleData
func (a authCookieSha1) HandleData(data []byte) ([]byte, AuthStatus) {
challenge := make([]byte, len(data)/2)
_, err := hex.Decode(challenge, data)
if err != nil {
return nil, AuthError
}
b := bytes.Split(challenge, []byte{' '})
if len(b) != 3 {
return nil, AuthError
}
context := b[0]
id := b[1]
svchallenge := b[2]
cookie := a.getCookie(context, id)
if cookie == nil {
return nil, AuthError
}
clchallenge := a.generateChallenge()
if clchallenge == nil {
return nil, AuthError
}
hash := sha1.New()
hash.Write(bytes.Join([][]byte{svchallenge, clchallenge, cookie}, []byte{':'}))
hexhash := make([]byte, 2*hash.Size())
hex.Encode(hexhash, hash.Sum(nil))
data = append(clchallenge, ' ')
data = append(data, hexhash...)
resp := make([]byte, 2*len(data))
hex.Encode(resp, data)
return resp, AuthOk
}
示例2: String
// String returns the UUID in it's canonical form, a 32 digit hexadecimal
// number in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func (u UUID) String() string {
buf := [36]byte{8: '-', 13: '-', 18: '-', 23: '-'}
hex.Encode(buf[0:], u[0:4])
hex.Encode(buf[9:], u[4:6])
hex.Encode(buf[14:], u[6:8])
hex.Encode(buf[19:], u[8:10])
hex.Encode(buf[24:], u[10:])
return string(buf[:])
}
示例3: String
// String prints an SID in the form used by MySQL 5.6.
func (sid SID) String() string {
dst := []byte("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
hex.Encode(dst, sid[:4])
hex.Encode(dst[9:], sid[4:6])
hex.Encode(dst[14:], sid[6:8])
hex.Encode(dst[19:], sid[8:10])
hex.Encode(dst[24:], sid[10:16])
return string(dst)
}
示例4: Hex
func (u UUID) Hex() string {
buf := make([]byte, 32)
hex.Encode(buf[0:8], u[0:4])
hex.Encode(buf[8:12], u[4:6])
hex.Encode(buf[12:16], u[6:8])
hex.Encode(buf[16:20], u[8:10])
hex.Encode(buf[20:], u[10:])
return string(buf)
}
示例5: encodeHex
func encodeHex(dst []byte, uuid UUID) {
hex.Encode(dst[:], uuid[:4])
dst[8] = '-'
hex.Encode(dst[9:13], uuid[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], uuid[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], uuid[8:10])
dst[23] = '-'
hex.Encode(dst[24:], uuid[10:])
}
示例6: String
// Returns canonical string representation of UUID:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func (u UUID) String() string {
buf := make([]byte, 36)
hex.Encode(buf[0:8], u[0:4])
buf[8] = dash
hex.Encode(buf[9:13], u[4:6])
buf[13] = dash
hex.Encode(buf[14:18], u[6:8])
buf[18] = dash
hex.Encode(buf[19:23], u[8:10])
buf[23] = dash
hex.Encode(buf[24:], u[10:])
return string(buf)
}
示例7: Encode
// Encode encodes UUID to "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" format.
func Encode(uuid UUID) []byte {
buf := make([]byte, 36)
hex.Encode(buf[:8], uuid[:4])
buf[8] = '-'
hex.Encode(buf[9:13], uuid[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], uuid[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], uuid[8:10])
buf[23] = '-'
hex.Encode(buf[24:], uuid[10:])
return buf
}
示例8: Guidv4String
func Guidv4String() string {
t := Guidv4()
tmp := make([]byte, 36)
hex.Encode(tmp[:8], t[:4])
tmp[8] = '-'
hex.Encode(tmp[9:], t[4:6])
tmp[13] = '-'
hex.Encode(tmp[14:], t[6:8])
tmp[18] = '-'
hex.Encode(tmp[19:], t[8:10])
tmp[23] = '-'
hex.Encode(tmp[24:], t[10:])
return string(tmp)
}
示例9: toString
func toString(uuid []byte) string {
buf := make([]byte, 36)
hex.Encode(buf[0:8], uuid[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], uuid[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], uuid[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], uuid[8:10])
buf[23] = '-'
hex.Encode(buf[24:], uuid[10:])
return string(buf)
}
示例10: decodeUUIDBinary
// decodeUUIDBinary interprets the binary format of a uuid, returning it in text format.
func decodeUUIDBinary(src []byte) ([]byte, error) {
if len(src) != 16 {
return nil, fmt.Errorf("pq: unable to decode uuid; bad length: %d", len(src))
}
dst := make([]byte, 36)
dst[8], dst[13], dst[18], dst[23] = '-', '-', '-', '-'
hex.Encode(dst[0:], src[0:4])
hex.Encode(dst[9:], src[4:6])
hex.Encode(dst[14:], src[6:8])
hex.Encode(dst[19:], src[8:10])
hex.Encode(dst[24:], src[10:16])
return dst, nil
}
示例11: noUnderLine
func noUnderLine(u uuid.UUID) string {
buf := make([]byte, 36)
hex.Encode(buf[0:8], u[0:4])
//buf[8] = dash
hex.Encode(buf[8:12], u[4:6])
//buf[13] = dash
hex.Encode(buf[12:16], u[6:8])
//buf[18] = dash
hex.Encode(buf[16:20], u[8:10])
//buf[23] = dash
hex.Encode(buf[20:], u[10:])
return string(buf)
}
示例12: Check
func (u *User) Check(adminName, adminPwd string) bool {
sql := "select count(1) as num from `admin` where admin_name = '%s' and admin_pwd = '%s'"
db, err := initDB()
if err != nil {
u.Logger.Fatalf("initDB ERR:%v", err)
}
bin := md5.Sum([]byte(adminPwd))
tmp := make([]byte, 32)
hex.Encode(tmp, bin[:])
rows, err := db.Query(fmt.Sprintf(sql, adminName, string(tmp)))
if err != nil {
u.Logger.Printf("user check err: sql is %s, err is %v", sql, err)
return false
}
if rows.Next() {
var num int
rows.Scan(&num)
if num > 0 {
return true
} else {
return false
}
} else {
return false
}
}
示例13: GenerateMAC
// Generates an HMAC using SHA256
func GenerateMAC(message, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(message)
ret := make([]byte, 64)
hex.Encode(ret, mac.Sum(nil))
return ret
}
示例14: Decode
// trim(url_base64(json(token))) + "." + hex(hmac-sha256(base64_str))
func (token *Token) Decode(tokenBytes []byte) error {
const signatureLen = 64 // hmac-sha256
bytesArray := bytes.Split(tokenBytes, tokenBytesSplitSep)
if len(bytesArray) < 2 {
return errors.New("invalid token bytes")
}
// 驗證簽名
signatrue := make([]byte, signatureLen)
Hash := hmac.New(sha256.New, securitykey.Key)
Hash.Write(bytesArray[0])
hex.Encode(signatrue, Hash.Sum(nil))
if !bytes.Equal(signatrue, bytesArray[1]) {
return errors.New("invalid token bytes, signature mismatch")
}
// 解碼
temp := signatrue[:4] // signatrue 不再使用, 利用其空間
copy(temp, tokenBytes[len(bytesArray[0]):]) // 保護 tokenBytes
defer func() {
copy(tokenBytes[len(bytesArray[0]):], temp) // 恢複 tokenBytes
token.Signatrue = string(bytesArray[1])
}()
base64Bytes := base64Pad(bytesArray[0])
base64Decoder := base64.NewDecoder(base64.URLEncoding, bytes.NewReader(base64Bytes))
return json.NewDecoder(base64Decoder).Decode(token)
}
示例15: Encode
// trim(url_base64(json(token))) + "." + hex(hmac-sha256(base64_str))
func (token *Token) Encode() ([]byte, error) {
const signatureLen = 64 // hmac-sha256
jsonBytes, err := json.Marshal(token)
if err != nil {
return nil, err
}
base64BytesLen := base64.URLEncoding.EncodedLen(len(jsonBytes))
buf := make([]byte, base64BytesLen+1+signatureLen)
base64Bytes := buf[:base64BytesLen]
base64.URLEncoding.Encode(base64Bytes, jsonBytes)
// 去掉 base64 編碼尾部的 '='
base64Bytes = base64Trim(base64Bytes)
base64BytesLen = len(base64Bytes)
signatureOffset := base64BytesLen + 1
buf = buf[:signatureOffset+signatureLen]
buf[base64BytesLen] = '.'
signature := buf[signatureOffset:]
Hash := hmac.New(sha256.New, securitykey.Key)
Hash.Write(base64Bytes)
hex.Encode(signature, Hash.Sum(nil))
token.Signatrue = string(signature)
return buf, nil
}