-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
traced_bcrypt.go
46 lines (38 loc) · 1.11 KB
/
traced_bcrypt.go
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
package tracing
import (
"context"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)
const (
HashOpName = "bcrypt.hash"
CompareOpName = "bcrypt.compare"
WorkFactorTagName = "bcrypt.workfactor"
)
// TracedBCrypt implements the Hasher interface
type TracedBCrypt struct {
WorkFactor int
}
func (b *TracedBCrypt) Hash(ctx context.Context, data []byte) ([]byte, error) {
span, _ := opentracing.StartSpanFromContext(ctx, HashOpName)
defer span.Finish()
span.SetTag(WorkFactorTagName, b.WorkFactor)
s, err := bcrypt.GenerateFromPassword(data, b.WorkFactor)
if err != nil {
ext.Error.Set(span, true)
return nil, errors.WithStack(err)
}
return s, nil
}
func (b *TracedBCrypt) Compare(ctx context.Context, hash, data []byte) error {
span, _ := opentracing.StartSpanFromContext(ctx, CompareOpName)
defer span.Finish()
span.SetTag(WorkFactorTagName, b.WorkFactor)
if err := bcrypt.CompareHashAndPassword(hash, data); err != nil {
ext.Error.Set(span, true)
return errors.WithStack(err)
}
return nil
}