Skip to content

Commit 9a7096c

Browse files
committed
hmac: tidy interface
1 parent 7aa10a3 commit 9a7096c

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

handler/handler.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package handler
33
import (
44
"bufio"
55
"bytes"
6-
chmac "crypto/hmac"
7-
"crypto/sha256"
86
"fmt"
97
"io"
108

@@ -30,7 +28,7 @@ func (a *BasicHMACAuthHandler) Run(input io.Reader, output io.Writer) error {
3028
rd := bufio.NewReaderSize(input, bufSize)
3129
scanner := proto.NewElasticLineScanner(rd, '\n')
3230

33-
mac := chmac.New(sha256.New, a.Secret)
31+
mac := hmac.NewHasher(a.Secret)
3432

3533
for scanner.Scan() {
3634
parts := bytes.SplitN(scanner.Bytes(), []byte{' '}, 4)

hmac/hmac.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hmac
22

33
import (
44
"crypto/hmac"
5+
"crypto/sha256"
56
"encoding/base64"
67
"encoding/binary"
78
"hash"
@@ -15,6 +16,10 @@ const (
1516

1617
var hmacSignaturePrefix = []byte(HMACSignaturePrefix)
1718

19+
func NewHasher(secret []byte) hash.Hash {
20+
return hmac.New(sha256.New, secret)
21+
}
22+
1823
func VerifyHMACLoginAndPassword(mac hash.Hash, login, password []byte) bool {
1924
buf := make([]byte, base64.RawURLEncoding.DecodedLen(len(password)))
2025
n, err := base64.RawURLEncoding.Decode(buf, password)

0 commit comments

Comments
 (0)