本文整理汇总了Golang中encoding/hex.EncodedLen函数的典型用法代码示例。如果您正苦于以下问题:Golang EncodedLen函数的具体用法?Golang EncodedLen怎么用?Golang EncodedLen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EncodedLen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: packRequest
func (c *conn) packRequest(r *http.Request) (*http.Request, error) {
buf := &bytes.Buffer{}
zbuf, err := zlib.NewWriterLevel(buf, zlib.BestCompression)
if err != nil {
return nil, fmt.Errorf("conn.packRequest(zlib.NewWriterLevel)>%s", err)
}
url := c.url + r.URL.String()
urlhex := make([]byte, hex.EncodedLen(len(url)))
hex.Encode(urlhex, []byte(url))
fmt.Fprintf(zbuf, "url=%s", urlhex)
fmt.Fprintf(zbuf, "&method=%s", hex.EncodeToString([]byte(r.Method)))
if c.ps.password != "" {
fmt.Fprintf(zbuf, "&password=%s", c.ps.password)
}
fmt.Fprint(zbuf, "&headers=")
for k, v := range r.Header {
fmt.Fprint(zbuf, hex.EncodeToString([]byte(fmt.Sprintf("%s:%s\r\n", k, v[0]))))
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("conn.packRequest(ioutil.ReadAll(r.Body))>%s", err)
}
payload := hex.EncodeToString(body)
fmt.Fprintf(zbuf, "&payload=%s", payload)
zbuf.Close()
req, err := http.NewRequest("POST", c.ps.path, buf)
if err != nil {
return nil, fmt.Errorf("conn.packRequest(http.NewRequest)>%s", err)
}
req.Host = c.ps.appid[rand.Intn(len(c.ps.appid))] + ".appspot.com"
req.URL.Scheme = "http"
return req, nil
}
示例2: GetSHA256
// Get the corresponding ID, which is the (hex encoded) SHA256 of the (base64 encoded) public key.
func (pk PublicKey) GetSHA256() []byte {
h := sha256.New()
h.Write([]byte(pk.String()))
sha256hex := make([]byte, hex.EncodedLen(sha256.Size))
hex.Encode(sha256hex, h.Sum(nil))
return sha256hex
}
示例3: newSID
func newSID() string {
b := make([]byte, 8)
rand.Read(b)
d := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(d, b)
return string(d)
}
示例4: GetSHA256
// Get the (hex-encoded) SHA256 of the String value of the ballot.
func (ballot *Ballot) GetSHA256() []byte {
h := sha256.New()
h.Write([]byte(ballot.String()))
sha256hex := make([]byte, hex.EncodedLen(sha256.Size))
hex.Encode(sha256hex, h.Sum(nil))
return sha256hex
}
示例5: Value
// Value implements the driver.Valuer interface. It uses the "hex" format which
// is only supported on PostgreSQL 9.0 or newer.
func (a ByteaArray) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
if n := len(a); n > 0 {
// There will be at least two curly brackets, 2*N bytes of quotes,
// 3*N bytes of hex formatting, and N-1 bytes of delimiters.
size := 1 + 6*n
for _, x := range a {
size += hex.EncodedLen(len(x))
}
b := make([]byte, size)
for i, s := 0, b; i < n; i++ {
o := copy(s, `,"\\x`)
o += hex.Encode(s[o:], a[i])
s[o] = '"'
s = s[o+1:]
}
b[0] = '{'
b[size-1] = '}'
return string(b), nil
}
return "{}", nil
}
示例6: Sign
// Sign 微信支付签名.
// params: 待签名的参数集合
// apiKey: api密钥
// fn: func() hash.Hash, 如果为 nil 则默认用 md5.New
func Sign(params map[string]string, apiKey string, fn func() hash.Hash) string {
if fn == nil {
fn = md5.New
}
h := fn()
bufw := bufio.NewWriterSize(h, 128)
keys := make([]string, 0, len(params))
for k := range params {
if k == "sign" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := params[k]
if v == "" {
continue
}
bufw.WriteString(k)
bufw.WriteByte('=')
bufw.WriteString(v)
bufw.WriteByte('&')
}
bufw.WriteString("key=")
bufw.WriteString(apiKey)
bufw.Flush()
signature := make([]byte, hex.EncodedLen(h.Size()))
hex.Encode(signature, h.Sum(nil))
return string(bytes.ToUpper(signature))
}
示例7: Sign2
// 传统的签名代码, Sign 是优化后的代码, 要提高 30% 的速度
func Sign2(params map[string]string, apiKey string, fn func() hash.Hash) string {
if fn == nil {
fn = md5.New
}
h := fn()
keys := make([]string, 0, len(params))
for k := range params {
if k == "sign" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := params[k]
if v == "" {
continue
}
h.Write([]byte(k))
h.Write([]byte{'='})
h.Write([]byte(v))
h.Write([]byte{'&'})
}
h.Write([]byte("key="))
h.Write([]byte(apiKey))
signature := make([]byte, hex.EncodedLen(h.Size()))
hex.Encode(signature, h.Sum(nil))
return string(bytes.ToUpper(signature))
}
示例8: ToWireMsg
// ToWireMsg translates a ComposedMsg into a multipart ZMQ message ready to send, and
// signs it. This does not add the return identities or the delimiter.
func (msg ComposedMsg) ToWireMsg(signkey []byte) (msgparts [][]byte) {
msgparts = make([][]byte, 5)
header, _ := json.Marshal(msg.Header)
msgparts[1] = header
parent_header, _ := json.Marshal(msg.Parent_header)
msgparts[2] = parent_header
if msg.Metadata == nil {
msg.Metadata = make(map[string]interface{})
}
metadata, _ := json.Marshal(msg.Metadata)
msgparts[3] = metadata
content, _ := json.Marshal(msg.Content)
msgparts[4] = content
// Sign the message
if len(signkey) != 0 {
mac := hmac.New(sha256.New, signkey)
for _, msgpart := range msgparts[1:] {
mac.Write(msgpart)
}
msgparts[0] = make([]byte, hex.EncodedLen(mac.Size()))
hex.Encode(msgparts[0], mac.Sum(nil))
}
return
}
示例9: compareMAC
// compareMAC reports whether expectedMAC is a valid HMAC tag for message.
func compareMAC(message, expectedMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
messageMAC := make([]byte, hex.EncodedLen(mac.Size()))
hex.Encode(messageMAC, mac.Sum(nil))
return subtle.ConstantTimeCompare(messageMAC, expectedMAC) == 1
}
示例10: MarshalJSON
// MarshalJSON allows the representation in JSON of hexbytes
func (b HexBytes) MarshalJSON() ([]byte, error) {
res := make([]byte, hex.EncodedLen(len(b))+2)
res[0] = '"'
res[len(res)-1] = '"'
hex.Encode(res[1:], b)
return res, nil
}
示例11: UnmarshalJSON
func (ref *Ref) UnmarshalJSON(data []byte) error {
if len(data) != hex.EncodedLen(RefLen)+2 {
return errors.New("Ref of wrong length")
}
_, err := hex.Decode(ref[:], data[1:len(data)-1])
return err
}
示例12: hex
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
}
示例13: TestEncodeConcatenatedHashes
func TestEncodeConcatenatedHashes(t *testing.T) {
// Input Hash slice. Data taken from Decred's first three mainnet blocks.
hashSlice := []chainhash.Hash{
decodeHash("298e5cc3d985bfe7f81dc135f360abe089edd4396b86d2de66b0cef42b21d980"),
decodeHash("000000000000437482b6d47f82f374cde539440ddb108b0a76886f0d87d126b9"),
decodeHash("000000000000c41019872ff7db8fd2e9bfa05f42d3f8fee8e895e8c1e5b8dcba"),
}
hashLen := hex.EncodedLen(len(hashSlice[0]))
// Expected output. The string representations of the underlying byte arrays
// in the input []chainhash.Hash
blockHashes := []string{
"80d9212bf4ceb066ded2866b39d4ed89e0ab60f335c11df8e7bf85d9c35c8e29",
"b926d1870d6f88760a8b10db0d4439e5cd74f3827fd4b6827443000000000000",
"badcb8e5c1e895e8e8fef8d3425fa0bfe9d28fdbf72f871910c4000000000000",
}
concatenatedHashes := strings.Join(blockHashes, "")
// Test from 0 to N of the hashes
for j := 0; j < len(hashSlice)+1; j++ {
// Expected output string
concatRef := concatenatedHashes[:j*hashLen]
// Encode to string
concatenated := dcrjson.EncodeConcatenatedHashes(hashSlice[:j])
// Verify output
if concatenated != concatRef {
t.Fatalf("EncodeConcatenatedHashes failed (%v!=%v)",
concatenated, concatRef)
}
}
}
示例14: JsapiSign
// jssdk 支付签名, signType 只支持 "MD5", "SHA1", 传入其他的值会 panic.
func JsapiSign(appId, timeStamp, nonceStr, packageStr, signType string, apiKey string) string {
var h hash.Hash
switch signType {
case "MD5":
h = md5.New()
case "SHA1":
h = sha1.New()
default:
panic("unsupported signType")
}
bufw := bufio.NewWriterSize(h, 128)
// appId
// nonceStr
// package
// signType
// timeStamp
bufw.WriteString("appId=")
bufw.WriteString(appId)
bufw.WriteString("&nonceStr=")
bufw.WriteString(nonceStr)
bufw.WriteString("&package=")
bufw.WriteString(packageStr)
bufw.WriteString("&signType=")
bufw.WriteString(signType)
bufw.WriteString("&timeStamp=")
bufw.WriteString(timeStamp)
bufw.WriteString("&key=")
bufw.WriteString(apiKey)
bufw.Flush()
signature := make([]byte, hex.EncodedLen(h.Size()))
hex.Encode(signature, h.Sum(nil))
return string(bytes.ToUpper(signature))
}
示例15: getMd5
func getMd5(token, offset string) []byte {
d5.Reset()
d5.Write([]byte(token))
src := d5.Sum([]byte(offset))
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
return dst
}