]> Cypherpunks.ru repositories - gocheese.git/blobdiff - integrity.go
Refactor digest processing, BLAKE2b-256 support, cleanup non-SHA256 digests
[gocheese.git] / integrity.go
diff --git a/integrity.go b/integrity.go
new file mode 100644 (file)
index 0000000..a96d80e
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+GoCheese -- Python private package repository and caching proxy
+Copyright (C) 2019 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/>.
+*/
+
+package main
+
+import (
+       "bytes"
+       "crypto/sha256"
+       "fmt"
+       "io/ioutil"
+       "log"
+       "os"
+       "path/filepath"
+       "strings"
+)
+
+func goodIntegrity() bool {
+       dirs, err := ioutil.ReadDir(*root)
+       if err != nil {
+               log.Fatal(err)
+       }
+       hasher := sha256.New()
+       digest := make([]byte, sha256.Size)
+       isGood := true
+       var data []byte
+       var pkgName string
+       for _, dir := range dirs {
+               files, err := ioutil.ReadDir(filepath.Join(*root, dir.Name()))
+               if err != nil {
+                       log.Fatal(err)
+               }
+               for _, file := range files {
+                       if !strings.HasSuffix(file.Name(), "."+HashAlgoSHA256) {
+                               continue
+                       }
+                       pkgName = strings.TrimSuffix(file.Name(), "."+HashAlgoSHA256)
+                       data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), pkgName))
+                       if err != nil {
+                               if os.IsNotExist(err) {
+                                       continue
+                               }
+                               log.Fatal(err)
+                       }
+                       hasher.Write(data)
+                       data, err = ioutil.ReadFile(filepath.Join(*root, dir.Name(), file.Name()))
+                       if err != nil {
+                               log.Fatal(err)
+                       }
+                       if bytes.Compare(hasher.Sum(digest[:0]), data) == 0 {
+                               fmt.Println(pkgName, "GOOD")
+                       } else {
+                               isGood = false
+                               fmt.Println(pkgName, "BAD")
+                       }
+                       hasher.Reset()
+               }
+       }
+       return isGood
+}