package storage import ( "crypto/sha256" ) type Storage struct { Path string } func NewStorage(path string) (*Storage, error) { s := Storage{ Path: path, } return &s, nil } type Object interface { GetHash() [32]byte } type Blob struct { Data []byte } // The `GetHash()` for a `Blob` is a straightforward SHA256 hash of the // contents of that blob. func (b *Blob) GetHash() [32]byte { return sha256.Sum256(b.Data) } func NewBlob(data []byte) (*Blob, error) { b := Blob{ Data: data, } return &b, nil } type Tree struct { Items map[string]Object } func NewTree() (*Tree, error) { return nil, nil } // For a `Tree` the `GetHash()` is a bit more intertwined. It's basically the // hash of all its items. Yes, this recurses trees which is bad but the naive // approach suffices for now. I also don't like the `[32]` vs `[]` dance here. func (t *Tree) GetHash() [32]byte { h := sha256.New() for _, v := range t.Items { s := v.GetHash() h.Write(s[:]) } return [32]byte(h.Sum(nil)) }