MCPcopy Create free account
hub / github.com/AdRoll/baker / NewHash

Function NewHash

filter/hash.go:49–96  ·  view source on GitHub ↗
(cfg baker.FilterParams)

Source from the content-addressed store, hash-verified

47}
48
49func NewHash(cfg baker.FilterParams) (baker.Filter, error) {
50 dcfg := cfg.DecodedConfig.(*HashConfig)
51
52 src, ok := cfg.FieldByName(dcfg.SrcField)
53 if !ok {
54 return nil, fmt.Errorf("can't find the SrcField %s", dcfg.SrcField)
55 }
56
57 dst, ok := cfg.FieldByName(dcfg.DstField)
58 if !ok {
59 return nil, fmt.Errorf("can't find the DstField %s", dcfg.DstField)
60 }
61
62 h := &Hash{
63 src: src,
64 dst: dst,
65 }
66
67 switch dcfg.Function {
68 case "md5":
69 h.hash = func(b []byte) ([]byte, error) {
70 sum := md5.Sum(b)
71 return sum[:], nil
72 }
73 case "sha256":
74 h.hash = func(b []byte) ([]byte, error) {
75 sum := sha256.Sum256(b)
76 return sum[:], nil
77 }
78 default:
79 return nil, fmt.Errorf("unsupported hash function %s", dcfg.Function)
80 }
81
82 switch dcfg.Encoding {
83 case "hex":
84 h.encode = func(b []byte) ([]byte, error) {
85 dst := make([]byte, hex.EncodedLen(len(b)))
86 hex.Encode(dst, b)
87 return dst, nil
88 }
89 case "": // pass through
90 h.encode = func(b []byte) ([]byte, error) { return b, nil }
91 default:
92 return nil, fmt.Errorf("unsupported encoding %s", dcfg.Encoding)
93 }
94
95 return h, nil
96}
97
98func (h *Hash) Stats() baker.FilterStats {
99 return baker.FilterStats{

Callers 1

TestHashFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestHashFunction · 0.68