func verifyHash()

in getdeps/fetch.go [155:191]


func verifyHash(data []byte, expectedHash string) (string, error) {
	var ct string
	expectedHash = strings.ToLower(expectedHash)
	if expectedHash == "" {
		// Hash update mode
		ct = "sha256"
	} else {
		parts := strings.Split(expectedHash, ":")
		if len(parts) != 2 {
			return "", fmt.Errorf("unsupported hash format %q", expectedHash)
		}
		expectedHashType := parts[0]
		switch expectedHashType {
		case "sha256":
			ct = "sha256"
		default:
			return "", fmt.Errorf("unsupported hash type %q", expectedHashType)
		}
	}

	var csHex string
	switch ct {
	case "sha256":
		cs := sha256.Sum256(data)
		csHex = strings.ToLower(hex.EncodeToString(cs[:]))
	}

	actualHash := fmt.Sprintf("%s:%s", ct, csHex)

	var err error

	if expectedHash != "" && actualHash != expectedHash {
		return actualHash, fmt.Errorf("hash mismatch: expected %q, got %q", expectedHash, actualHash)
	}

	return actualHash, err
}