/
hash.go
175 lines (144 loc) · 3.86 KB
/
hash.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hashing
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"strings"
"github.com/golang/glog"
"k8s.io/kops/pkg/try"
)
type HashAlgorithm string
const (
HashAlgorithmSHA256 HashAlgorithm = "sha256"
HashAlgorithmSHA1 HashAlgorithm = "sha1"
HashAlgorithmMD5 HashAlgorithm = "md5"
)
type Hash struct {
Algorithm HashAlgorithm
HashValue []byte
}
func (h *Hash) String() string {
return string(h.Algorithm) + ":" + h.Hex()
}
func (h *Hash) Hex() string {
return hex.EncodeToString(h.HashValue)
}
func (ha HashAlgorithm) NewHasher() hash.Hash {
switch ha {
case HashAlgorithmMD5:
return md5.New()
case HashAlgorithmSHA1:
return sha1.New()
case HashAlgorithmSHA256:
return sha256.New()
}
glog.Exitf("Unknown hash algorithm: %v", ha)
return nil
}
func (ha HashAlgorithm) FromString(s string) (*Hash, error) {
l := -1
switch ha {
case HashAlgorithmMD5:
l = 32
case HashAlgorithmSHA1:
l = 40
case HashAlgorithmSHA256:
l = 64
default:
return nil, fmt.Errorf("unknown hash algorithm: %q", ha)
}
if len(s) != l {
return nil, fmt.Errorf("invalid %q hash - unexpected length %d", ha, len(s))
}
hashValue, err := hex.DecodeString(s)
if err != nil {
return nil, fmt.Errorf("invalid hash %q - not hex", s)
}
return &Hash{Algorithm: ha, HashValue: hashValue}, nil
}
func FromString(s string) (*Hash, error) {
for _, ha := range []HashAlgorithm{HashAlgorithmMD5, HashAlgorithmSHA1, HashAlgorithmSHA256} {
prefix := fmt.Sprintf("%s:", ha)
if strings.HasPrefix(s, prefix) {
return ha.FromString(s[len(prefix):])
}
}
var ha HashAlgorithm
switch len(s) {
case 32:
ha = HashAlgorithmMD5
case 40:
ha = HashAlgorithmSHA1
case 64:
ha = HashAlgorithmSHA256
default:
return nil, fmt.Errorf("cannot determine algorithm for hash length: %d", len(s))
}
return ha.FromString(s)
}
func (ha HashAlgorithm) Hash(r io.Reader) (*Hash, error) {
hasher := ha.NewHasher()
_, err := copyToHasher(hasher, r)
if err != nil {
return nil, fmt.Errorf("error while hashing resource: %v", err)
}
return &Hash{Algorithm: ha, HashValue: hasher.Sum(nil)}, nil
}
func (ha HashAlgorithm) HashFile(p string) (*Hash, error) {
f, err := os.OpenFile(p, os.O_RDONLY, 0)
if err != nil {
if os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("error opening file %q: %v", p, err)
}
defer try.CloseFile(f)
return ha.Hash(f)
}
func HashesForResource(r io.Reader, hashAlgorithms []HashAlgorithm) ([]*Hash, error) {
var hashers []hash.Hash
var writers []io.Writer
for _, hashAlgorithm := range hashAlgorithms {
hasher := hashAlgorithm.NewHasher()
hashers = append(hashers, hasher)
writers = append(writers, hasher)
}
w := io.MultiWriter(writers...)
_, err := copyToHasher(w, r)
if err != nil {
return nil, fmt.Errorf("error while hashing resource: %v", err)
}
var hashes []*Hash
for i, hasher := range hashers {
hashes = append(hashes, &Hash{Algorithm: hashAlgorithms[i], HashValue: hasher.Sum(nil)})
}
return hashes, nil
}
func copyToHasher(dest io.Writer, src io.Reader) (int64, error) {
n, err := io.Copy(dest, src)
if err != nil {
return n, fmt.Errorf("error hashing data: %v", err)
}
return n, nil
}
func (l *Hash) Equal(r *Hash) bool {
return (l.Algorithm == r.Algorithm) && bytes.Equal(l.HashValue, r.HashValue)
}