当前位置: 首页>>代码示例>>Golang>>正文


Golang fnv.New32a函数代码示例

本文整理汇总了Golang中hash/fnv.New32a函数的典型用法代码示例。如果您正苦于以下问题:Golang New32a函数的具体用法?Golang New32a怎么用?Golang New32a使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New32a函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewEventFactory

func NewEventFactory(processName, threadName string) *EventFactory {
	processHash := fnv.New32a()
	processHash.Write([]byte(processName))
	threadHash := fnv.New32a()
	threadHash.Write([]byte(threadName))
	return &EventFactory{
		processName: processName,
		threadName:  threadName,
		pid:         int(processHash.Sum32()),
		tid:         int(threadHash.Sum32()),
	}
}
开发者ID:lhchavez,项目名称:quark,代码行数:12,代码来源:tracing.go

示例2: handler

func handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	params := strings.SplitN(strings.Trim(r.URL.Path, "/"), "/", 2)

	// / -> redirect
	if len(params[0]) == 0 {
		http.Redirect(w, r, "https://github.com/igrigorik/ga-beacon", http.StatusFound)
		return
	}

	// /account -> account template
	// /account/page -> GIF + log pageview to GA collector
	if len(params) == 1 {
		account := map[string]interface{}{"Account": params[0]}
		if err := pageTemplate.ExecuteTemplate(w, "page", account); err != nil {
			panic("Cannot execute template")
		}

	} else {
		hash := fnv.New32a()
		hash.Write([]byte(strings.Split(r.RemoteAddr, ":")[0]))
		hash.Write([]byte(r.Header.Get("User-Agent")))
		cid := fmt.Sprintf("%d", hash.Sum32())

		w.Header().Set("Content-Type", "image/gif")
		w.Header().Set("Cache-Control", "no-cache")
		w.Header().Set("CID", cid)

		payload := url.Values{
			"v":   {"1"},        // protocol version = 1
			"t":   {"pageview"}, // hit type
			"tid": {params[0]},  // tracking / property ID
			"cid": {cid},        // unique client ID (IP + UA hash)
			"dp":  {params[1]},  // page path
		}

		req, _ := http.NewRequest("POST", beaconURL,
			strings.NewReader(payload.Encode()))
		req.Header.Add("User-Agent", r.Header.Get("User-Agent"))

		client := urlfetch.Client(c)
		resp, err := client.Do(req)
		if err != nil {
			c.Errorf("GA collector POST error: %s", err.Error())
		}
		c.Infof("GA collector status: %v, cid: %v", resp.Status, cid)

		// write out GIF pixel
		query, _ := url.ParseQuery(r.URL.RawQuery)
		_, ok_pixel := query["pixel"]

		if ok_pixel {
			io.WriteString(w, string(pixel))
		} else {
			io.WriteString(w, string(badge))
		}
	}

	return
}
开发者ID:BlastarIndia,项目名称:ga-beacon,代码行数:60,代码来源:ga-beacon.go

示例3: ReadVMessUDP

func ReadVMessUDP(buffer []byte, userset user.UserSet) (*VMessUDP, error) {
	userHash := buffer[:user.IDBytesLen]
	userId, timeSec, valid := userset.GetUser(userHash)
	if !valid {
		return nil, errors.NewAuthenticationError(userHash)
	}

	buffer = buffer[user.IDBytesLen:]
	aesCipher, err := aes.NewCipher(userId.CmdKey())
	if err != nil {
		return nil, err
	}
	aesStream := cipher.NewCFBDecrypter(aesCipher, user.Int64Hash(timeSec))
	aesStream.XORKeyStream(buffer, buffer)

	fnvHash := binary.BigEndian.Uint32(buffer[:4])
	fnv1a := fnv.New32a()
	fnv1a.Write(buffer[4:])
	fnvHashActual := fnv1a.Sum32()

	if fnvHash != fnvHashActual {
		log.Warning("Unexpected fhv hash %d, should be %d", fnvHashActual, fnvHash)
		return nil, errors.NewCorruptedPacketError()
	}

	buffer = buffer[4:]

	vmess := &VMessUDP{
		user:    *userId,
		version: buffer[0],
		token:   binary.BigEndian.Uint16(buffer[1:3]),
	}

	// buffer[3] is reserved

	port := binary.BigEndian.Uint16(buffer[4:6])
	addrType := buffer[6]
	var address v2net.Address
	switch addrType {
	case addrTypeIPv4:
		address = v2net.IPAddress(buffer[7:11], port)
		buffer = buffer[11:]
	case addrTypeIPv6:
		address = v2net.IPAddress(buffer[7:23], port)
		buffer = buffer[23:]
	case addrTypeDomain:
		domainLength := buffer[7]
		domain := string(buffer[8 : 8+domainLength])
		address = v2net.DomainAddress(domain, port)
		buffer = buffer[8+domainLength:]
	default:
		log.Warning("Unexpected address type %d", addrType)
		return nil, errors.NewCorruptedPacketError()
	}

	vmess.address = address
	vmess.data = buffer

	return vmess, nil
}
开发者ID:s752550916,项目名称:v2ray-core,代码行数:60,代码来源:udp.go

