-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
178 lines (151 loc) · 3.9 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// The CDAS Traceability Commandline Interface utility.
//
// This package implements the various crypotgraphic checksum algorithms.
//
// Copyright 2023 Cloudflight Austria GmbH
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"encoding/hex"
"io"
"os"
"strings"
"golang.org/x/crypto/sha3"
// "crypto/sha256"
"github.com/minio/sha256-simd"
"github.com/zeebo/blake3"
"github.com/gobwas/glob"
log "github.com/sirupsen/logrus"
)
type Algorithm string
const (
SHA256 Algorithm = "SHA256"
SHA3 Algorithm = "SHA3"
BLAKE3 Algorithm = "BLAKE3"
)
func EncodeHash(checksum []byte) string {
return hex.EncodeToString(checksum)
}
func DecodeHash(checksum string) ([]byte, error) {
return hex.DecodeString(checksum)
}
func HashContents(filename string, include_pattern glob.Glob, algorithm Algorithm) *map[string]string {
if strings.HasSuffix(filename, ".zip") {
return HashZipContents(filename, include_pattern, algorithm)
}
if strings.HasSuffix(filename, ".tar") {
return HashTarContents(filename, include_pattern, algorithm)
}
log.Fatalf("Unable to load contents from file '%s', can only handle zip and tar archives.", filename)
return nil
}
func HashZipContents(filename string, include_pattern glob.Glob, algorithm Algorithm) *map[string]string {
archive, err := zip.OpenReader(filename)
if err != nil {
log.Fatal(err)
}
defer archive.Close()
var contents = make(map[string]string)
for _, f := range archive.File {
if !includeArchiveContent(f.Name, include_pattern) {
continue
}
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
contents[f.Name] = EncodeHash(hashArchiveContent(f.Name, rc, algorithm))
rc.Close()
}
return &contents
}
func HashTarContents(filename string, include_pattern glob.Glob, algorithm Algorithm) *map[string]string {
archive, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer archive.Close()
reader := tar.NewReader(archive)
if err != nil {
log.Fatal(err)
}
var contents = make(map[string]string)
for {
f, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if !includeArchiveContent(f.Name, include_pattern) {
continue
}
contents[f.Name] = EncodeHash(hashArchiveContent(f.Name, reader, algorithm))
}
return &contents
}
func hashArchiveContent(name string, reader io.Reader, algorithm Algorithm) []byte {
sum := HashData(reader, algorithm)
log.Debugf("%x %s\n", sum, name)
return sum
}
func includeArchiveContent(name string, include_pattern glob.Glob) bool {
if strings.HasSuffix(name, "/") {
// log.Debugf("Skipping directory entry: '%s'", f.Name)
return false
}
if !include_pattern.Match(name) {
log.Debugf("Skipping element, does not match include pattern: %s", name)
return false
}
return true
}
func HashFile(filename string, algorithm Algorithm) ([]byte, int64) {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
sum, size := HashStream(file, algorithm)
log.Debugf("%x %s\n", sum, filename)
return sum, size
}
type ByteCounter struct {
Count int64
}
func (bc *ByteCounter) Write(p []byte) (n int, err error) {
count := len(p)
bc.Count += int64(count)
return count, nil
}
func HashStream(reader io.Reader, algorithm Algorithm) ([]byte, int64) {
bc := ByteCounter{}
sum := HashData(io.TeeReader(reader, &bc), algorithm)
return sum, bc.Count
}
func HashString(data string, algorithm Algorithm) []byte {
return HashData(strings.NewReader(data), algorithm)
}
func HashBytes(data []byte, algorithm Algorithm) []byte {
return HashData(bytes.NewReader(data), algorithm)
}
func HashData(src io.Reader, algorithm Algorithm) []byte {
switch algorithm {
case SHA3:
hasher := sha3.New256()
io.Copy(hasher, src)
return hasher.Sum(nil)
case SHA256:
hasher := sha256.New()
io.Copy(hasher, src)
return hasher.Sum(nil)
case BLAKE3:
hasher := blake3.New()
io.Copy(hasher, src)
return hasher.Sum(nil)
}
return []byte{}
}