-
Notifications
You must be signed in to change notification settings - Fork 57
/
fingerprint.go
66 lines (55 loc) · 1.73 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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package resource
import (
stdhash "hash"
"io"
"github.com/juju/errors"
"github.com/juju/utils/v3/hash"
)
var newHash, validateSum = hash.SHA384()
// Fingerprint represents the unique fingerprint value of a resource's data.
type Fingerprint struct {
hash.Fingerprint
}
// NewFingerprint returns wraps the provided raw fingerprint bytes.
// This function roundtrips with Fingerprint.Bytes().
func NewFingerprint(raw []byte) (Fingerprint, error) {
fp, err := hash.NewFingerprint(raw, validateSum)
if err != nil {
return Fingerprint{}, errors.Trace(err)
}
return Fingerprint{fp}, nil
}
// ParseFingerprint returns wraps the provided raw fingerprint string.
// This function roundtrips with Fingerprint.String().
func ParseFingerprint(raw string) (Fingerprint, error) {
fp, err := hash.ParseHexFingerprint(raw, validateSum)
if err != nil {
return Fingerprint{}, errors.Trace(err)
}
return Fingerprint{fp}, nil
}
// GenerateFingerprint returns the fingerprint for the provided data.
func GenerateFingerprint(reader io.Reader) (Fingerprint, error) {
fp, err := hash.GenerateFingerprint(reader, newHash)
if err != nil {
return Fingerprint{}, errors.Trace(err)
}
return Fingerprint{fp}, nil
}
// Fingerprint is a hash that may be used to generate fingerprints.
type FingerprintHash struct {
stdhash.Hash
}
// NewFingerprintHash returns a hash that may be used to create fingerprints.
func NewFingerprintHash() *FingerprintHash {
return &FingerprintHash{
Hash: newHash(),
}
}
// Fingerprint returns the current fingerprint of the hash.
func (fph FingerprintHash) Fingerprint() Fingerprint {
fp := hash.NewValidFingerprint(fph)
return Fingerprint{fp}
}