示例4: hashn

func hashn(s string) (h1, h2 uint32) {

	// This construction comes from
	// http://www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf
	// "Building a Better Bloom Filter", by Kirsch and Mitzenmacher. Their
	// proof that this is allowed for count-min requires the h functions to
	// be from the 2-universal hash family, w be a prime and d be larger
	// than the traditional CM-sketch requirements.

	// Empirically, though, this seems to work "just fine".

	// TODO(dgryski): Switch to something that is actually validated by the literature.

	fnv1a := fnv.New32a()
	fnv1a.Write([]byte(s))
	h1 = fnv1a.Sum32()

	// inlined jenkins one-at-a-time hash
	h2 = uint32(0)
	for _, c := range s {
		h2 += uint32(c)
		h2 += h2 << 10
		h2 ^= h2 >> 6
	}
	h2 += (h2 << 3)
	h2 ^= (h2 >> 11)
	h2 += (h2 << 15)

	return h1, h2
}
开发者ID:hsavit1,项目名称:go-probably,代码行数:30,代码来源:count.go

示例5: NewNode

func NewNode(hn coconet.Host, suite abstract.Suite, random cipher.Stream) *Node {
	sn := &Node{Host: hn, suite: suite}
	msgSuite = suite
	sn.PrivKey = suite.Secret().Pick(random)
	sn.PubKey = suite.Point().Mul(nil, sn.PrivKey)

	sn.peerKeys = make(map[string]abstract.Point)
	sn.Rounds = make(map[int]*Round)

	sn.closed = make(chan error, 20)
	sn.done = make(chan int, 10)
	sn.commitsDone = make(chan int, 10)
	sn.viewChangeCh = make(chan string, 0)

	sn.FailureRate = 0
	h := fnv.New32a()
	h.Write([]byte(hn.Name()))
	seed := h.Sum32()
	sn.Rand = rand.New(rand.NewSource(int64(seed)))
	sn.Host.SetSuite(suite)
	sn.VoteLog = NewVoteLog()
	sn.Actions = make(map[int][]*Vote)
	sn.RoundsPerView = 100
	return sn
}
开发者ID:ineiti,项目名称:prifi,代码行数:25,代码来源:signingNode.go

示例6: Read

func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
	buffer := alloc.NewBuffer()
	if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {
		buffer.Release()
		return nil, err
	}

	length := serial.BytesLiteral(buffer.Value[:2]).Uint16Value()
	if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil {
		buffer.Release()
		return nil, err
	}
	buffer.Slice(0, int(length))

	fnvHash := fnv.New32a()
	fnvHash.Write(buffer.Value[4:])
	expAuth := serial.BytesLiteral(fnvHash.Sum(nil))
	actualAuth := serial.BytesLiteral(buffer.Value[:4])
	if !actualAuth.Equals(expAuth) {
		buffer.Release()
		return nil, transport.ErrorCorruptedPacket
	}
	buffer.SliceFrom(4)
	return buffer, nil
}
开发者ID:wangyou,项目名称:v2ray-core,代码行数:25,代码来源:reader.go

示例7: NewKeyedNode

