This repository was archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfingerprint.go
410 lines (385 loc) · 10.7 KB
/
fingerprint.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2018 IBM Corporation
// Licensed under the Apache License, Version 2.0. See LICENSE file.
package record
import (
"io"
"os"
"strings"
"github.com/IBM/binprint/hash"
)
// Fingerprint is the core of binprint representing a universally unique (when
// sufficiently populated) identifier for any blob of bytes (typically a file).
type Fingerprint struct {
GitSHA hash.GitShaDigest `yaml:",omitempty"`
MD5 hash.MD5Digest `yaml:",omitempty"`
SHA1 hash.SHA1Digest `yaml:",omitempty"`
SHA256 hash.SHA256Digest `yaml:",omitempty"`
SHA384 hash.SHA384Digest `yaml:",omitempty"`
SHA512 hash.SHA512Digest `yaml:",omitempty"`
Hwy64 hash.Highway64Digest `yaml:",omitempty"`
Hwy128 hash.Highway128Digest `yaml:",omitempty"`
Hwy256 hash.Highway256Digest `yaml:",omitempty"`
Size int64 `yaml:",omitempty"`
embeddedCacheID `yaml:"-"`
}
var self *Fingerprint
// Self returns a Fingerprint of the executable being run. If an error is not
// returned, the all subsequent calls will return the same value without
// re-calculating it.
func Self() (*Fingerprint, error) {
if self != nil {
return self, nil
}
execPath, err := os.Executable()
if err != nil {
return nil, err
}
stat, err := os.Stat(execPath)
if err != nil {
return nil, err
}
file, err := os.Open(execPath)
if err != nil {
return nil, err
}
defer file.Close()
fp := new(Fingerprint)
if err := fp.CalculateSums(file, stat.Size()); err != nil {
return nil, err
}
self = fp
return self, nil
}
// SerializedFingerprint is a variant of Fingerprint that is suitable for
// serializing with a serialization specific numeric id
type SerializedFingerprint struct {
ID uint64
Fingerprint `yaml:",inline"`
}
// GetDigest returns an existing hash.Digest calculated using the given
// algorithm. If the algorithm is not valid then nil is returned.
func (f Fingerprint) GetDigest(alg string) hash.Digest {
switch alg {
case "git", "gitsha":
return f.GitSHA
case "md5":
return f.MD5
case "sha1":
return f.SHA1
case "sha256":
return f.SHA256
case "sha384":
return f.SHA384
case "sha512":
return f.SHA512
case "hwy64bp", "hwy64":
return f.Hwy64
case "hwy128bp", "hwy128":
return f.Hwy128
case "hwy256bp", "hwy256":
return f.Hwy256
default:
return nil
}
}
// HasDigest checks if the hash.Digest matches any of the Fingerprint's
// hash.Digests
func (f Fingerprint) HasDigest(other hash.Digest) bool {
switch o := other.(type) {
case hash.GitShaDigest:
return f.GitSHA == o
// case hash.ShortGitShaDigest:
// return o.Matches(f.GitSHA)
case hash.MD5Digest:
return f.MD5 == o
case hash.SHA1Digest:
return f.SHA1 == o
case hash.SHA256Digest:
return f.SHA256 == o
case hash.SHA384Digest:
return f.SHA384 == o
case hash.SHA512Digest:
return f.SHA512 == o
case hash.Highway64Digest:
return f.Hwy64 == o
case hash.Highway128Digest:
return f.Hwy128 == o
case hash.Highway256Digest:
return f.Hwy256 == o
default:
return false
}
}
// CalculateSums calculates any sums missing on the Fingerprint using the
// provided io.Reader and given file size. If the given size is <= 0 then a
// gitsha is not calculated. Any sums that are already set (have
// non-zero-values) are not overwritten and their hash is not recalculated or
// verified.
func (f *Fingerprint) CalculateSums(data io.Reader, size int64) error {
hashers := []hash.AsyncHash{}
if f.Size == 0 && size != 0 {
f.Size = size
}
// if fp != nil {
// return nil
// }
if f.GitSHA.IsZero() && f.Size >= 0 {
hashers = append(hashers, hash.NewGitShaHasher("blob", f.Size))
}
if f.MD5.IsZero() {
hashers = append(hashers, hash.NewMD5Hasher())
}
if f.SHA1.IsZero() {
hashers = append(hashers, hash.NewSHA1Hasher())
}
if f.SHA256.IsZero() {
hashers = append(hashers, hash.NewSHA256Hasher())
}
if f.SHA384.IsZero() {
hashers = append(hashers, hash.NewSHA384Hasher())
}
if f.SHA512.IsZero() {
hashers = append(hashers, hash.NewSHA512Hasher())
}
if f.Hwy64.IsZero() {
hashers = append(hashers, hash.NewHighway64Hasher())
}
if f.Hwy128.IsZero() {
hashers = append(hashers, hash.NewHighway128Hasher())
}
if f.Hwy256.IsZero() {
hashers = append(hashers, hash.NewHighway256Hasher())
}
if len(hashers) == 0 {
// log.Println("No hash digests missing")
return nil
}
// golang has no generics and the type of a slice is its type, not the container of its elements
// so even though hash.AsyncHasher satisfies io.Writer, a slice of them is not a slice of io.Writer :-(
writers := make([]io.Writer, len(hashers))
for i, h := range hashers {
writers[i] = h
}
// Calculate all the hashes at once!
hashedLength, err := io.Copy(io.MultiWriter(writers...), data)
if err != nil {
log.Debug("Error while copying data to hasher:", err)
// panic(err)
// log.Fatal(err)
}
// close each hasher to finalize its digest, then store the result
for _, h := range hashers {
h.Close()
}
for _, h := range hashers {
if err != nil {
// we just need to consume
d := <-h.Done()
log.Debugf("In error state, dropping hash: %#v\n", d)
continue
}
switch d := (<-h.Done()).(type) {
case hash.GitShaDigest:
f.GitSHA = d
case *hash.GitShaDigest:
f.GitSHA = *d
case hash.MD5Digest:
f.MD5 = d
case *hash.MD5Digest:
f.MD5 = *d
case hash.SHA1Digest:
f.SHA1 = d
case *hash.SHA1Digest:
f.SHA1 = *d
case hash.SHA256Digest:
f.SHA256 = d
case *hash.SHA256Digest:
f.SHA256 = *d
case hash.SHA384Digest:
f.SHA384 = d
case *hash.SHA384Digest:
f.SHA384 = *d
case hash.SHA512Digest:
f.SHA512 = d
case *hash.SHA512Digest:
f.SHA512 = *d
case hash.Highway64Digest:
f.Hwy64 = d
case *hash.Highway64Digest:
f.Hwy64 = *d
case hash.Highway128Digest:
f.Hwy128 = d
case *hash.Highway128Digest:
f.Hwy128 = *d
case hash.Highway256Digest:
f.Hwy256 = d
case *hash.Highway256Digest:
f.Hwy256 = *d
default:
log.Debugf("Received unknown digest: %#v\n", d)
}
}
if err == nil && size != 0 && size != hashedLength {
log.Debugf("Something went horribly wrong. We only hashed %d bytes of %d!", hashedLength, size)
}
return err
}
// Is performs a full or partial match against the argument. If the argument is
// another Fingerprint then the fingerprints are considered equivalent if they
// have any matching non-zero digests. If the argument is a digest then it is
// matched against the corresponding digest in the Fingerprint. If the argument
// is a File then a comparison is made against the Fingerprint of that File.
func (f *Fingerprint) Is(other interface{}) bool {
var of *Fingerprint
// log.Println("is %s:%s == %s:%s?", f.Name(), f.GitSHA.String(), other.Name(), other.(File).GitSHA.String())
switch o := other.(type) {
case hash.Digest:
return f.HasDigest(o)
case Fingerprint:
of = &o
case *Fingerprint:
of = o
case File:
of = o.Fingerprint
case *File:
of = o.Fingerprint
default:
return false
}
if f == of {
return true
}
// cheap negative case first
if f.Size != 0 && of.Size != 0 && f.Size != of.Size {
return false
}
if !f.GitSHA.IsZero() && f.GitSHA == of.GitSHA {
return true
}
// TODO: consider protecting against collissions here by requiring a certain match confidence
if !f.MD5.IsZero() && f.MD5 == of.MD5 {
return true
}
// ditto
if !f.Hwy64.IsZero() && f.Hwy64 == of.Hwy64 {
return true
}
if !f.Hwy128.IsZero() && f.Hwy128 == of.Hwy128 {
return true
}
if !f.Hwy256.IsZero() && f.Hwy256 == of.Hwy256 {
return true
}
if !f.SHA1.IsZero() && f.SHA1 == of.SHA1 {
return true
}
if !f.SHA256.IsZero() && f.SHA256 == of.SHA256 {
return true
}
if !f.SHA384.IsZero() && f.SHA384 == of.SHA384 {
return true
}
if !f.SHA512.IsZero() && f.SHA512 == of.SHA512 {
return true
}
return false
}
// SRI returns a string of space separated subresource integrity values, which
// are base64 encoded hashes prefixed with the name of the hash algorithm. This
// multi-value string can be used as-is in the "integrity" attribute of a
// <script> tag so that the browser can perform integrity checks when
// downloading scripts from potentially untrusted 3rd party CDNs. NPM has also
// adopted sha512 SRI values for the "dist.integrity" property in package
// manifests for hosted packages as well as in lock files (package-lock.json,
// npm-shrinkwrap.json). For more information see:
// - https://w3c.github.io/webappsec-subresource-integrity/
// - https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
func (f Fingerprint) SRI() string {
sums := []string{
// only include the hashes that a browser is at least somewhat likely to support
f.MD5.SRI(),
f.SHA1.SRI(),
f.SHA256.SRI(),
f.SHA384.SRI(),
f.SHA512.SRI(),
}
return strings.Join(sums, " ")
}
func (f Fingerprint) String() string {
// TODO: what's a reasonable string representation of a fingerprint consisting of multiple hashes?
hashes := []string{}
if !f.GitSHA.IsZero() {
hashes = append(hashes, "git:"+f.GitSHA.String())
}
if !f.MD5.IsZero() {
hashes = append(hashes, "md5:"+f.MD5.String())
}
if !f.SHA1.IsZero() {
hashes = append(hashes, "sha1:"+f.SHA1.String())
}
if !f.SHA256.IsZero() {
hashes = append(hashes, "sha256:"+f.SHA256.String())
}
if !f.SHA384.IsZero() {
hashes = append(hashes, "sha384:"+f.SHA384.String())
}
if !f.SHA512.IsZero() {
hashes = append(hashes, "sha512:"+f.SHA512.String())
}
if !f.Hwy64.IsZero() {
hashes = append(hashes, "hwy64:"+f.Hwy64.String())
}
if !f.Hwy128.IsZero() {
hashes = append(hashes, "hwy128:"+f.Hwy128.String())
}
if !f.Hwy256.IsZero() {
hashes = append(hashes, "hwy256:"+f.Hwy256.String())
}
return strings.Join(hashes, " ")
}
// UpdateWith fills in any digests that are missing with the digests provided by
// `other`. Returns the number of hashes that are copied.
func (f *Fingerprint) UpdateWith(of *Fingerprint) int {
updates := 0
if f.Size == 0 && of.Size != 0 {
f.Size = of.Size
updates++
}
if f.GitSHA.IsZero() && f.GitSHA != of.GitSHA {
f.GitSHA = of.GitSHA
updates++
}
if f.MD5.IsZero() && f.MD5 != of.MD5 {
f.MD5 = of.MD5
updates++
}
if f.SHA1.IsZero() && f.SHA1 != of.SHA1 {
f.SHA1 = of.SHA1
updates++
}
if f.SHA256.IsZero() && f.SHA256 != of.SHA256 {
f.SHA256 = of.SHA256
updates++
}
if f.SHA384.IsZero() && f.SHA384 != of.SHA384 {
f.SHA384 = of.SHA384
updates++
}
if f.SHA512.IsZero() && f.SHA512 != of.SHA512 {
f.SHA512 = of.SHA512
updates++
}
if f.Hwy64.IsZero() && f.Hwy64 != of.Hwy64 {
f.Hwy64 = of.Hwy64
updates++
}
if f.Hwy128.IsZero() && f.Hwy128 != of.Hwy128 {
f.Hwy128 = of.Hwy128
updates++
}
if f.Hwy256.IsZero() && f.Hwy256 != of.Hwy256 {
f.Hwy256 = of.Hwy256
updates++
}
return updates
}