forked from barak/tarpoon
43 lines
580 B
Go
43 lines
580 B
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
type fileSha struct {
|
|
bytes []byte
|
|
size int64
|
|
}
|
|
|
|
func sha256File(path string) fileSha {
|
|
h := sha256.New()
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer f.Close()
|
|
fi, err := f.Stat()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
io.Copy(h, f)
|
|
return fileSha{
|
|
bytes: h.Sum(nil),
|
|
size: fi.Size(),
|
|
}
|
|
}
|
|
|
|
func (f fileSha) String() string {
|
|
return fmt.Sprintf("sha256:%x", f.bytes)
|
|
}
|
|
|
|
func (f fileSha) Name() string {
|
|
return fmt.Sprintf("%x", f.bytes)
|
|
}
|
|
|
|
func (f fileSha) Size() int64 {
|
|
return f.size
|
|
}
|