作者:shitfSig
项目:goagent-g
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
}
作者:lapric
项目:cryptoballo
// 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
}
作者:samegoa
项目:wccha
func newSID() string {
b := make([]byte, 8)
rand.Read(b)
d := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(d, b)
return string(d)
}
作者:lapric
项目:cryptoballo
// 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
}
作者:CatchReleas
项目:s3zippe
// 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
}
作者:btbxbo
项目:wecha
// 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))
}
作者:btbxbo
项目:wecha
// 传统的签名代码, 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))
}
作者:PaulWeiHa
项目:ig
// 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
}
作者:outdoors
项目:check
// 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
}
作者:dmcgowa
项目:gotu
// 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
}
作者:dches
项目:hesfi
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
}
作者: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
}
作者:decre
项目:dcr
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)
}
}
}
作者:btbxbo
项目:wecha
// 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))
}
作者:marknewmai
项目:go
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
}
作者:andreb
项目:ex
// String return the string representation of the value
func (kp KeyPrinter) PrintString(k Key) string {
if k == nil {
return ""
}
out := make([]byte, hex.EncodedLen(len(k.Bytes())))
out = kp.Print(out, k)
return string(out)
}
作者:uruddarraj
项目:p
func appendBytes(dst []byte, v []byte) []byte {
tmp := make([]byte, hex.EncodedLen(len(v)))
hex.Encode(tmp, v)
dst = append(dst, "\\x"...)
dst = append(dst, tmp...)
return dst
}
作者:rsrsp
项目:p
func appendBytes(dst []byte, src []byte) []byte {
tmp := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(tmp, src)
dst = append(dst, "'\\x"...)
dst = append(dst, tmp...)
dst = append(dst, '\'')
return dst
}
作者:catalyzei
项目:cl
// Hex encode bytes
func (c *SCrypto) Hex(src []byte, maxLen int) []byte {
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
if len(dst) > maxLen {
// avoid extraneous padding
dst = dst[:maxLen]
}
return dst
}
作者:eran
项目:iron-g
// Generates a random, hex-encoded salt.
func generateSalt(saltBits int) (salt []byte, err error) {
saltBytes, err := randomBits(saltBits)
if err != nil {
return
}
salt = make([]byte, hex.EncodedLen(len(saltBytes)))
hex.Encode(salt, saltBytes)
return
}