mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat: add AesGcmEncrypt and AesGcmDecrypt in cryptor package
This commit is contained in:
@@ -244,6 +244,56 @@ func AesOfbDecrypt(data, key []byte) []byte {
|
|||||||
return decrypted
|
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
|
// DesEcbEncrypt encrypt data with key use DES ECB algorithm
|
||||||
// len(key) should be 8.
|
// len(key) should be 8.
|
||||||
// Play: https://go.dev/play/p/8qivmPeZy4P
|
// Play: https://go.dev/play/p/8qivmPeZy4P
|
||||||
|
|||||||
@@ -129,6 +129,34 @@ func ExampleAesOfbDecrypt() {
|
|||||||
// hello
|
// 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() {
|
func ExampleDesEcbEncrypt() {
|
||||||
data := "hello"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
|
|||||||
@@ -168,3 +168,17 @@ func TestRsaEncryptOAEP(t *testing.T) {
|
|||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal("hello world", string(decrypted))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ import (
|
|||||||
- [AesCfbDecrypt](#AesCfbDecrypt)
|
- [AesCfbDecrypt](#AesCfbDecrypt)
|
||||||
- [AesOfbEncrypt](#AesOfbEncrypt)
|
- [AesOfbEncrypt](#AesOfbEncrypt)
|
||||||
- [AesOfbDecrypt](#AesOfbDecrypt)
|
- [AesOfbDecrypt](#AesOfbDecrypt)
|
||||||
|
- [AesGcmEncrypt](#AesGcmEncrypt)
|
||||||
|
- [AesGcmDecrypt](#AesGcmDecrypt)
|
||||||
- [Base64StdEncode](#Base64StdEncode)
|
- [Base64StdEncode](#Base64StdEncode)
|
||||||
- [Base64StdDecode](#Base64StdDecode)
|
- [Base64StdDecode](#Base64StdDecode)
|
||||||
- [DesEcbEncrypt](#DesEcbEncrypt)
|
- [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>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>将字符串base64编码。</p>
|
<p>将字符串base64编码。</p>
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ import (
|
|||||||
- [AesCfbDecrypt](#AesCfbDecrypt)
|
- [AesCfbDecrypt](#AesCfbDecrypt)
|
||||||
- [AesOfbEncrypt](#AesOfbEncrypt)
|
- [AesOfbEncrypt](#AesOfbEncrypt)
|
||||||
- [AesOfbDecrypt](#AesOfbDecrypt)
|
- [AesOfbDecrypt](#AesOfbDecrypt)
|
||||||
|
- [AesGcmEncrypt](#AesGcmEncrypt)
|
||||||
|
- [AesGcmDecrypt](#AesGcmDecrypt)
|
||||||
- [Base64StdEncode](#Base64StdEncode)
|
- [Base64StdEncode](#Base64StdEncode)
|
||||||
- [Base64StdDecode](#Base64StdDecode)
|
- [Base64StdDecode](#Base64StdDecode)
|
||||||
- [DesEcbEncrypt](#DesEcbEncrypt)
|
- [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>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>Encode string with base64 encoding.</p>
|
<p>Encode string with base64 encoding.</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user