You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package maxmind
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
func checksumFromFile(filename string) (string, error) {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
hash := sha256.New()
|
|
if _, err := io.Copy(hash, file); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return hex.EncodeToString(hash.Sum(nil)), nil
|
|
}
|
|
|
|
func checksumFromReader(reader io.Reader) (string, io.Reader, error) {
|
|
var b bytes.Buffer
|
|
|
|
hash := sha256.New()
|
|
if _, err := io.Copy(&b, io.TeeReader(reader, hash)); err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
return hex.EncodeToString(hash.Sum(nil)), &b, nil
|
|
}
|
|
|
|
func createFile(path string, content string) error {
|
|
file, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
if _, err = file.WriteString(content); err != nil {
|
|
return err
|
|
}
|
|
if err = file.Sync(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fileExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|