Esempio n. 1
0
// Reseed reseeds the generator with the given arbitrary input.
func (g *Generator) Reseed(s string) {
	h := sha3.NewKeccak256()
	h.Write(g.key[:])
	h.Write([]byte(s))
	key := h.Sum(nil)
	copy(g.key[:], key)
	zero(key)
	h.Reset()
	incCounter(g.ctr)
}
Esempio n. 2
0
// Write performs the same operation as Reseed, but allows the
// generator to be used as an io.Writer.
func (g *Generator) Write(bs []byte) (int, error) {
	h := sha3.NewKeccak256()
	h.Write(g.key[:])
	h.Write(bs)
	key := h.Sum(nil)
	copy(g.key[:], key)
	zero(key)
	h.Reset()
	incCounter(g.ctr)
	return len(bs), nil
}
Esempio n. 3
0
// getHash calculates the hash of a file.
// It reads a file block by block, and updates a hashsum with each block.
// Reading by blocks consume very little memory, which is needed for large files.
// parameters:
//      - fd is an open file descriptor that points to the file to inspect
//      - hashType is an integer that define the type of hash
// return:
//      - hexhash, the hex encoded hash of the file found at fp
func getHash(fd *os.File, hashType int) (hexhash string) {
	if DEBUG {
		fmt.Printf("getHash: computing hash for '%s'\n", fd.Name())
	}
	var h hash.Hash
	switch hashType {
	case CheckMD5:
		h = md5.New()
	case CheckSHA1:
		h = sha1.New()
	case CheckSHA256:
		h = sha256.New()
	case CheckSHA384:
		h = sha512.New384()
	case CheckSHA512:
		h = sha512.New()
	case CheckSHA3_224:
		h = sha3.NewKeccak224()
	case CheckSHA3_256:
		h = sha3.NewKeccak256()
	case CheckSHA3_384:
		h = sha3.NewKeccak384()
	case CheckSHA3_512:
		h = sha3.NewKeccak512()
	default:
		err := fmt.Sprintf("getHash: Unkown hash type %d", hashType)
		panic(err)
	}
	buf := make([]byte, 4096)
	var offset int64 = 0
	for {
		block, err := fd.ReadAt(buf, offset)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if block == 0 {
			break
		}
		h.Write(buf[:block])
		offset += int64(block)
	}
	hexhash = fmt.Sprintf("%x", h.Sum(nil))
	return
}
Esempio n. 4
0
func (rng *Tunafish) reseed() {
	rng.counter++
	s := []byte{}

	for i := 0; i < len(rng.pools); i++ {
		if ((1 << uint32(i)) | rng.counter) != 0 {
			rng.pools[i].Lock()
			h := sha3.NewKeccak256()
			h.Write(rng.pools[i].hash)
			s = append(s, h.Sum(nil)...)
			rng.pools[i].hash = []byte{}
			rng.pools[i].Unlock()
		}
	}
	rng.g.Write(s)
	rng.lastReseed.Lock()
	rng.lastReseed.Time = time.Now()
	rng.lastReseed.Unlock()
}
Esempio n. 5
0
func Sha3Bin(data []byte) []byte {
	d := sha3.NewKeccak256()
	d.Write(data)

	return d.Sum(nil)
}
Esempio n. 6
0
func keccakMac(key, data []byte) []byte {
	h := sha3.NewKeccak256()
	h.Write(key)
	h.Write(data)
	return h.Sum(nil)
}