-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathhash.go
More file actions
81 lines (69 loc) · 1.28 KB
/
Copy pathhash.go
File metadata and controls
81 lines (69 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package crypto
import (
"crypto/md5"
"crypto/sha3"
//nolint: gosec
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"embed"
"fmt"
"hash"
"io"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/interp"
//nolint: staticcheck
"golang.org/x/crypto/md4"
)
//go:embed hash.jq
var hashFS embed.FS
func init() {
interp.RegisterFunc1("_to_hash", toHash)
interp.RegisterFS(hashFS)
}
func hashFn(s string) hash.Hash {
switch s {
case "md4":
return md4.New()
case "md5":
return md5.New()
case "sha1":
return sha1.New()
case "sha256":
return sha256.New()
case "sha512":
return sha512.New()
case "sha3_224":
return sha3.New224()
case "sha3_256":
return sha3.New256()
case "sha3_384":
return sha3.New384()
case "sha3_512":
return sha3.New512()
default:
return nil
}
}
type toHashOpts struct {
Name string
}
func toHash(_ *interp.Interp, c any, opts toHashOpts) any {
inBR, err := interp.ToBitReader(c)
if err != nil {
return err
}
h := hashFn(opts.Name)
if h == nil {
return fmt.Errorf("unknown hash function %s", opts.Name)
}
if _, err := io.Copy(h, bitio.NewIOReader(inBR)); err != nil {
return err
}
outBR := bitio.NewBitReader(h.Sum(nil), -1)
bb, err := interp.NewBinaryFromBitReader(outBR, 8, 0)
if err != nil {
return err
}
return bb
}