From c74509774978b799fe28316d02d3cd1cc36188b5 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 10 Sep 2024 10:37:47 +0800 Subject: [PATCH] feat: add AesGcmEncrypt and AesGcmDecrypt in cryptor package --- cryptor/crypto.go | 50 +++++++++++++++++++++++ cryptor/crypto_example_test.go | 28 +++++++++++++ cryptor/crypto_test.go | 14 +++++++ docs/api/packages/cryptor.md | 70 +++++++++++++++++++++++++++++++++ docs/en/api/packages/cryptor.md | 70 +++++++++++++++++++++++++++++++++ 5 files changed, 232 insertions(+) diff --git a/cryptor/crypto.go b/cryptor/crypto.go index 2f493f4..9a1c6c8 100644 --- a/cryptor/crypto.go +++ b/cryptor/crypto.go @@ -244,6 +244,56 @@ func AesOfbDecrypt(data, key []byte) []byte { return decrypted } +// AesGcmEncrypt encrypt data with key use AES GCM algorithm +// Play: todo +func AesGcmEncrypt(data, key []byte) []byte { + block, err := aes.NewCipher(key) + if err != nil { + panic(err) + } + + gcm, err := cipher.NewGCM(block) + if err != nil { + panic(err) + } + + nonce := make([]byte, gcm.NonceSize()) + if _, err = io.ReadFull(rand.Reader, nonce); err != nil { + panic(err) + } + + ciphertext := gcm.Seal(nonce, nonce, data, nil) + + return ciphertext +} + +// AesGcmDecrypt decrypt data with key use AES GCM algorithm +// Play: todo +func AesGcmDecrypt(data, key []byte) []byte { + block, err := aes.NewCipher(key) + if err != nil { + panic(err) + } + + gcm, err := cipher.NewGCM(block) + if err != nil { + panic(err) + } + + nonceSize := gcm.NonceSize() + if len(data) < nonceSize { + panic("ciphertext too short") + } + + nonce, ciphertext := data[:nonceSize], data[nonceSize:] + plaintext, err := gcm.Open(nil, nonce, ciphertext, nil) + if err != nil { + panic(err) + } + + return plaintext +} + // DesEcbEncrypt encrypt data with key use DES ECB algorithm // len(key) should be 8. // Play: https://go.dev/play/p/8qivmPeZy4P diff --git a/cryptor/crypto_example_test.go b/cryptor/crypto_example_test.go index 574da99..30405b0 100644 --- a/cryptor/crypto_example_test.go +++ b/cryptor/crypto_example_test.go @@ -129,6 +129,34 @@ func ExampleAesOfbDecrypt() { // hello } +func ExampleAesGcmEncrypt() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := AesGcmEncrypt([]byte(data), []byte(key)) + + decrypted := AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} + +func ExampleAesGcmDecrypt() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := AesGcmEncrypt([]byte(data), []byte(key)) + + decrypted := AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} + func ExampleDesEcbEncrypt() { data := "hello" key := "abcdefgh" diff --git a/cryptor/crypto_test.go b/cryptor/crypto_test.go index 58813c9..86f4218 100644 --- a/cryptor/crypto_test.go +++ b/cryptor/crypto_test.go @@ -168,3 +168,17 @@ func TestRsaEncryptOAEP(t *testing.T) { assert.IsNil(err) assert.Equal("hello world", string(decrypted)) } + +func TestAesGcmEncrypt(t *testing.T) { + + t.Parallel() + + data := "hello world" + key := "abcdefghijklmnop" + + encrypted := AesGcmEncrypt([]byte(data), []byte(key)) + decrypted := AesGcmDecrypt(encrypted, []byte(key)) + + assert := internal.NewAssert(t, "TestAesGcmEncrypt") + assert.Equal(data, string(decrypted)) +} diff --git a/docs/api/packages/cryptor.md b/docs/api/packages/cryptor.md index 12bb688..8882631 100644 --- a/docs/api/packages/cryptor.md +++ b/docs/api/packages/cryptor.md @@ -32,6 +32,8 @@ import ( - [AesCfbDecrypt](#AesCfbDecrypt) - [AesOfbEncrypt](#AesOfbEncrypt) - [AesOfbDecrypt](#AesOfbDecrypt) +- [AesGcmEncrypt](#AesGcmEncrypt) +- [AesGcmDecrypt](#AesGcmDecrypt) - [Base64StdEncode](#Base64StdEncode) - [Base64StdDecode](#Base64StdDecode) - [DesEcbEncrypt](#DesEcbEncrypt) @@ -379,6 +381,74 @@ func main() { } ``` +### AesGcmEncrypt + +

使用AES GCM算法模式加密数据。

+ +函数签名: + +```go +func AesGcmEncrypt(data, key []byte) []byte +``` + +示例:[运行]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} +``` + +### AesGcmDecrypt + +

使用AES GCM算法解密数据。

+ +函数签名: + +```go +func AesGcmDecrypt(data, key []byte) []byte +``` + +示例:[运行]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} +``` + ### Base64StdEncode

将字符串base64编码。

diff --git a/docs/en/api/packages/cryptor.md b/docs/en/api/packages/cryptor.md index 44a28bf..7b564ca 100644 --- a/docs/en/api/packages/cryptor.md +++ b/docs/en/api/packages/cryptor.md @@ -32,6 +32,8 @@ import ( - [AesCfbDecrypt](#AesCfbDecrypt) - [AesOfbEncrypt](#AesOfbEncrypt) - [AesOfbDecrypt](#AesOfbDecrypt) +- [AesGcmEncrypt](#AesGcmEncrypt) +- [AesGcmDecrypt](#AesGcmDecrypt) - [Base64StdEncode](#Base64StdEncode) - [Base64StdDecode](#Base64StdDecode) - [DesEcbEncrypt](#DesEcbEncrypt) @@ -379,6 +381,74 @@ func main() { } ``` +### AesGcmEncrypt + +

Encrypt data with key use AES GCM algorithm.

+ +Signature: + +```go +func AesGcmEncrypt(data, key []byte) []byte +``` + +Example:[Run]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} +``` + +### AesGcmDecrypt + +

Decrypt data with key use AES GCM algorithm.

+ +Signature: + +```go +func AesGcmDecrypt(data, key []byte) []byte +``` + +Example:[Run]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/cryptor" +) + +func main() { + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello +} +``` + ### Base64StdEncode

Encode string with base64 encoding.