Add a hashing system to Entity

This commit is contained in:
Sasha Koshka 2024-03-01 02:53:10 -05:00
parent 69cbe192f9
commit a3de4f565e
1 changed files with 28 additions and 0 deletions

View File

@ -1,10 +1,32 @@
package entity
import "fmt"
import "crypto/md5"
import "encoding/base64"
import "github.com/google/uuid"
import "git.tebibyte.media/fspl/fspl/errors"
// A hash represents a hash sum that fits within a uint64.
type Hash [8]byte
// NewHash creates a new truncated md5 hash using the specified data.
func NewHash (data []byte) Hash {
sum := md5.Sum(data)
hash := Hash { }
copy(hash[:], sum[:8])
return hash
}
// Number converts the hash into a uint64.
func (hash Hash) Number () uint64 {
var number uint64
for _, part := range hash {
number <<= 8
number |= uint64(part)
}
return number
}
// Key globally indexes top level entities in contexts where modules matter.
type Key struct {
Unit uuid.UUID
@ -20,6 +42,12 @@ func (key Key) String () string {
return out
}
// Hash returns a representation of the hash of this key that fits within a
// uint64.
func (key Key) Hash () Hash {
return NewHash([]byte("Key:" + key.String()))
}
// LinkName returns the name that the entity it refers to will be given when
// compiled.
func (key Key) LinkName () string {