Exemplo n.º 1
0
func BigRandomBlob() *BlobTest {
	data := make([]byte, 2097152)
	rand.Read(data)
	sha := blake2b.New256()
	sha.Write(data)
	hash := fmt.Sprintf("%x", sha.Sum(nil))
	return &BlobTest{hash, data}
}
Exemplo n.º 2
0
func NewHash() (h hash.Hash) {
	if ih := hashPool.Get(); ih != nil {
		h = ih.(hash.Hash)
		h.Reset()
	} else {
		// Creates a new one if the pool is empty
		h = blake2b.New256()
	}
	return
}
Exemplo n.º 3
0
func RandomBlob(content []byte) *BlobTest {
	var data []byte
	if content == nil {
		data = make([]byte, 512)
		rand.Read(data)
	} else {
		data = content
	}
	sha := blake2b.New256()
	sha.Write(data)
	hash := fmt.Sprintf("%x", sha.Sum(nil))
	return &BlobTest{hash, data}
}
Exemplo n.º 4
0
func (st *StateTree) Add(h string) {
	st.Lock()
	defer st.Unlock()
	var chash hash.Hash
	if exhash, ok := st.level1[h[0:2]]; ok {
		chash = exhash
	} else {
		chash = blake2b.New256()
		st.level1[h[0:2]] = chash
	}
	chash.Write([]byte(h))
	st.root.Write([]byte(h))
	st.count++
}
Exemplo n.º 5
0
// Encode the data, produce AONT package. Data size will be larger than
// the original one for 48 bytes.
func Encode(r *[RSize]byte, in []byte) ([]byte, error) {
	out := make([]byte, len(in)+HSize+RSize)
	copy(out, in)
	h := blake2b.New256()
	h.Write(r[:])
	h.Write(in)
	copy(out[len(in):], h.Sum(nil))
	salsaKey := new([32]byte)
	copy(salsaKey[:], r[:])
	salsa20.XORKeyStream(out, out, dummyNonce, salsaKey)
	h.Reset()
	h.Write(out[:len(in)+32])
	for i, b := range h.Sum(nil)[:RSize] {
		out[len(in)+32+i] = b ^ r[i]
	}
	return out, nil
}
Exemplo n.º 6
0
func (backend *BlobsFileBackend) encodeBlob(blob []byte) (size int, data []byte) {
	h := blake2b.New256()
	h.Write(blob)

	if backend.snappyCompression {
		dataEncoded := snappy.Encode(nil, blob)
		blob = dataEncoded
	}
	size = len(blob)
	data = make([]byte, len(blob)+Overhead)
	copy(data[:], h.Sum(nil))
	// set the flag
	data[hashSize] = 0
	binary.LittleEndian.PutUint32(data[hashSize+1:], uint32(size))
	copy(data[Overhead:], blob)
	return
}
Exemplo n.º 7
0
func (backend *BlobsFileBackend) decodeBlob(data []byte) (size int, blob []byte) {
	//flag := data[hashSize]
	size = int(binary.LittleEndian.Uint32(data[hashSize+1 : Overhead]))
	blob = make([]byte, size)
	copy(blob, data[Overhead:])
	if backend.snappyCompression {
		blobDecoded, err := snappy.Decode(nil, blob)
		if err != nil {
			panic(fmt.Errorf("Failed to decode blob with Snappy: %v", err))
		}
		blob = blobDecoded
	}
	h := blake2b.New256()
	h.Write(blob)
	if !bytes.Equal(h.Sum(nil), data[0:hashSize]) {
		panic(fmt.Errorf("Hash doesn't match %x != %x", h.Sum(nil), data[0:hashSize]))
	}
	return
}
Exemplo n.º 8
0
// Decode the data from AONT package. Data size will be smaller than the
// original one for 48 bytes.
func Decode(in []byte) ([]byte, error) {
	if len(in) < HSize+RSize {
		return nil, errors.New("Too small input buffer")
	}
	h := blake2b.New256()
	h.Write(in[:len(in)-RSize])
	salsaKey := new([32]byte)
	for i, b := range h.Sum(nil)[:RSize] {
		salsaKey[i] = b ^ in[len(in)-RSize+i]
	}
	h.Reset()
	h.Write(salsaKey[:RSize])
	out := make([]byte, len(in)-RSize)
	salsa20.XORKeyStream(out, in[:len(in)-RSize], dummyNonce, salsaKey)
	h.Write(out[:len(out)-HSize])
	if subtle.ConstantTimeCompare(h.Sum(nil), out[len(out)-HSize:]) != 1 {
		return nil, errors.New("Invalid checksum")
	}
	return out[:len(out)-HSize], nil
}
Exemplo n.º 9
0
func NewStateTree() *StateTree {
	return &StateTree{
		root:   blake2b.New256(),
		level1: map[string]hash.Hash{},
	}
}
Exemplo n.º 10
0
Arquivo: hash.go Projeto: mm3/Sia
// NewHash returns a blake2b 256bit hasher.
func NewHash() hash.Hash {
	return blake2b.New256()
}