Skip to content

Commit

Permalink
enhance docs
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Sep 18, 2022
1 parent aa8a984 commit 2c4db06
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions enc/enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ func New(key string) Enc {

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

// Select a random nonce, and leave capacity for the ciphertext.
nonce := random(
e.aead.NonceSize(),
e.aead.NonceSize()+len(msgToEncryt)+e.aead.Overhead(),
e.aead.NonceSize()+len(msgToEncrypt)+e.aead.Overhead(),
)

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

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

// Decrypt un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decryted bytes.
// Decrypt un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decrypted bytes.
func (e Enc) Decrypt(encryptedMsg []byte) (decryptedMsg []byte, err error) {
if len(encryptedMsg) < e.aead.NonceSize() {
return nil, errors.New("ciphertext too short")
Expand Down
34 changes: 17 additions & 17 deletions enc/enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ func TestEnc(t *testing.T) {
t.Run("encrypt/decrypt", func(t *testing.T) {
t.Parallel()

msgToEncryt := "hello world!"
msgToEncrypt := "hello world!"
key := getSecretKey()
enc := New(key)

encryptedMsg := enc.Encrypt(msgToEncryt)
encryptedMsg := enc.Encrypt(msgToEncrypt)

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

attest.Equal(t, string(decryptedMsg), msgToEncryt)
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
})

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

msgToEncryt := "hello world!"
msgToEncrypt := "hello world!"
key := getSecretKey()
enc := New(key)

token := enc.EncryptEncode(msgToEncryt)
token := enc.EncryptEncode(msgToEncrypt)

decryptedMsg, err := enc.DecryptDecode(token)
attest.Ok(t, err)
attest.Equal(t, string(decryptedMsg), msgToEncryt)
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
})

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

msgToEncryt := "hello world!"
msgToEncrypt := "hello world!"
key := getSecretKey()
enc := New(key)

encryptedMsg := enc.Encrypt(msgToEncryt)
encryptedMsg := enc.Encrypt(msgToEncrypt)

var em []byte
for i := 0; i < 4; i++ {
em = enc.Encrypt(msgToEncryt)
em = enc.Encrypt(msgToEncrypt)
if slices.Equal(encryptedMsg, em) {
t.Fatal("slices should not be equal")
}
}

decryptedMsg, err := enc.Decrypt(encryptedMsg)
attest.Ok(t, err)
attest.Equal(t, string(decryptedMsg), msgToEncryt)
attest.Equal(t, string(decryptedMsg), msgToEncrypt)

decryptedMsgForEm, err := enc.Decrypt(em)
attest.Ok(t, err)
attest.Equal(t, string(decryptedMsgForEm), msgToEncryt)
attest.Equal(t, string(decryptedMsgForEm), msgToEncrypt)
})

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

msgToEncryt := "hello world!"
msgToEncrypt := "hello world!"
key := getSecretKey()

enc1 := New(key)
encryptedMsg := enc1.Encrypt(msgToEncryt)
encryptedMsg := enc1.Encrypt(msgToEncrypt)

enc2 := New(key) // server restarted
decryptedMsg, err := enc2.Decrypt(encryptedMsg)
attest.Ok(t, err)
attest.Equal(t, string(decryptedMsg), msgToEncryt)
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
})

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

msgToEncryt := "hello world!"
msgToEncrypt := "hello world!"

run := func() {
key := getSecretKey()
enc := New(key)

encryptedMsg := enc.Encrypt(msgToEncryt)
encryptedMsg := enc.Encrypt(msgToEncrypt)
decryptedMsg, err := enc.Decrypt(encryptedMsg)
attest.Ok(t, err)
attest.Equal(t, string(decryptedMsg), msgToEncryt)
attest.Equal(t, string(decryptedMsg), msgToEncrypt)
}

wg := &sync.WaitGroup{}
Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
// 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.
func Csrf(wrappedHandler http.HandlerFunc, secretKey, domain string) http.HandlerFunc {
enc := enc.New(secretKey)
msgToEncryt := id.Random(16)
msgToEncrypt := id.Random(16)

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

// 3. create cookie
cookie.Set(
Expand Down
4 changes: 2 additions & 2 deletions middleware/csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestCsrf(t *testing.T) {

key := getSecretKey()
enc := enc.New(key)
reqCsrfTok := enc.EncryptEncode("msgToEncryt")
reqCsrfTok := enc.EncryptEncode("msgToEncrypt")

{
// make GET request
Expand Down Expand Up @@ -399,7 +399,7 @@ func TestCsrf(t *testing.T) {

key := getSecretKey()
enc := enc.New(key)
reqCsrfTok := enc.EncryptEncode("msgToEncryt")
reqCsrfTok := enc.EncryptEncode("msgToEncrypt")

{
// make GET request
Expand Down

0 comments on commit 2c4db06

Please sign in to comment.