Skip to content

Commit 2c4db06

Browse files
committed
enhance docs
1 parent aa8a984 commit 2c4db06

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

enc/enc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ func New(key string) Enc {
9898

9999
// Encrypt encrypts the plainTextMsg using XChaCha20-Poly1305 and returns encrypted bytes.
100100
func (e Enc) Encrypt(plainTextMsg string) (encryptedMsg []byte) {
101-
msgToEncryt := []byte(plainTextMsg)
101+
msgToEncrypt := []byte(plainTextMsg)
102102

103103
// Select a random nonce, and leave capacity for the ciphertext.
104104
nonce := random(
105105
e.aead.NonceSize(),
106-
e.aead.NonceSize()+len(msgToEncryt)+e.aead.Overhead(),
106+
e.aead.NonceSize()+len(msgToEncrypt)+e.aead.Overhead(),
107107
)
108108

109109
// Encrypt the message and append the ciphertext to the nonce.
110-
encrypted := e.aead.Seal(nonce, nonce, msgToEncryt, nil)
110+
encrypted := e.aead.Seal(nonce, nonce, msgToEncrypt, nil)
111111

112112
encrypted = append(
113113
// "you can send the nonce in the clear before each message; so long as it's unique." - agl
@@ -122,7 +122,7 @@ func (e Enc) Encrypt(plainTextMsg string) (encryptedMsg []byte) {
122122
return encrypted
123123
}
124124

125-
// Decrypt un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decryted bytes.
125+
// Decrypt un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decrypted bytes.
126126
func (e Enc) Decrypt(encryptedMsg []byte) (decryptedMsg []byte, err error) {
127127
if len(encryptedMsg) < e.aead.NonceSize() {
128128
return nil, errors.New("ciphertext too short")

enc/enc_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,30 @@ func TestEnc(t *testing.T) {
3131
t.Run("encrypt/decrypt", func(t *testing.T) {
3232
t.Parallel()
3333

34-
msgToEncryt := "hello world!"
34+
msgToEncrypt := "hello world!"
3535
key := getSecretKey()
3636
enc := New(key)
3737

38-
encryptedMsg := enc.Encrypt(msgToEncryt)
38+
encryptedMsg := enc.Encrypt(msgToEncrypt)
3939

4040
decryptedMsg, err := enc.Decrypt(encryptedMsg)
4141
attest.Ok(t, err)
4242

43-
attest.Equal(t, string(decryptedMsg), msgToEncryt)
43+
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
4444
})
4545

4646
t.Run("encrypt/decrypt base64", func(t *testing.T) {
4747
t.Parallel()
4848

49-
msgToEncryt := "hello world!"
49+
msgToEncrypt := "hello world!"
5050
key := getSecretKey()
5151
enc := New(key)
5252

53-
token := enc.EncryptEncode(msgToEncryt)
53+
token := enc.EncryptEncode(msgToEncrypt)
5454

5555
decryptedMsg, err := enc.DecryptDecode(token)
5656
attest.Ok(t, err)
57-
attest.Equal(t, string(decryptedMsg), msgToEncryt)
57+
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
5858
})
5959

6060
t.Run("encrypt same msg is unique", func(t *testing.T) {
@@ -63,27 +63,27 @@ func TestEnc(t *testing.T) {
6363
// This is a useful property especially in how we use it in csrf protection
6464
// against breachattack.
6565

66-
msgToEncryt := "hello world!"
66+
msgToEncrypt := "hello world!"
6767
key := getSecretKey()
6868
enc := New(key)
6969

70-
encryptedMsg := enc.Encrypt(msgToEncryt)
70+
encryptedMsg := enc.Encrypt(msgToEncrypt)
7171

7272
var em []byte
7373
for i := 0; i < 4; i++ {
74-
em = enc.Encrypt(msgToEncryt)
74+
em = enc.Encrypt(msgToEncrypt)
7575
if slices.Equal(encryptedMsg, em) {
7676
t.Fatal("slices should not be equal")
7777
}
7878
}
7979

8080
decryptedMsg, err := enc.Decrypt(encryptedMsg)
8181
attest.Ok(t, err)
82-
attest.Equal(t, string(decryptedMsg), msgToEncryt)
82+
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
8383

8484
decryptedMsgForEm, err := enc.Decrypt(em)
8585
attest.Ok(t, err)
86-
attest.Equal(t, string(decryptedMsgForEm), msgToEncryt)
86+
attest.Equal(t, string(decryptedMsgForEm), msgToEncrypt)
8787
})
8888

8989
t.Run("same input key will always be able to encrypt and decrypt", func(t *testing.T) {
@@ -93,31 +93,31 @@ func TestEnc(t *testing.T) {
9393
// A csrf token that was encrypted today, should be able to be decrypted tomorrow
9494
// even if the server was restarted; so long as the same key is re-used.
9595

96-
msgToEncryt := "hello world!"
96+
msgToEncrypt := "hello world!"
9797
key := getSecretKey()
9898

9999
enc1 := New(key)
100-
encryptedMsg := enc1.Encrypt(msgToEncryt)
100+
encryptedMsg := enc1.Encrypt(msgToEncrypt)
101101

102102
enc2 := New(key) // server restarted
103103
decryptedMsg, err := enc2.Decrypt(encryptedMsg)
104104
attest.Ok(t, err)
105-
attest.Equal(t, string(decryptedMsg), msgToEncryt)
105+
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
106106
})
107107

108108
t.Run("concurrency safe", func(t *testing.T) {
109109
t.Parallel()
110110

111-
msgToEncryt := "hello world!"
111+
msgToEncrypt := "hello world!"
112112

113113
run := func() {
114114
key := getSecretKey()
115115
enc := New(key)
116116

117-
encryptedMsg := enc.Encrypt(msgToEncryt)
117+
encryptedMsg := enc.Encrypt(msgToEncrypt)
118118
decryptedMsg, err := enc.Decrypt(encryptedMsg)
119119
attest.Ok(t, err)
120-
attest.Equal(t, string(decryptedMsg), msgToEncryt)
120+
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
121121
}
122122

123123
wg := &sync.WaitGroup{}

middleware/csrf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const (
5353
// If a csrf token is not provided(or is not valid), when it ought to have been; this middleware will issue a http GET redirect to the same url.
5454
func Csrf(wrappedHandler http.HandlerFunc, secretKey, domain string) http.HandlerFunc {
5555
enc := enc.New(secretKey)
56-
msgToEncryt := id.Random(16)
56+
msgToEncrypt := id.Random(16)
5757

5858
return func(w http.ResponseWriter, r *http.Request) {
5959
// - https://docs.djangoproject.com/en/4.0/ref/csrf/
@@ -135,7 +135,7 @@ func Csrf(wrappedHandler http.HandlerFunc, secretKey, domain string) http.Handle
135135
1. http://breachattack.com/
136136
2. https://security.stackexchange.com/a/172646
137137
*/
138-
tokenToIssue := enc.EncryptEncode(msgToEncryt)
138+
tokenToIssue := enc.EncryptEncode(msgToEncrypt)
139139

140140
// 3. create cookie
141141
cookie.Set(

middleware/csrf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestCsrf(t *testing.T) {
300300

301301
key := getSecretKey()
302302
enc := enc.New(key)
303-
reqCsrfTok := enc.EncryptEncode("msgToEncryt")
303+
reqCsrfTok := enc.EncryptEncode("msgToEncrypt")
304304

305305
{
306306
// make GET request
@@ -399,7 +399,7 @@ func TestCsrf(t *testing.T) {
399399

400400
key := getSecretKey()
401401
enc := enc.New(key)
402-
reqCsrfTok := enc.EncryptEncode("msgToEncryt")
402+
reqCsrfTok := enc.EncryptEncode("msgToEncrypt")
403403

404404
{
405405
// make GET request

0 commit comments

Comments
 (0)