1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add AesGcmEncrypt and AesGcmDecrypt in cryptor package

This commit is contained in:
dudaodong
2024-09-10 10:37:47 +08:00
parent ba75e58e5f
commit c745097749
5 changed files with 232 additions and 0 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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))
}

View File

@@ -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() {
}
```
### <span id="AesGcmEncrypt">AesGcmEncrypt</span>
<p>使用AES GCM算法模式加密数据。</p>
<b>函数签名:</b>
```go
func AesGcmEncrypt(data, key []byte) []byte
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```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
}
```
### <span id="AesGcmDecrypt">AesGcmDecrypt</span>
<p>使用AES GCM算法解密数据。</p>
<b>函数签名:</b>
```go
func AesGcmDecrypt(data, key []byte) []byte
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```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
}
```
### <span id="Base64StdEncode">Base64StdEncode</span>
<p>将字符串base64编码。</p>

View File

@@ -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() {
}
```
### <span id="AesGcmEncrypt">AesGcmEncrypt</span>
<p>Encrypt data with key use AES GCM algorithm.</p>
<b>Signature:</b>
```go
func AesGcmEncrypt(data, key []byte) []byte
```
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
```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
}
```
### <span id="AesGcmDecrypt">AesGcmDecrypt</span>
<p>Decrypt data with key use AES GCM algorithm.</p>
<b>Signature:</b>
```go
func AesGcmDecrypt(data, key []byte) []byte
```
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
```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
}
```
### <span id="Base64StdEncode">Base64StdEncode</span>
<p>Encode string with base64 encoding.</p>