2014-09-23 20:39:12 +00:00
|
|
|
package backend
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-09-21 14:33:20 +00:00
|
|
|
"crypto/sha256"
|
2014-04-21 21:25:31 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2014-09-23 20:39:12 +00:00
|
|
|
"errors"
|
2014-04-21 21:25:31 +00:00
|
|
|
)
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
const sha256_length = 32 // in bytes
|
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
// References content within a repository.
|
|
|
|
type ID []byte
|
|
|
|
|
|
|
|
// ParseID converts the given string to an ID.
|
|
|
|
func ParseID(s string) (ID, error) {
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
if len(b) != sha256_length {
|
|
|
|
return nil, errors.New("invalid length for sha256 hash")
|
|
|
|
}
|
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
return ID(b), nil
|
|
|
|
}
|
2014-07-28 18:20:32 +00:00
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
func (id ID) String() string {
|
|
|
|
return hex.EncodeToString(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal compares an ID to another other.
|
|
|
|
func (id ID) Equal(other ID) bool {
|
|
|
|
return bytes.Equal(id, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EqualString compares this ID to another one, given as a string.
|
|
|
|
func (id ID) EqualString(other string) (bool, error) {
|
|
|
|
s, err := hex.DecodeString(other)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id.Equal(ID(s)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ID) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(id.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id *ID) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(b, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*id = make([]byte, len(s)/2)
|
|
|
|
_, err = hex.Decode(*id, []byte(s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-21 14:33:20 +00:00
|
|
|
|
|
|
|
func IDFromData(d []byte) ID {
|
|
|
|
hash := sha256.Sum256(d)
|
2014-09-23 20:39:12 +00:00
|
|
|
id := make([]byte, sha256_length)
|
2014-09-21 14:33:20 +00:00
|
|
|
copy(id, hash[:])
|
|
|
|
return id
|
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
type IDs []ID
|
|
|
|
|
|
|
|
func (ids IDs) Len() int {
|
|
|
|
return len(ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ids IDs) Less(i, j int) bool {
|
|
|
|
if len(ids[i]) < len(ids[j]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, b := range ids[i] {
|
|
|
|
if b == ids[j][k] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if b < ids[j][k] {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ids IDs) Swap(i, j int) {
|
|
|
|
ids[i], ids[j] = ids[j], ids[i]
|
|
|
|
}
|