Exemplo n.º 1
0
// Returns false if the hash is too old, or is already a
// member of the set.  Timestamp is in seconds.
func (r *Replay) Valid(mask int, hash [32]byte, timestamp interfaces.Timestamp, systemtime interfaces.Timestamp) (index int, valid bool) {

	now := Minutes(systemtime.GetTimeSeconds())
	t := Minutes(timestamp.GetTimeSeconds())

	diff := now - t
	// Check the timestamp to see if within 12 hours of the system time.  That not valid, we are
	// just done without any added concerns.
	if diff > Range || diff < -Range {
		//fmt.Println("Time in hours, range:", hours(timeSeconds-systemTimeSeconds), HourRange)
		return -1, false
	}

	r.Mutex.Lock()
	defer r.Mutex.Unlock()

	if mask == constants.TIME_TEST {
		return -1, true
	}

	// We don't let the system clock go backwards.  likely an attack if it does.
	// Move the current time up to r.center if it is in the past.
	if now < r.Center {
		now = r.Center
	}

	if r.Center == 0 {
		r.Center = now
		r.Basetime = r.Center - (numBuckets / 2)
	}
	for r.Center < now {
		for k := range r.Buckets[0] {
			delete(r.Buckets[0], k)
		}
		copy(r.Buckets[:], r.Buckets[1:])
		r.Buckets[numBuckets-1] = make(map[[32]byte]int)
		r.Center++
		r.Basetime++
	}

	// Just take the time of the thing in hours less the basetime to get the index.
	index = t - r.Basetime

	if index < 0 || index >= numBuckets {
		return -1, false
	}

	if r.Buckets[index] == nil {
		r.Buckets[index] = make(map[[32]byte]int)
	} else {
		v, _ := r.Buckets[index][hash]
		if v&mask > 0 {
			return index, false
		}
	}
	return index, true
}
Exemplo n.º 2
0
func (r *Replay) SetHashNow(mask int, hash [32]byte, now interfaces.Timestamp) {

	if r.IsHashUnique(mask, hash) {
		index := Minutes(now.GetTimeSeconds()) - r.Basetime
		if index < 0 || index >= len(r.Buckets) {
			return
		}

		r.Mutex.Lock()
		defer r.Mutex.Unlock()

		if r.Buckets[index] == nil {
			r.Buckets[index] = make(map[[32]byte]int)
		}
		r.Buckets[index][hash] = mask | r.Buckets[index][hash]
	}
}