github.com/eris-ltd/erisdb@v0.25.0/storage/content_addressed_store.go (about) 1 package storage 2 3 import ( 4 "crypto/sha256" 5 6 "fmt" 7 8 dbm "github.com/tendermint/tendermint/libs/db" 9 ) 10 11 type ContentAddressedStore struct { 12 db dbm.DB 13 } 14 15 func NewContentAddressedStore(db dbm.DB) *ContentAddressedStore { 16 return &ContentAddressedStore{ 17 db: db, 18 } 19 } 20 21 // These function match those used in Hoard 22 23 // Put data in the database by saving data with a key that is its sha256 hash 24 func (cas *ContentAddressedStore) Put(data []byte) ([]byte, error) { 25 hasher := sha256.New() 26 _, err := hasher.Write(data) 27 if err != nil { 28 return nil, fmt.Errorf("ContentAddressedStore could not hash data: %v", err) 29 } 30 hash := hasher.Sum(nil) 31 cas.db.SetSync(hash, data) 32 return hash, nil 33 } 34 35 func (cas *ContentAddressedStore) Get(hash []byte) ([]byte, error) { 36 return cas.db.Get(hash), nil 37 }