Skip to content

Commit a2dc764

Browse files
Dillon StreatorDillon Streator
Dillon Streator
authored and
Dillon Streator
committed
add documentation around Verify & Sign to detail why string is not an advisable input for key
1 parent 0c4e387 commit a2dc764

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

hmac.go

+15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ func (m *SigningMethodHMAC) Alg() string {
4646
}
4747

4848
// Verify implements token verification for the SigningMethod. Returns nil if the signature is valid.
49+
// Key must be []byte
50+
// Note it is not advised to provide a []byte which was converted from a 'human readable' string using a subset of ASCII characters.
51+
// To maximize entropy, you should ideally be providing a []byte key which was produced from a cryptographically random source.
52+
// i.e. crypto/rand https://pkg.go.dev/crypto/rand#Read
53+
//
54+
// Storing keys in the environment can be done by base64 encoding the cryptographically random []byte.
55+
// Reading keys from the environment can be done by base64 decoding the environment variable to retrieve the original cryptographically random []byte.
56+
// i.e. encoding/base64 https://pkg.go.dev/encoding/base64#Encoding.DecodeString
4957
func (m *SigningMethodHMAC) Verify(signingString, signature string, key interface{}) error {
5058
// Verify the key is the right type
5159
keyBytes, ok := key.([]byte)
@@ -79,6 +87,13 @@ func (m *SigningMethodHMAC) Verify(signingString, signature string, key interfac
7987

8088
// Sign implements token signing for the SigningMethod.
8189
// Key must be []byte
90+
// Note it is not advised to provide a []byte which was converted from a 'human readable' string using a subset of ASCII characters.
91+
// To maximize entropy, you should ideally be providing a []byte key which was produced from a cryptographically random source.
92+
// i.e. crypto/rand https://pkg.go.dev/crypto/rand#Read
93+
//
94+
// Storing keys in your environment can be done by base64 encoding the cryptographically random []byte.
95+
// Reading keys from the environment can be done by base64 decoding the environment variable to retrieve the original cryptographically random []byte.
96+
// i.e. encoding/base64 https://pkg.go.dev/encoding/base64#Encoding.DecodeString
8297
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) {
8398
if keyBytes, ok := key.([]byte); ok {
8499
if !m.Hash.Available() {

token.go

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func NewWithClaims(method SigningMethod, claims Claims) *Token {
5555

5656
// SignedString creates and returns a complete, signed JWT.
5757
// The token is signed using the SigningMethod specified in the token.
58+
// Note it is not advised to provide a []byte which was converted from a 'human readable' string using a subset of ASCII characters.
59+
// i.e.
60+
// token.SignedString([]byte(os.Getenv("JWT_SECRET")))
61+
// where `JWT_SECRET“ is some random string containing the subset of ASCII characters which is synonymous with string based keys
62+
//
63+
// To maximize entropy, you should ideally be providing a []byte key which was produced from a cryptographically random source.
64+
// i.e. crypto/rand https://pkg.go.dev/crypto/rand#Read
65+
//
66+
// Storing keys in the environment can be done by base64 encoding the cryptographically random []byte.
67+
// Reading keys from the environment can be done by base64 decoding the environment variable to retrieve the original cryptographically random []byte.
68+
// i.e. encoding/base64 https://pkg.go.dev/encoding/base64#Encoding.DecodeString
5869
func (t *Token) SignedString(key interface{}) (string, error) {
5970
var sig, sstr string
6071
var err error

0 commit comments

Comments
 (0)