1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 // Sorted by preference order.
28 var HashesDefault = []string{
32 "shake128:goshake128",
33 "shake256:goshake256",
36 "streebog-256:streebog256sum",
37 "streebog-512:streebog512sum",
48 func NewHasher(hashes string) *Hasher {
50 for _, hc := range strings.Split(hashes, ",") {
51 cols := strings.SplitN(hc, ":", 2)
52 name, cmdline := cols[0], cols[1]
53 cmd := exec.Command(cmdline)
54 in, err := cmd.StdinPipe()
58 out, err := cmd.StdoutPipe()
62 h.Names = append(h.Names, name)
63 h.Ins = append(h.Ins, in)
64 h.Outs = append(h.Outs, out)
65 h.Cmds = append(h.Cmds, cmd)
70 func (h *Hasher) Start() {
71 for _, cmd := range h.Cmds {
72 if err := cmd.Start(); err != nil {
78 func (h *Hasher) Write(p []byte) (n int, err error) {
79 h.wg.Add(len(h.Names))
80 for _, in := range h.Ins {
81 go func(in io.WriteCloser) {
82 if _, err := io.Copy(in, bytes.NewReader(p)); err != nil {
92 func (h *Hasher) Sums() []Hash {
93 sums := make([]Hash, 0, len(h.Names))
94 for i, name := range h.Names {
95 if err := h.Ins[i].Close(); err != nil {
98 dgst, err := io.ReadAll(h.Outs[i])
102 sums = append(sums, Hash{Type: name, Hash: string(dgst[:len(dgst)-1])})
103 if err = h.Cmds[i].Wait(); err != nil {