mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-27 10:52:52 +00:00
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
/*
|
|
Copyright 2022 The KubeSphere 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 hash implements hash utilities
|
|
package hash
|
|
|
|
import (
|
|
"crypto/md5" // #nosec
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// FileMD5 count file md5
|
|
func FileMD5(path string) (string, error) {
|
|
file, err := os.Open(filepath.Clean(path))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
m := md5.New() // #nosec
|
|
if _, err := io.Copy(m, file); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fileMd5 := fmt.Sprintf("%x", m.Sum(nil))
|
|
return fileMd5, nil
|
|
}
|
|
|
|
// FileSHA256 count file sha256
|
|
func FileSHA256(path string) (string, error) {
|
|
file, err := os.Open(filepath.Clean(path))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
s := sha256.New()
|
|
if _, err := io.Copy(s, file); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return fmt.Sprintf("%x", s.Sum(nil)), nil
|
|
}
|