2017-11-05 21:35:46 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2017-07-31 18:22:10 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2020-12-04 12:40:42 +00:00
|
|
|
"crypto/sha1" // #nosec
|
2017-07-31 18:22:10 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
2019-06-30 22:58:18 +00:00
|
|
|
|
2020-08-08 23:31:02 +00:00
|
|
|
"k8s.io/klog/v2"
|
2017-07-31 18:22:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SHA1 returns the SHA1 of a file.
|
|
|
|
func SHA1(filename string) string {
|
2020-12-04 12:40:42 +00:00
|
|
|
hasher := sha1.New() // #nosec
|
2017-07-31 18:22:10 +00:00
|
|
|
s, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2020-09-27 20:32:40 +00:00
|
|
|
klog.ErrorS(err, "Error reading file", "path", filename)
|
2017-07-31 18:22:10 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
hasher.Write(s)
|
|
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
}
|