Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encrypter): Added an Encrypter #65

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions encrypter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Encrypter

## Usage

```go
package main

import "github.com/go-kratos-ecosystem/components/v2/encrypter"

func main() {
e := encrypter.New("EAFBSPAXDCIOGRUVNERQGXPYGPNKYATM")

ciphertext, _ := e.Encrypt("test")
plaintext, _ := e.Decrypt(ciphertext)

println(ciphertext, plaintext) // I-UVz6tds3MlRX-1afR36cLcmMw= test
}
```
80 changes: 80 additions & 0 deletions encrypter/encrypter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package encrypter

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)

type Encrypter struct {
key []byte
cipher cipher.Block
}

func New(key string) *Encrypter {
return &Encrypter{
key: []byte(key),
}
}

func (e *Encrypter) Encrypt(value string) (string, error) {
plaintext := []byte(value)

block, err := e.getBlock()
if err != nil {
return "", err
}

ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}

Check warning on line 35 in encrypter/encrypter.go

View check run for this annotation

Codecov / codecov/patch

encrypter/encrypter.go#L34-L35

Added lines #L34 - L35 were not covered by tests

stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

return base64.URLEncoding.EncodeToString(ciphertext), nil
}

func (e *Encrypter) Decrypt(value string) (string, error) {
ciphertext, err := base64.URLEncoding.DecodeString(value)
if err != nil {
return "", err
}

block, err := e.getBlock()
if err != nil {
return "", err
}

if len(ciphertext) < aes.BlockSize {
return "", fmt.Errorf("ciphertext too short")
}

iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]

stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)

return string(ciphertext), nil
}

func (e *Encrypter) getBlock() (cipher.Block, error) {
if e.cipher != nil {
return e.cipher, nil
}

c, err := aes.NewCipher(e.key)
if err != nil {
return nil, err
}

e.cipher = c

return c, nil
}
46 changes: 46 additions & 0 deletions encrypter/encrypter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package encrypter

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
assert.Equal(t, &Encrypter{
key: []byte("test"),
}, New("test"))
}

func TestEncrypter(t *testing.T) {
e := New("EAFBSPAXDCIOGRUVNERQGXPYGPNKYATM")

ciphertext, _ := e.Encrypt("test")
plaintext, _ := e.Decrypt(ciphertext)

println(ciphertext, plaintext)

assert.NotNil(t, ciphertext)
assert.NotNil(t, plaintext)
assert.Equal(t, "test", plaintext)
}

func TestEncrypter_Error(t *testing.T) {
e := New("test")

_, err1 := e.Encrypt("test")
assert.Error(t, err1)

_, err2 := e.Decrypt("test")
assert.Error(t, err2)
}

func TestEncrypter_Decrypt_Error(t *testing.T) {
e := New("EAFBSPAXDCIOGRUVNERQGXPYGPNKYATM")

_, err1 := e.Decrypt("j9_mcZXKVlInk8bbpBqJOpmDp")
assert.Error(t, err1)

_, err2 := e.Decrypt("MQ==")
assert.Error(t, err2)
}