package testonly

// This file implements the hashing functions that are part of a Trillian
// personality.

import (
	"crypto"
	"crypto/sha256"

	"github.com/google/trillian/merkle/rfc6962"
)

// Hasher is the default hasher for tests.
// TODO: Make this a custom algorithm to decouple hashing from coded defaults.
var Hasher = rfc6962.TreeHasher{Hash: crypto.SHA256}

// HashKey converts a map key into a map index using SHA256.
// This preserves tests that precomputed indexes based on SHA256.
func HashKey(key string) []byte {
	h := sha256.New()
	h.Write([]byte(key))
	return h.Sum(nil)
}

// TransparentHash returns a key that can be visually inspected.
// This supports testing where it was nice to see what the key was.
func TransparentHash(key string) []byte {
	if len(key) > sha256.Size {
		panic("key too long")
	}
	b := make([]byte, sha256.Size)
	copy(b, key)
	return b
}
