lib/sha256: Smoke test the implementation on startup (hello OpenSUSE!)

This commit is contained in:
Jakob Borg 2016-12-28 12:15:51 +01:00
parent 54911d44c5
commit c69c3c7c36

View File

@ -9,6 +9,7 @@ package sha256
import ( import (
"crypto/rand" "crypto/rand"
cryptoSha256 "crypto/sha256" cryptoSha256 "crypto/sha256"
"encoding/hex"
"fmt" "fmt"
"hash" "hash"
"os" "os"
@ -66,6 +67,8 @@ func SelectAlgo() {
// implementation as it may be disabled for incompatibility reasons. // implementation as it may be disabled for incompatibility reasons.
cryptoPerf = cpuBenchOnce(benchmarkingIterations*benchmarkingDuration, cryptoSha256.New) cryptoPerf = cpuBenchOnce(benchmarkingIterations*benchmarkingDuration, cryptoSha256.New)
} }
verifyCorrectness()
} }
// Report prints a line with the measured hash performance rates for the // Report prints a line with the measured hash performance rates for the
@ -134,3 +137,24 @@ func formatRate(rate float64) string {
} }
return fmt.Sprintf("%.*f MB/s", decimals, rate) return fmt.Sprintf("%.*f MB/s", decimals, rate)
} }
func verifyCorrectness() {
// The currently selected algo should in fact perform a SHA256 calculation.
// $ echo "Syncthing Magic Testing Value" | openssl dgst -sha256 -hex
correct := "87f6cfd24131724c6ec43495594c5c22abc7d2b86bcc134bc6f10b7ec3dda4ee"
input := "Syncthing Magic Testing Value\n"
h := New()
h.Write([]byte(input))
sum := hex.EncodeToString(h.Sum(nil))
if sum != correct {
panic("sha256 is broken")
}
arr := Sum256([]byte(input))
sum = hex.EncodeToString(arr[:])
if sum != correct {
panic("sha256 is broken")
}
}