]> Sergey Matveev's repositories - meta4ra.git/blobdiff - cmd/meta4-create/main.go
Unify copyright comment format
[meta4ra.git] / cmd / meta4-create / main.go
index ea0a776086014ce50817dc0431742e4e1b8ee942..1df7143258c9b1766d8b52c25a0d9f78f3e1d065 100644 (file)
@@ -1,94 +1,42 @@
-/*
-meta4a -- Metalink 4.0 creator
-Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
-
-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 <http://www.gnu.org/licenses/>.
-*/
+// meta4ra -- Metalink 4.0 utilities
+// Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// 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 <http://www.gnu.org/licenses/>.
 
 // Metalink 4.0 creator
 package main
 
 import (
        "bufio"
-       "crypto/sha256"
-       "crypto/sha512"
-       "encoding/hex"
        "encoding/xml"
        "flag"
-       "hash"
        "io"
        "log"
        "os"
-       "sync"
+       "path"
+       "strings"
        "time"
 
-       "go.cypherpunks.ru/gogost/v5/gost34112012256"
-       "go.cypherpunks.ru/gogost/v5/gost34112012512"
        "go.stargrave.org/meta4ra"
 )
 
-type MultiHasher struct {
-       sha256h      hash.Hash
-       sha512h      hash.Hash
-       streebog256h hash.Hash
-       streebog512h hash.Hash
-       g            sync.WaitGroup
-}
-
-func NewMultiHasher() *MultiHasher {
-       return &MultiHasher{
-               sha256h:      sha256.New(),
-               sha512h:      sha512.New(),
-               streebog256h: gost34112012256.New(),
-               streebog512h: gost34112012512.New(),
-       }
-}
-
-func (h *MultiHasher) Write(p []byte) (n int, err error) {
-       h.g.Add(4)
-       go func() {
-               if _, err := h.sha256h.Write(p); err != nil {
-                       panic(err)
-               }
-               h.g.Done()
-       }()
-       go func() {
-               if _, err := h.sha512h.Write(p); err != nil {
-                       panic(err)
-               }
-               h.g.Done()
-       }()
-       go func() {
-               if _, err := h.streebog256h.Write(p); err != nil {
-                       panic(err)
-               }
-               h.g.Done()
-       }()
-       go func() {
-               if _, err := h.streebog512h.Write(p); err != nil {
-                       panic(err)
-               }
-               h.g.Done()
-       }()
-       h.g.Wait()
-       return len(p), nil
-}
-
 func main() {
        fn := flag.String("fn", "", "Filename")
        mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
        desc := flag.String("desc", "", "Description")
-       sig := flag.String("sig", "", "Path to signature file")
+       sigPGP := flag.String("sig-pgp", "", "Path to OpenPGP .asc signature file")
+       sigSSH := flag.String("sig-ssh", "", "Path to OpenSSH .sig signature file")
+       hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
        torrent := flag.String("torrent", "", "Torrent URL")
        log.SetFlags(log.Lshortfile)
        flag.Parse()
@@ -99,34 +47,40 @@ func main() {
        for _, u := range flag.Args() {
                urls = append(urls, meta4ra.URL{URL: u})
        }
+       h := meta4ra.NewHasher(*hashes)
+       h.Start()
        br := bufio.NewReaderSize(os.Stdin, 1<<20)
        buf := make([]byte, 1<<20)
-       h := NewMultiHasher()
        size, err := io.CopyBuffer(h, br, buf)
        if err != nil {
                log.Fatalln(err)
        }
        f := meta4ra.File{
-               Name:        *fn,
+               Name:        path.Base(*fn),
                Description: *desc,
                Size:        uint64(size),
                URLs:        urls,
-               Hashes: []meta4ra.Hash{
-                       {Type: meta4ra.HashSHA256, Hash: hex.EncodeToString(h.sha256h.Sum(nil))},
-                       {Type: meta4ra.HashSHA512, Hash: hex.EncodeToString(h.sha512h.Sum(nil))},
-                       {Type: meta4ra.HashStreebog256, Hash: hex.EncodeToString(h.streebog256h.Sum(nil))},
-                       {Type: meta4ra.HashStreebog512, Hash: hex.EncodeToString(h.streebog512h.Sum(nil))},
-               },
+               Hashes:      h.Sums(),
        }
-       if *sig != "" {
-               sigData, err := os.ReadFile(*sig)
+       if *sigPGP != "" {
+               sigData, err := os.ReadFile(*sigPGP)
                if err != nil {
                        log.Fatalln(err)
                }
-               f.Signature = &meta4ra.Signature{
-                       MediaType: meta4ra.GPGSigMediaType,
+               f.Signature = append(f.Signature, meta4ra.Signature{
+                       MediaType: meta4ra.SigMediaTypePGP,
                        Signature: "\n" + string(sigData),
+               })
+       }
+       if *sigSSH != "" {
+               sigData, err := os.ReadFile(*sigSSH)
+               if err != nil {
+                       log.Fatalln(err)
                }
+               f.Signature = append(f.Signature, meta4ra.Signature{
+                       MediaType: meta4ra.SigMediaTypeSSH,
+                       Signature: "\n" + string(sigData),
+               })
        }
        if *torrent != "" {
                f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}