cmpHash computes the hash of the current file and compares it with the provided value.
(o *Opts)
| 124 | // cmpHash computes the hash of the current file and compares it with the |
| 125 | // provided value. |
| 126 | func cmpHash(o *Opts) (result bool, err error) { |
| 127 | hashType := "SHA1" |
| 128 | if len(o.Modifiers) > 0 { |
| 129 | hashType = o.Modifiers[0].Name |
| 130 | } |
| 131 | |
| 132 | hashFunc := transform.FindHash(hashType) |
| 133 | if hashFunc == nil { |
| 134 | return false, fmt.Errorf("unexpected hash algorithm %s", hashType) |
| 135 | } |
| 136 | h, err := transform.ComputeHash(o.File, o.Path, hashFunc()) |
| 137 | if err != nil { |
| 138 | return false, err |
| 139 | } |
| 140 | |
| 141 | switch o.Operator { |
| 142 | case tokenizer.Equals: |
| 143 | result = h == o.Value |
| 144 | case tokenizer.NotEquals: |
| 145 | result = h != o.Value |
| 146 | default: |
| 147 | err = &ErrUnsupportedOperator{o.Attribute, o.Operator} |
| 148 | } |
| 149 | return result, err |
| 150 | } |