| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | ) |
| 8 | |
| 9 | // Digest is the export's canonical content digest: SHA-256, in hex, over |
| 10 | // the export's JSON with every display-time field cleared. Those fields |
| 11 | // are Collected and each claim's AgeSeconds and Stale, which change with |
| 12 | // the clock and not with the valley. Signing time is the manifest's, and |
| 13 | // is outside the export altogether. So two exports read from the same |
| 14 | // valley state have one digest, and the publisher uploads only when the |
| 15 | // digest changes or the receiver says it lacks something. |
| 16 | func (e *Export) Digest() (string, error) { |
| 17 | c := *e |
| 18 | c.Collected = "" |
| 19 | c.Projects = make([]Project, len(e.Projects)) |
| 20 | for i, p := range e.Projects { |
| 21 | p.Knots = make([]Knot, len(e.Projects[i].Knots)) |
| 22 | for j, k := range e.Projects[i].Knots { |
| 23 | if k.Claim != nil { |
| 24 | claim := *k.Claim |
| 25 | claim.AgeSeconds, claim.Stale = 0, false |
| 26 | k.Claim = &claim |
| 27 | } |
| 28 | p.Knots[j] = k |
| 29 | } |
| 30 | c.Projects[i] = p |
| 31 | } |
| 32 | // Every list in an export is a slice in a fixed order and it holds no |
| 33 | // map, so its JSON is one byte string. |
| 34 | b, err := json.Marshal(&c) |
| 35 | if err != nil { |
| 36 | return "", err |
| 37 | } |
| 38 | sum := sha256.Sum256(b) |
| 39 | return hex.EncodeToString(sum[:]), nil |
| 40 | } |