Skip to content

Commit df15149

Browse files
committed
Added paragraph about HMAC signing and string
1 parent 12f1b31 commit df15149

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

docs/usage/signing_methods.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ Each signing method expects a different object type for its signing keys. The fo
3535
[^ecdsa]: [Section 3.2 of RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518#section-3.4)
3636
[^rsapss]: [Section 3.2 of RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518#section-3.5)
3737
[^eddsa]: [Section 3.1 of RFC 8037](https://datatracker.ietf.org/doc/html/rfc8037#section-3.1)
38+
39+
**Why is the HMAC signing method not accepting `string` as a key type?**
40+
41+
We often get asked why the HMAC signing method only supports `[]byte` and not `string`. This is intentionally and there are different reasons for doing so. First, we aim to use the key type that the underlying cryptographic operation in the Go library uses (this also applies to the other signing methods). In case of HMAC, this is [`hmac.New`](https://pkg.go.dev/crypto/hmac#New) and it uses `[]byte` as the type to represent a key.
42+
43+
Second, using `string` as a key type to represent a symmetric key can lead to unwanted situations. It gives the impression that this is something 'human readable' (like a password), but it is not. A symmetric key should contain as much entropy as possible and therefore include characters from the whole character set (even 'unreadable' ones) and ideally be generated by a cryptographic random source, such as [`rand.Read`](https://pkg.go.dev/crypto/rand#Read). Signing tokens with a cryptographically weak key will compromise the security of the tokens and in effect everything that depends on it, e.g., user authentication.
44+
45+
If you have trouble handling a `[]byte` key in our setup, e.g., because you are reading it from your environment variables on your cluster or similar, you can always use base64 encoding to have the key as a "string" type outside of your program and then use [`base64.Encoding.DecodeString`](https://pkg.go.dev/encoding/base64#Encoding.DecodeString) to decode the base64 string into the `[]byte` slice that the signing method needs.

0 commit comments

Comments
 (0)