https://github.com/leo60081/gostructcrypto
一個簡單使用 防呆的結構加密套件
https://github.com/leo60081/gostructcrypto
aes base64 cbc crypto ecb encode foolproof golang structure
Last synced: 22 days ago
JSON representation
一個簡單使用 防呆的結構加密套件
- Host: GitHub
- URL: https://github.com/leo60081/gostructcrypto
- Owner: leo60081
- License: mit
- Created: 2019-04-09T01:14:26.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-06-18T16:50:58.000Z (about 6 years ago)
- Last Synced: 2025-12-17T09:15:53.240Z (6 months ago)
- Topics: aes, base64, cbc, crypto, ecb, encode, foolproof, golang, structure
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoStructCrypto
### 一個簡單使用 防呆的結構加密套件
## 特性
1. 使用簡單
2. AES ECB (預設) AES CBC
3. Base64 編碼 (預設)
4. 支援自定義加密 (對稱式加密算法)
5. 支援自訂義編碼方法
6. 加密結構宣告維護簡易
7. 防呆設計 避免加解密次數不對等 導致的資料錯誤
## 簡易範例:
```
package main
import (
"fmt"
"encoding/json"
"github.com/leo60081/gostructcrypto"
)
type person struct {
ID int
Name string
Password string `crypto:"required"`
Addr string `crypto:"required" encode:"required"`
gostructcrypto.Crypto `-`
}
func main(){
sample := &person{
ID: 1234567890,
Name: "LeoChen",
Password: "112233445566",
Addr: "100台北市中正區重慶南路一段122號",
}
gostructcrypto.SetGlobalKey("Happy_gostructcrypto")
gostructcrypto.EnCryptoStruct(sample)
b, _ := json.Marshal(sample)
fmt.Printf("Plaintext: %+v\n", string(b))
gostructcrypto.DecryptoStruct(sample)
b, _ = json.Marshal(sample)
fmt.Printf("Ciphertext: %+v\n", string(b))
}
```
## 進階範例:
```
package main
import (
"fmt"
"encoding/json"
"github.com/leo60081/gostructcrypto"
)
type person struct {
ID int
Name string
Password string `crypto:"required"`
Addr string `crypto:"required" encode:"required"`
Tags *[]string `crypto:"required" encode:"required"`
Habbits *habit
gostructcrypto.Crypto `-`
}
type habit struct {
Items []string `crypto:"required" encode:"required"`
gostructcrypto.Crypto `-`
}
func main() {
sample := &person{
ID: 1234567890,
Name: "LeoChen",
Password: "112233445566",
Addr: "100台北市中正區重慶南路一段122號",
Tags: func() *[]string {
s := []string{"abc", "efg", "hij"}
return &s
}(),
Habbits: &habit{
Items: []string{"coding", "golang", "sleeping"},
},
}
sample.SetKey([]byte("Happy_gostructcrypto"))
sample.Habbits.SetKey([]byte("qWrsXdRTb2WE$etpdlsDFTR2#!QA;?de"))
cbc := aes.NewAseCBC([]byte("plqawskiedcvfgbr"))
sample.Habbits.SetEnCryptoFunc(cbc.Encrypt)
sample.Habbits.SetDeCryptoFunc(cbc.Decrypt)
gostructcrypto.EnCryptoStruct(sample)
b, _ := json.Marshal(sample)
fmt.Printf("Plaintext: %+v\n", string(b))
gostructcrypto.DecryptoStruct(sample)
b, _ = json.Marshal(sample)
fmt.Printf("Ciphertext: %+v\n", string(b))
}
```