// Create new signing node that incorporates a given private key
func NewKeyedNode(hn coconet.Host, suite abstract.Suite, PrivKey abstract.Secret) *Node {
	sn := &Node{Host: hn, suite: suite, PrivKey: PrivKey}
	sn.PubKey = suite.Point().Mul(nil, sn.PrivKey)

	sn.peerKeys = make(map[string]abstract.Point)

	sn.closed = make(chan error, 20)
	sn.done = make(chan int, 10)
	sn.commitsDone = make(chan int, 10)
	sn.viewChangeCh = make(chan string, 0)

	sn.RoundCommits = make(map[int][]*SigningMessage)
	sn.RoundResponses = make(map[int][]*SigningMessage)

	sn.FailureRate = 0
	h := fnv.New32a()
	h.Write([]byte(hn.Name()))
	seed := h.Sum32()
	sn.Rand = rand.New(rand.NewSource(int64(seed)))
	sn.Host.SetSuite(suite)
	sn.VoteLog = NewVoteLog()
	sn.Actions = make(map[int][]*Vote)
	sn.RoundsPerView = 0
	sn.Rounds = make(map[int]Round)
	sn.MaxWait = 50 * time.Second
	return sn
}
开发者ID:mlncn,项目名称:cothority,代码行数:28,代码来源:nodehelper.go

示例8: handleCommand

func (this *VMessOutboundHandler) handleCommand(dest v2net.Destination, cmdId byte, data []byte) {
	if len(data) < 4 {
		return
	}
	fnv1hash := fnv.New32a()
	fnv1hash.Write(data[4:])
	actualHashValue := fnv1hash.Sum32()
	expectedHashValue := serial.BytesLiteral(data[:4]).Uint32Value()
	if actualHashValue != expectedHashValue {
		return
	}
	data = data[4:]
	cmd, err := command.CreateResponseCommand(cmdId)
	if err != nil {
		log.Warning("VMessOut: Unknown response command (", cmdId, "): ", err)
		return
	}
	if err := cmd.Unmarshal(data); err != nil {
		log.Warning("VMessOut: Failed to parse response command: ", err)
		return
	}
	switch typedCommand := cmd.(type) {
	case *command.SwitchAccount:
		if typedCommand.Host == nil {
			typedCommand.Host = dest.Address()
		}
		this.handleSwitchAccount(typedCommand)
	default:
	}
}
开发者ID:airmao,项目名称:v2ray-core,代码行数:30,代码来源:command.go

示例9: getStreamKey

func (cons *PcapHTTPConsumer) getStreamKey(pkt *pcap.Packet) (uint32, string, bool) {
	if len(pkt.Headers) != 2 {
		Log.Debug.Printf("Invalid number of headers: %d", len(pkt.Headers))
		Log.Debug.Printf("Not a TCP/IP packet: %#v", pkt)
		return 0, "", false
	}

	ipHeader, isIPHeader := ipFromPcap(pkt)
	tcpHeader, isTCPHeader := tcpFromPcap(pkt)
	if !isIPHeader || !isTCPHeader {
		Log.Debug.Printf("Not a TCP/IP packet: %#v", pkt)
		return 0, "", false
	}

	if len(pkt.Payload) == 0 {
		return 0, "", false
	}

	clientID := fmt.Sprintf("%s:%d", ipHeader.SrcAddr(), tcpHeader.SrcPort)
	key := fmt.Sprintf("%s-%s:%d", clientID, ipHeader.DestAddr(), tcpHeader.DestPort)
	keyHash := fnv.New32a()
	keyHash.Write([]byte(key))

	return keyHash.Sum32(), clientID, true
}
开发者ID:pombredanne,项目名称:gollum-1,代码行数:25,代码来源:pcaphttp.go

示例10: makeFieldsHashPartitioner

func makeFieldsHashPartitioner(fields []string, dropFail bool) partitioner {
	generator := rand.New(rand.NewSource(rand.Int63()))
	hasher := fnv.New32a()

	return func(msg *message, numPartitions int32) (int32, error) {
		hash := msg.hash
		if hash == 0 {
			hasher.Reset()

			var err error
			for _, field := range fields {
				err = hashFieldValue(hasher, msg.event, field)
				if err != nil {
					break
				}
			}

			if err != nil {
				if dropFail {
					logp.Err("Hashing partition key failed: %v", err)
					return -1, err
				}

				msg.hash = generator.Uint32()
			} else {
				msg.hash = hasher.Sum32()
			}
			hash = msg.hash
		}

		return hash2Partition(hash, numPartitions)
	}
}
开发者ID:Zhoutall,项目名称:beats,代码行数:33,代码来源:partition.go

