1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00
gocsaf/util/hash.go
Sascha L. Teichmann 8a1ebe0b7a
Add aggregator; improve itest workflow
* Factor JSON evaluation and  construction base URLs out of of checker.
* Move json path matching to util.
* Add csaf_aggregator (as additional command)
* Improve itest workflow to checkout the branch where it is running on.

resolve #105
resolve  #72

Co-authored-by: tschmidtb51 <65305130+tschmidtb51@users.noreply.github.com>
Co-authored-by: Bernhard Reiter <bernhard@intevation.de>
Co-authored-by: Fadi Abbud <fadi.abbud@intevation.de>
2022-05-10 18:12:38 +02:00

66 lines
1.5 KiB
Go

// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
package util
import (
"bufio"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"regexp"
)
var hexRe = regexp.MustCompile(`^([[:xdigit:]]+)`)
// HashFromReader reads a base 16 coded hash sum from a reader.
func HashFromReader(r io.Reader) ([]byte, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if m := hexRe.FindStringSubmatch(scanner.Text()); m != nil {
return hex.DecodeString(m[1])
}
}
return nil, scanner.Err()
}
// HashFromFile reads a base 16 coded hash sum from a file.
func HashFromFile(fname string) ([]byte, error) {
f, err := os.Open(fname)
if err != nil {
return nil, err
}
defer f.Close()
return HashFromReader(f)
}
// WriteHashToFile writes a hash of data to file fname.
func WriteHashToFile(fname, name string, h hash.Hash, data []byte) error {
if _, err := h.Write(data); err != nil {
return err
}
f, err := os.Create(fname)
if err != nil {
return err
}
fmt.Fprintf(f, "%x %s\n", h.Sum(nil), name)
return f.Close()
}
// WriteHashSumToFile writes a hash sum to file fname.
func WriteHashSumToFile(fname, name string, sum []byte) error {
f, err := os.Create(fname)
if err != nil {
return err
}
fmt.Fprintf(f, "%x %s\n", sum, name)
return f.Close()
}