Skip to content

Commit 843e9bf

Browse files
dillonstreatorDillon Streatoroxisto
authored
add documentation to hmac Verify & Sign to detail why string is not an advisable input for key (#249)
* add documentation around Verify & Sign to detail why string is not an advisable input for key * Refer to the usage guide --------- Co-authored-by: Dillon Streator <[email protected]> Co-authored-by: Christian Banse <[email protected]>
1 parent 1c4047f commit 843e9bf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

hmac.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ func (m *SigningMethodHMAC) Alg() string {
4545
return m.Name
4646
}
4747

48-
// Verify implements token verification for the SigningMethod. Returns nil if the signature is valid.
48+
// Verify implements token verification for the SigningMethod. Returns nil if
49+
// the signature is valid. Key must be []byte.
50+
//
51+
// Note it is not advised to provide a []byte which was converted from a 'human
52+
// readable' string using a subset of ASCII characters. To maximize entropy, you
53+
// should ideally be providing a []byte key which was produced from a
54+
// cryptographically random source, e.g. crypto/rand. Additional information
55+
// about this, and why we intentionally are not supporting string as a key can
56+
// be found on our usage guide
57+
// https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types.
4958
func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interface{}) error {
5059
// Verify the key is the right type
5160
keyBytes, ok := key.([]byte)
@@ -71,8 +80,14 @@ func (m *SigningMethodHMAC) Verify(signingString string, sig []byte, key interfa
7180
return nil
7281
}
7382

74-
// Sign implements token signing for the SigningMethod.
75-
// Key must be []byte
83+
// Sign implements token signing for the SigningMethod. Key must be []byte.
84+
//
85+
// Note it is not advised to provide a []byte which was converted from a 'human
86+
// readable' string using a subset of ASCII characters. To maximize entropy, you
87+
// should ideally be providing a []byte key which was produced from a
88+
// cryptographically random source, e.g. crypto/rand. Additional information
89+
// about this, and why we intentionally are not supporting string as a key can
90+
// be found on our usage guide https://golang-jwt.github.io/jwt/usage/signing_methods/.
7691
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) ([]byte, error) {
7792
if keyBytes, ok := key.([]byte); ok {
7893
if !m.Hash.Available() {

token.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func NewWithClaims(method SigningMethod, claims Claims, opts ...TokenOption) *To
4242
}
4343

4444
// SignedString creates and returns a complete, signed JWT. The token is signed
45-
// using the SigningMethod specified in the token.
45+
// using the SigningMethod specified in the token. Please refer to
46+
// https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types
47+
// for an overview of the different signing methods and their respective key
48+
// types.
4649
func (t *Token) SignedString(key interface{}) (string, error) {
4750
sstr, err := t.SigningString()
4851
if err != nil {

0 commit comments

Comments
 (0)