Exemple #1
0
// Generate ID tag from client identification and data.
func idTag(id *PeerId, data []byte) []byte {
	ciph, err := xtea.NewCipher(id[:])
	if err != nil {
		panic(err)
	}
	enc := make([]byte, xtea.BlockSize)
	ciph.Encrypt(enc, data[:xtea.BlockSize])
	return enc
}
Exemple #2
0
// Initialize dummy cache for client-side usage.
func PeersInitDummy(id *PeerId, conf *PeerConf) {
	IDsCache = make(map[PeerId]*xtea.Cipher)
	cipher, err := xtea.NewCipher(id[:])
	if err != nil {
		panic(err)
	}
	IDsCache[*id] = cipher
	dummyConf = conf
}
Exemple #3
0
// Generate ID tag from client identification and data.
func idTag(id *PeerId, timeSync int, data []byte) []byte {
	ciph, err := xtea.NewCipher(id[:])
	if err != nil {
		panic(err)
	}
	enc := make([]byte, xtea.BlockSize)
	copy(enc, data)
	AddTimeSync(timeSync, enc)
	ciph.Encrypt(enc, enc)
	return enc
}
Exemple #4
0
// Init initialises the Uid generator
func (uid *UidGenerator) Init(workerId uint, key []byte) error {
	var err error

	if uid.seq == nil {
		uid.seq, err = sf.NewSnowFlake(uint32(workerId))
	}
	if uid.cipher == nil {
		uid.cipher, err = xtea.NewCipher(key)
	}

	return err
}
Exemple #5
0
func newNonceCipher(key *[32]byte) *xtea.Cipher {
	nonceKey := make([]byte, 16)
	salsa20.XORKeyStream(
		nonceKey,
		make([]byte, 32),
		make([]byte, xtea.BlockSize),
		key,
	)
	ciph, err := xtea.NewCipher(nonceKey)
	if err != nil {
		panic(err)
	}
	return ciph
}
Exemple #6
0
// Refresh IDsCache: remove disappeared keys, add missing ones with
// initialized ciphers.
func (cc cipherCache) refresh() {
	dir, err := os.Open(PeersPath)
	if err != nil {
		panic(err)
	}
	peerIds, err := dir.Readdirnames(0)
	if err != nil {
		panic(err)
	}
	available := make(map[PeerId]bool)
	for _, peerId := range peerIds {
		id, err := IDDecode(peerId)
		if err != nil {
			continue
		}
		available[*id] = true
	}

	cipherCacheLock.Lock()
	// Cleanup deleted ones from cache
	for k, _ := range cc {
		if _, exists := available[k]; !exists {
			delete(cc, k)
			log.Println("Cleaning key: ", k)
		}
	}
	// Add missing ones
	for peerId, _ := range available {
		if _, exists := cc[peerId]; !exists {
			log.Println("Adding key", peerId)
			cipher, err := xtea.NewCipher(peerId[:])
			if err != nil {
				panic(err)
			}
			cc[peerId] = cipher
		}
	}
	cipherCacheLock.Unlock()
}
Exemple #7
0
// Remove disappeared keys, add missing ones with initialized ciphers.
func (cc *CipherCache) Update(peers *map[PeerId]*PeerConf) {
	cc.l.Lock()
	for pid, _ := range cc.c {
		if _, exists := (*peers)[pid]; !exists {
			log.Println("Cleaning key:", pid)
			delete(cc.c, pid)
		}
	}
	for pid, pc := range *peers {
		if _, exists := cc.c[pid]; exists {
			cc.c[pid].t = pc.TimeSync
		} else {
			log.Println("Adding key", pid)
			cipher, err := xtea.NewCipher(pid[:])
			if err != nil {
				panic(err)
			}
			cc.c[pid] = &CipherAndTimeSync{cipher, pc.TimeSync}
		}
	}
	cc.l.Unlock()
}
Exemple #8
0
// Remove disappeared keys, add missing ones with initialized ciphers.
func (cc CipherCache) Update(peerIds []PeerId) {
	available := make(map[PeerId]struct{})
	for _, peerId := range peerIds {
		available[peerId] = struct{}{}
	}
	cc.l.Lock()
	for k, _ := range cc.c {
		if _, exists := available[k]; !exists {
			log.Println("Cleaning key:", k)
			delete(cc.c, k)
		}
	}
	for peerId, _ := range available {
		if _, exists := cc.c[peerId]; !exists {
			log.Println("Adding key", peerId)
			cipher, err := xtea.NewCipher(peerId[:])
			if err != nil {
				panic(err)
			}
			cc.c[peerId] = cipher
		}
	}
	cc.l.Unlock()
}