X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=internal%2Fbuiltin.go;fp=internal%2Fbuiltin.go;h=a65e889e14e2bc7d37181818222c49b67712f6be;hb=be69a4fc0c240c2772a0f0bf0955b39783e4c48a;hp=0000000000000000000000000000000000000000;hpb=ce902a58a32f42801603475c67dd75da86d4502a;p=meta4ra.git diff --git a/internal/builtin.go b/internal/builtin.go new file mode 100644 index 0000000..a65e889 --- /dev/null +++ b/internal/builtin.go @@ -0,0 +1,59 @@ +// meta4ra -- Metalink 4.0 utilities +// Copyright (C) 2021-2024 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package internal + +import ( + "crypto/sha256" + "crypto/sha512" + "encoding/hex" + "hash" + "io" +) + +const BuiltinCmd = "builtin" + +var ( + BuiltinHashes map[string]func() hash.Hash = map[string]func() hash.Hash{ + "sha-256": sha256.New, + "sha-512": sha512.New, + } + HashesDefault = "sha-512:builtin,sha-256:builtin" +) + +type BuiltinHasher struct { + h hash.Hash + finished bool +} + +func (h *BuiltinHasher) Write(p []byte) (int, error) { + return h.h.Write(p) +} + +func (h *BuiltinHasher) Read(p []byte) (int, error) { + if h.finished { + return 0, io.EOF + } + if len(p) < 2*h.h.Size() { + panic("too small buffer for BuiltinHasher.h.Sum()") + } + hex.Encode(p, h.h.Sum(nil)) + h.finished = true + return hex.EncodedLen(h.h.Size()), nil +} + +func (h *BuiltinHasher) Close() error { + return nil +}