Golang encoding-hex.Encode类(方法)实例源码

下面列出了Golang encoding-hex.Encode 类(方法)源码代码实例,从而了解它的用法。

作者:40    项目:bootkub   
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
}

作者:Rudlof    项目:platfor   
// 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[:])
}

作者:CowLe    项目:vites   
// 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)
}

作者:yangzhao2    项目:go.uui   
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)
}

作者:quixote    项目:vaul   
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:])
}

作者:deepzz    项目:go-co   
// 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)
}

作者:chanxuehon    项目:uui   
// 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
}

作者:karlsegui    项目:n   
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)
}

作者:essentialkao    项目:e   
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)
}

作者:jingwen    项目:jqpla   
// 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
}

作者:springCa    项目:go_util   
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)
}

作者:ky    项目:compas   
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
	}
}

作者:agoravotin    项目:agora-http-g   
// 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
}

作者:bmbstac    项目:go-use   
// 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)
}

作者:bmbstac    项目:go-use   
// 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
}

作者:jchri    项目:indexin   
func main() {
	var err error

	argParse()
	codec := collatejson.NewCodec(100)
	out := make([]byte, 0, len(options.inp)*3+collatejson.MinBufferSize)
	if options.encode {
		out, err = codec.Encode([]byte(options.inp), out)
		if err != nil {
			log.Fatal(err)
		}
		hexout := make([]byte, len(out)*5)
		n := hex.Encode(hexout, out)
		fmt.Printf("in : %q\n", options.inp)
		fmt.Printf("out: %q\n", string(out))
		fmt.Printf("hex: %q\n", string(hexout[:n]))

	} else if options.decode {
		inpbs := make([]byte, len(options.inp)*5)
		n, err := hex.Decode(inpbs, []byte(options.inp))
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(n, inpbs[:n])
		out, err = codec.Decode([]byte(inpbs[:n]), out)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("in : %q\n", options.inp)
		fmt.Printf("out: %q\n", string(out))
	}
}

作者:GOCM    项目:wecha   
// 校验消息是否是从微信服务器发送过来的.
// 使用 buf 能提高一点性能 和 减少一些对 GC 的压力, buf 的长度最好 >=128
func _CheckSignature(signature, timestamp, nonce, token string, buf []byte) bool {
	const hashsumLen = 40 // sha1

	if len(signature) != hashsumLen {
		return false
	}

	bufLen := hashsumLen + len(timestamp) + len(nonce) + len(token)
	if len(buf) < bufLen {
		buf = make([]byte, hashsumLen, bufLen)
	} else {
		buf = buf[:hashsumLen]
	}

	strArray := sort.StringSlice{token, timestamp, nonce}
	strArray.Sort()

	buf = append(buf, strArray[0]...)
	buf = append(buf, strArray[1]...)
	buf = append(buf, strArray[2]...)

	hashsumArray := sha1.Sum(buf[hashsumLen:]) // require go1.2+

	hashsumHexBytes := buf[:hashsumLen]
	hex.Encode(hashsumHexBytes, hashsumArray[:])

	// 采用 subtle.ConstantTimeCompare 是防止 计时攻击!
	if rslt := subtle.ConstantTimeCompare(hashsumHexBytes, []byte(signature)); rslt == 1 {
		return true
	}
	return false
}

作者:bmatsu    项目:re   
// MarshalJSON implements json.Marshaller.
func (t *Time) MarshalJSON() ([]byte, error) {
	var x [18]byte
	x[0] = '"'
	hex.Encode(x[1:17], t.Bytes())
	x[17] = '"'
	return x[:], nil
}

作者:ancientlor    项目:hashsr   
func (e *Engine) hex() error {
	b := e.stack.Pop()
	enc := make([]byte, hex.EncodedLen(len(b)))
	hex.Encode(enc, b)
	e.stack.Push(enc)
	return nil
}

作者:rj    项目:hubroulett   
func signature(body string) string {
	dst := make([]byte, 40)
	computed := hmac.New(sha1.New, []byte(testSecret))
	computed.Write([]byte(body))
	hex.Encode(dst, computed.Sum(nil))
	return "sha1=" + string(dst)
}


问题


面经


文章

微信
公众号

扫码关注公众号