Dump writes the hash to a writer Format: [1 byte: type][variable: hash data]
(w io.Writer)
| 101 | // Dump writes the hash to a writer |
| 102 | // Format: [1 byte: type][variable: hash data] |
| 103 | func (h *ImageHash) Dump(w io.Writer) error { |
| 104 | // Write type byte |
| 105 | if err := binary.Write(w, binary.LittleEndian, h.hashType); err != nil { |
| 106 | return fmt.Errorf("failed to write hash type: %w", err) |
| 107 | } |
| 108 | |
| 109 | switch h.hashType { |
| 110 | case HashTypeSHA256: |
| 111 | if _, err := w.Write(h.sha256Hash[:]); err != nil { |
| 112 | return fmt.Errorf("failed to write SHA256 hash: %w", err) |
| 113 | } |
| 114 | |
| 115 | case HashTypeDct: |
| 116 | if err := h.dumpDctHash(w); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
| 120 | default: |
| 121 | return fmt.Errorf("unsupported hash type: %d", h.hashType) |
| 122 | } |
| 123 | |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | // LoadImageHash loads a hash from a reader |
| 128 | func LoadImageHash(r io.Reader) (*ImageHash, error) { |