示例11: Open

// Open implements cipher.AEAD.Open().
func (v *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte, error) {
	dst = append(dst, cipherText...)
	dstLen := len(dst)
	xtra := 4 - dstLen%4
	if xtra != 4 {
		dst = append(dst, make([]byte, xtra)...)
	}
	xorbkd(dst)
	if xtra != 4 {
		dst = dst[:dstLen]
	}

	fnvHash := fnv.New32a()
	fnvHash.Write(dst[4:])
	if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() {
		return nil, crypto.ErrAuthenticationFailed
	}

	length := serial.BytesToUint16(dst[4:6])
	if len(dst)-6 != int(length) {
		return nil, crypto.ErrAuthenticationFailed
	}

	return dst[6:], nil
}
开发者ID:v2ray,项目名称:v2ray-core,代码行数:26,代码来源:crypt.go

示例12: Get

// Get - fetch a cache entry (if exists)
func (rc *RouteCache) Get(path string) CacheEntry {
	if rc.ReorderOnAccess {
		rc.mutex.Lock()
		defer rc.mutex.Unlock()
	} else {
		rc.mutex.RLock()
		defer rc.mutex.RUnlock()
	}
	cacheEntry := CacheEntry{}
	hash := fnv.New32a()
	hash.Write([]byte(path))
	pathHash := hash.Sum32()
	var foundIdx = -1
	for idx, hashKey := range rc.pathHashes {
		if hashKey == pathHash {
			cacheEntry = rc.Entries[idx]
			foundIdx = idx
			break
		}
	}
	if foundIdx >= 0 && rc.ReorderOnAccess {
		if len(rc.pathHashes) > 1 {
			rc.moveEntryToTop(pathHash, foundIdx)
		}
	} else {
		cacheEntry = NotFoundCacheEntry()
	}
	return cacheEntry
}
开发者ID:goposse,项目名称:goro,代码行数:30,代码来源:cache.go

示例13: Put

// Put - add an item to the route cache
func (rc *RouteCache) Put(path string, entry CacheEntry) {
	rc.mutex.Lock()
	defer rc.mutex.Unlock()

	hash := fnv.New32a()
	hash.Write([]byte(path))
	allHashes := rc.pathHashes
	allEntries := rc.Entries
	pathHash := hash.Sum32()
	if rc.containsMap[pathHash] {
		return // don't add it again
	}
	if len(allHashes) == rc.MaxEntries {
		// remove the last element from the slices
		removeHash := allHashes[len(allHashes)-1]
		allHashes = allHashes[:len(allHashes)-1]
		allEntries = allEntries[:len(allEntries)-1]
		delete(rc.containsMap, removeHash)
	}

	newHashes := append([]uint32{pathHash}, allHashes...)
	newEntries := append([]CacheEntry{entry}, allEntries...)
	rc.containsMap[pathHash] = true
	rc.pathHashes = newHashes
	rc.Entries = newEntries
}
开发者ID:goposse,项目名称:goro,代码行数:27,代码来源:cache.go

示例14: getHash

func getHash(text string) string {
	h := fnv.New32a()
	if _, err := h.Write([]byte(text)); err != nil {
		return text
	}
	return fmt.Sprintf("%x", h.Sum32())
}
开发者ID:lucmichalski,项目名称:kube2vulcand,代码行数:7,代码来源:kube2vulcand.go

示例15: hashString

// BUG: The probability of hashing collisions is too high with only 17 bits.
// NOTE: Using a numerical base as high as valid characters in DNS names would
// reduce the resulting length without risking more collisions.
func hashString(s string) string {
	h := fnv.New32a()
	_, _ = h.Write([]byte(s))
	sum := h.Sum32()
	lower, upper := uint16(sum), uint16(sum>>16)
	return strconv.FormatUint(uint64(lower+upper), 10)
}
开发者ID:bigstepinc,项目名称:mesos-dns,代码行数:10,代码来源:generator.go


注:本文中的hash/fnv.New32a函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。