Ejemplo n.º 1
0
// Blake2b returns the 64-byte BLAKE2b cryptographic
// hash of the Frame. This is useful for hashing and
// de-duplicating a stream of Frames.
//
// reference: https://godoc.org/github.com/codahale/blake2
// reference: https://blake2.net/
// reference: https://tools.ietf.org/html/rfc7693
//
func (f *Frame) Blake2b() []byte {
	h, err := blake2b.New(nil)
	panicOn(err)

	n := f.NumBytes()

	var m [24]byte
	binary.LittleEndian.PutUint64(m[:8], uint64(f.Prim))
	switch {
	case n == 8:
		h.Write(m[:8])
	default:
		pti := f.GetPTI()
		switch pti {
		case PtiOneInt64:
			binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
			h.Write(m[:16])
		case PtiOneFloat64:
			binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
			h.Write(m[:16])
		case PtiTwo64:
			binary.LittleEndian.PutUint64(m[8:16], math.Float64bits(f.V0))
			binary.LittleEndian.PutUint64(m[16:24], uint64(f.Ude))
			h.Write(m[:24])
		case PtiUDE:
			binary.LittleEndian.PutUint64(m[8:16], uint64(f.Ude))
			h.Write(m[:16])
			h.Write(f.Data)
		}
	}

	return []byte(h.Sum(nil))
}
Ejemplo n.º 2
0
// Blake2bUint64 returns an 8 byte BLAKE2b cryptographic
// hash of the raw.
//
// we're using the pure go: https://github.com/dchest/blake2b
//
// but the C-wrapped refence may be helpful as well --
//
// reference: https://godoc.org/github.com/codahale/blake2
// reference: https://blake2.net/
// reference: https://tools.ietf.org/html/rfc7693
//
func Blake2bUint64(raw []byte) uint64 {
	cfg := &blake2b.Config{Size: 8}
	h, err := blake2b.New(cfg)
	panicOn(err)
	h.Write(raw)
	by := h.Sum(nil)
	return binary.LittleEndian.Uint64(by[:8])
}