Source file src/crypto/internal/cryptotest/hash.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cryptotest
     6  
     7  import (
     8  	"crypto/internal/boring"
     9  	"crypto/internal/fips140"
    10  	"hash"
    11  	"internal/testhash"
    12  	"io"
    13  	"math/rand"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  type MakeHash func() hash.Hash
    19  
    20  // TestHash performs a set of tests on hash.Hash implementations, checking the
    21  // documented requirements of Write, Sum, Reset, Size, and BlockSize.
    22  func TestHash(t *testing.T, mh MakeHash) {
    23  	t.Run("OutOfBounds", func(t *testing.T) {
    24  		start, end := BoundarySlices(t, 1000)
    25  		h := mh()
    26  		for i := range len(start) + 1 {
    27  			h.Write(start[:i])
    28  			h.Write(start[len(start)-i:])
    29  			h.Write(end[:i])
    30  			h.Write(end[len(end)-i:])
    31  		}
    32  		start, end = BoundarySlices(t, h.Size())
    33  		h.Sum(start[:0])
    34  		h.Sum(end[:0])
    35  	})
    36  
    37  	if boring.Enabled || fips140.Version() == "v1.0.0" {
    38  		testhash.TestHashWithoutClone(t, testhash.MakeHash(mh))
    39  		return
    40  	}
    41  	testhash.TestHash(t, testhash.MakeHash(mh))
    42  }
    43  
    44  func newRandReader(t *testing.T) io.Reader {
    45  	seed := time.Now().UnixNano()
    46  	t.Logf("Deterministic RNG seed: 0x%x", seed)
    47  	return rand.New(rand.NewSource(seed))
    48  }
    49  

View as plain text