package process import ( "crypto/hmac" "crypto/sha256" "encoding/binary" "encoding/hex" "hash" "strconv" ) type Keyed struct { mac hash.Hash buf []byte sum []byte } func (p *Processor) Keyed() *Keyed { return &Keyed{mac: hmac.New(sha256.New, p.key), buf: make([]byte, 0, 96), sum: make([]byte, 0, sha256.Size)} } func (k *Keyed) digest(label, hash string, level, slot int, source string) []byte { b := append(k.buf[:0], label...) b = append(b, '|') b = append(b, hash...) b = append(b, '|') b = strconv.AppendInt(b, int64(level), 10) if slot >= 0 { b = append(b, '|') b = strconv.AppendInt(b, int64(slot), 10) b = append(b, '|') b = append(b, source...) } k.buf = b k.mac.Reset() k.mac.Write(b) k.sum = k.mac.Sum(k.sum[:0]) return k.sum } func (k *Keyed) DecoyName(hash string, level, slot int, source string) string { return hex.EncodeToString(k.digest("decoy", hash, level, slot, source)[:8]) } func (k *Keyed) Seed(label, hash string, level int) uint64 { return binary.BigEndian.Uint64(k.digest(label, hash, level, -1, "")) } func (p *Processor) Ext() string { return "." + p.images.Format }