Skip to content

Commit 77967e5

Browse files
committed
kms: Added Hash function and associated tests
1 parent fa2455c commit 77967e5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

kms.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func (s *SigningMethodKMS) Override() {
9797
})
9898
}
9999

100+
// Hash will return the crypto.Hash used for this signing method
101+
func (s *SigningMethodKMS) Hash() crypto.Hash {
102+
return s.hasher
103+
}
104+
100105
// Sign implements the Sign method from jwt.SigningMethod. For this signing method, a valid context.Context must be
101106
// passed as the key containing a KMSConfig value.
102107
// https://cloud.google.com/kms/docs/create-validate-signatures#kms-howto-sign-go

kms_test.go

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

33
import (
44
"context"
5+
"crypto"
56
"encoding/json"
67
"fmt"
78
"io/ioutil"
@@ -243,3 +244,38 @@ func TestSigningMethodKMS_Sign(t *testing.T) {
243244
})
244245
}
245246
}
247+
248+
func TestSigningMethodKMS_Hash(t *testing.T) {
249+
type fields struct {
250+
alg string
251+
override jwt.SigningMethod
252+
hasher crypto.Hash
253+
}
254+
tests := []struct {
255+
name string
256+
fields fields
257+
want crypto.Hash
258+
}{
259+
{
260+
"SimpleTest",
261+
fields{
262+
"RS256",
263+
jwt.SigningMethodRS256,
264+
jwt.SigningMethodRS256.Hash,
265+
},
266+
jwt.SigningMethodRS256.Hash,
267+
},
268+
}
269+
for _, tt := range tests {
270+
t.Run(tt.name, func(t *testing.T) {
271+
s := &SigningMethodKMS{
272+
alg: tt.fields.alg,
273+
override: tt.fields.override,
274+
hasher: tt.fields.hasher,
275+
}
276+
if got := s.Hash(); got != tt.want {
277+
t.Errorf("SigningMethodKMS.Hash() = %v, want %v", got, tt.want)
278+
}
279+
})
280+
}
281+
}

0 commit comments

Comments
 (0)