]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-check/main.go
meta4-check
[meta4ra.git] / cmd / meta4-check / main.go
1 /*
2 meta4a -- Metalink 4.0 checker
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Metalink 4.0 checker
19 package main
20
21 import (
22         "bufio"
23         "crypto/sha256"
24         "crypto/sha512"
25         "encoding/hex"
26         "encoding/xml"
27         "flag"
28         "fmt"
29         "hash"
30         "io"
31         "io/fs"
32         "io/ioutil"
33         "log"
34         "os"
35
36         "go.stargrave.org/meta4ra"
37 )
38
39 func main() {
40         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
41         log.SetFlags(log.Lshortfile)
42         flag.Parse()
43         sha256Hasher := sha256.New()
44         sha512Hasher := sha512.New()
45         bad := false
46         for _, metaPath := range flag.Args() {
47                 data, err := ioutil.ReadFile(metaPath)
48                 if err != nil {
49                         log.Fatalln(err)
50                 }
51                 var meta meta4ra.Metalink
52                 err = xml.Unmarshal(data, &meta)
53                 if err != nil {
54                         log.Fatalln(err)
55                 }
56                 for _, f := range meta.Files {
57                         if f.Signature != nil && *extractSig {
58                                 if err = ioutil.WriteFile(
59                                         f.Name+".asc",
60                                         []byte(f.Signature.Signature),
61                                         fs.FileMode(0666),
62                                 ); err != nil {
63                                         fmt.Println("Error:", f.Name, "can not save signature:", err)
64                                         bad = true
65                                 }
66                         }
67                         var sha256Digest string
68                         var sha512Digest string
69                         fd, err := os.Open(f.Name)
70                         if err != nil {
71                                 continue
72                         }
73                         for _, h := range f.Hashes {
74                                 switch h.Type {
75                                 case meta4ra.HashSHA256:
76                                         sha256Digest = h.Hash
77                                 case meta4ra.HashSHA512:
78                                         sha512Digest = h.Hash
79                                 }
80                         }
81                         var digestTheir string
82                         var digestName string
83                         var hasher hash.Hash
84                         if sha512Digest != "" {
85                                 digestName = meta4ra.HashSHA512
86                                 digestTheir = sha512Digest
87                                 hasher = sha512Hasher
88                         } else if sha256Digest != "" {
89                                 digestName = meta4ra.HashSHA256
90                                 digestTheir = sha256Digest
91                                 hasher = sha256Hasher
92                         } else {
93                                 fd.Close()
94                                 fmt.Println("Error:", f.Name, "no satisfiable hash algorithm found")
95                                 bad = true
96                                 continue
97                         }
98                         hasher.Reset()
99                         _, err = io.Copy(hasher, bufio.NewReader(fd))
100                         fd.Close()
101                         if err != nil {
102                                 fmt.Println("Error:", f.Name, err)
103                                 bad = true
104                                 continue
105                         }
106                         digestOur := hex.EncodeToString(hasher.Sum(nil))
107                         if digestOur == digestTheir {
108                                 fmt.Println(f.Name, digestName, "good")
109                         } else {
110                                 fmt.Println(
111                                         "Hash does not match:", f.Name, digestName,
112                                         "our:", digestOur,
113                                         "their:", digestTheir,
114                                 )
115                                 bad = true
116                                 continue
117                         }
118                 }
119         }
120         if bad {
121                 os.Exit(1)
122         }
123 }