File returns a hex-encoded hash of a file's contents.
(path string)
| 33 | |
| 34 | // File returns a hex-encoded hash of a file's contents. |
| 35 | func File(path string) (string, error) { |
| 36 | f, err := os.Open(path) |
| 37 | if errors.Is(err, os.ErrNotExist) { |
| 38 | return "", nil |
| 39 | } |
| 40 | if err != nil { |
| 41 | return "", err |
| 42 | } |
| 43 | defer f.Close() |
| 44 | |
| 45 | h := newHash() |
| 46 | if _, err := io.Copy(h, f); err != nil { |
| 47 | return "", err |
| 48 | } |
| 49 | return hex.EncodeToString(h.Sum(nil)), nil |
| 50 | } |
| 51 | |
| 52 | // JSON marshals a to JSON and returns its hex-encoded hash. |
| 53 | func JSON(a any) (string, error) { |