Example #1
0
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()),
	}
}
Example #2
0
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
}
Example #3
0
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
}
Example #4
0
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
}
Example #5
0
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
}
Example #6
0
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
}
Example #7
0
// 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
}
Example #8
0
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:
	}
}
Example #9
0
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
}
Example #10
0
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)
	}
}
Example #11
0
// 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
}
Example #12
0
// 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
}
Example #13
0
// 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
}
Example #14
0
func getHash(text string) string {
	h := fnv.New32a()
	if _, err := h.Write([]byte(text)); err != nil {
		return text
	}
	return fmt.Sprintf("%x", h.Sum32())
}
Example #15
0
// 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)
}
Example #16
0
func (this *SimpleAuthenticator) Open(buffer *alloc.Buffer) bool {
	len := buffer.Len()
	xtra := 4 - len%4
	if xtra != 0 {
		buffer.Slice(0, len+xtra)
	}
	xorbkd(buffer.Value)
	if xtra != 0 {
		buffer.Slice(0, len)
	}

	fnvHash := fnv.New32a()
	fnvHash.Write(buffer.Value[4:])
	if serial.BytesToUint32(buffer.Value[:4]) != fnvHash.Sum32() {
		return false
	}

	length := serial.BytesToUint16(buffer.Value[4:6])
	if buffer.Len()-6 != int(length) {
		return false
	}

	buffer.SliceFrom(6)

	return true
}
Example #17
0
func (e *Engine) fnv32a() error {
	data, err := computeHash(fnv.New32a(), e.stack.Pop())
	if err == nil {
		e.stack.Push(data)
	}
	return err
}
Example #18
0
// Locking wraps an orcas.Orca to provide locking around operations on the same
// key. When multipleReaders is true, operations will allow many readers and
// only a single writer at a time. When false, only a single reader is allowed.
// The concurrency param allows 2^(concurrency) operations to happen in
// parallel. E.g. concurrency of 1 would allow 2 parallel operations, while a
// concurrency of 4 allows 2^4 = 16 parallel operations.
func Locked(oc OrcaConst, multipleReaders bool, concurrency uint8) (OrcaConst, uint32) {
	if concurrency < 0 {
		panic("Concurrency level must be at least 0")
	}

	slot := getNewLocks(multipleReaders, concurrency)

	//counts := make([]uint32, 1<<concurrency)

	hashpool := &sync.Pool{
		New: func() interface{} {
			return fnv.New32a()
		},
	}

	return func(l1, l2 handlers.Handler, res common.Responder) Orca {
		return &LockedOrca{
			wrapped: oc(l1, l2, res),
			locks:   locks[slot],
			rlocks:  rlocks[slot],
			hpool:   hashpool,
			//counts:  counts,
		}
	}, slot
}
Example #19
0
func Authenticate(buffer *alloc.Buffer) {
	fnvHash := fnv.New32a()
	fnvHash.Write(buffer.Value)
	buffer.PrependHash(fnvHash)

	buffer.PrependUint16(uint16(buffer.Len()))
}
Example #20
0
// Get has value from a key.
func getHashValue(key string) uint32 {

	hash := fnv.New32a()
	hash.Write([]byte(key))
	return hash.Sum32()

}
Example #21
0
func sliceStringID32(sign []string) int32 {
	h := fnv.New32a()
	for _, s := range sign {
		h.Write([]byte(s))
	}
	return int32Bytes(h.Sum(nil))
}
Example #22
0
// fnvSum calculates a hash for concatenation of all input strings.
func fnvSum(elements ...string) uint32 {
	fnvHash := fnv.New32a()
	for _, element := range elements {
		fnvHash.Write([]byte(element))
	}
	return fnvHash.Sum32()
}
Example #23
0
func parsePayload(data []byte) (payload []byte, rest []byte, err error) {
	dataLen := len(data)
	if dataLen < 6 {
		err = TruncatedPayload
		return
	}
	payloadLen := int(data[0])<<8 + int(data[1])
	if dataLen < payloadLen+6 {
		err = TruncatedPayload
		return
	}

	payload = data[6 : 6+payloadLen]
	rest = data[6+payloadLen:]

	fnv1a := fnv.New32a()
	fnv1a.Write(payload)
	actualHash := fnv1a.Sum32()
	expectedHash := uint32(data[2])<<24 + uint32(data[3])<<16 + uint32(data[4])<<8 + uint32(data[5])
	if actualHash != expectedHash {
		err = transport.CorruptedPacket
		return
	}
	return
}
Example #24
0
func (idx *Indexer) BuildIndex(db *Database) int {
	drop_cache := make(map[string]string, len(idx.keyword_counts))

	idx.images_by_keyword = make(map[string][]*Image)
	for _, kwcnt := range idx.keyword_counts {
		idx.images_by_keyword[DropAccents(kwcnt.keyword, drop_cache)] =
			make([]*Image, 0, kwcnt.count)
	}
	hasher := fnv.New32a()
	num_images := 0
	for _, dir := range db.Directories() {
		for _, img := range dir.Images() {
			img.Id = imageHash(hasher, dir, img)
			img.Rank = num_images
			num_images += 1
			idx.addImageByKeyword(img, DropAccents(img.Name(), drop_cache))
			for _, kwd := range img.Keywords() {
				idx.addImageByKeyword(img, DropAccents(kwd, drop_cache))
			}
			for _, kwd := range img.SubKeywords() {
				idx.addImageByKeyword(img, DropAccents(kwd, drop_cache))
			}
		}
	}
	idx.images_by_id = make(map[int]*Image, num_images)
	for _, dir := range db.Directories() {
		for _, img := range dir.Images() {
			idx.images_by_id[img.Id] = img
		}
	}
	return num_images
}
Example #25
0
func defaultHasher(value interface{}) uint32 {
	switch v := value.(type) {
	case uint8:
		return uint32(v)
	case uint16:
		return uint32(v)
	case uint32:
		return v
	case uint64:
		return uint32(v)
	case int8:
		return uint32(v)
	case int16:
		return uint32(v)
	case int32:
		return uint32(v)
	case int64:
		return uint32(v)
	case uint:
		return uint32(v)
	case int:
		return uint32(v)
	case uintptr:
		return uint32(v)
	case float32:
		return uint32(v)
	case float64:
		return uint32(v)
	}
	hasher := fnv.New32a()
	hasher.Write([]byte(fmt.Sprintf("%#v", value)))
	return hasher.Sum32()
}
Example #26
0
// hash calculates the hexadecimal representation (8-chars)
// of the hash of the passed in string using the FNV-a algorithm
func hash(s string) string {
	hash := fnv.New32a()
	hash.Write([]byte(s))
	intHash := hash.Sum32()
	result := fmt.Sprintf("%08x", intHash)
	return result
}
Example #27
0
func validateBatchBench(b *testing.B, db *DB) {
	var rollback = errors.New("sentinel error to cause rollback")
	validate := func(tx *bolt.Tx) error {
		bucket := tx.Bucket([]byte("bench"))
		h := fnv.New32a()
		buf := make([]byte, 4)
		for id := uint32(0); id < 1000; id++ {
			binary.LittleEndian.PutUint32(buf, id)
			h.Reset()
			_, _ = h.Write(buf[:])
			k := h.Sum(nil)
			v := bucket.Get(k)
			if v == nil {
				b.Errorf("not found id=%d key=%x", id, k)
				continue
			}
			if g, e := v, []byte("filler"); !bytes.Equal(g, e) {
				b.Errorf("bad value for id=%d key=%x: %s != %q", id, k, g, e)
			}
			if err := bucket.Delete(k); err != nil {
				return err
			}
		}
		// should be empty now
		c := bucket.Cursor()
		for k, v := c.First(); k != nil; k, v = c.Next() {
			b.Errorf("unexpected key: %x = %q", k, v)
		}
		return rollback
	}
	if err := db.Update(validate); err != nil && err != rollback {
		b.Error(err)
	}
}
Example #28
0
// createStoredMsgId creates a uniq stored id from
// publish msg. This is used as a key of StoredMessages.
// <clientdi>-<msgid>-<randint>
// Note: QoS0 does not have a messageid, but it is not required to store.
// so this func is not invoked.
func createStoredMsgId(clientid string, m *proto.Publish) string {
	var randomness int32
	binary.Read(rand.Reader, binary.LittleEndian, &randomness) //add a little randomness
	inputString := fmt.Sprintf("%s-%v-%v", clientid, m.MessageId, randomness)
	h := fnv.New32a()
	h.Write([]byte(inputString))
	return fmt.Sprintf("%d", h.Sum32())
}
Example #29
0
func tokenCacheFile(config *oauth2.Config) string {
	hash := fnv.New32a()
	hash.Write([]byte(config.ClientID))
	hash.Write([]byte(config.ClientSecret))
	hash.Write([]byte(strings.Join(config.Scopes, " ")))
	fn := fmt.Sprintf("go-api-demo-tok%v", hash.Sum32())
	return filepath.Join(osUserCacheDir(), url.QueryEscape(fn))
}
Example #30
0
func hash(seed uint32, c *cid.Cid) uint32 {
	var buf [4]byte
	binary.LittleEndian.PutUint32(buf[:], seed)
	h := fnv.New32a()
	_, _ = h.Write(buf[:])
	_, _ = h.Write(c.Bytes())
	return h.Sum32()
}