JSONFile compacts the JSON in a file and returns its hex-encoded hash.
(path string)
| 60 | |
| 61 | // JSONFile compacts the JSON in a file and returns its hex-encoded hash. |
| 62 | func JSONFile(path string) (string, error) { |
| 63 | b, err := os.ReadFile(path) |
| 64 | if errors.Is(err, os.ErrNotExist) { |
| 65 | return "", nil |
| 66 | } |
| 67 | if err != nil { |
| 68 | return "", err |
| 69 | } |
| 70 | buf := &bytes.Buffer{} |
| 71 | if err := json.Compact(buf, b); err != nil { |
| 72 | return "", redact.Errorf("compact json for hashing: %v", err) |
| 73 | } |
| 74 | return Bytes(buf.Bytes()), nil |
| 75 | } |
| 76 | |
| 77 | func newHash() hash.Hash { return sha256.New() } |