Documentation ¶
Overview ¶
Package cfg 读取可加密配置信息. 对配置文件中占位符 AES[明文] 或 DES[明文] 加密,并返回明文. 原有占位符被替换成 AES(密文) 或 DES(密文). 主要方法 cfg.New(password).Bytes(path), cfg.New(password).Reader(path), cfg.New(password).String(path).
Index ¶
- Variables
- func Decrypt(src, key string) (string, error)
- func Encrypt(str, key string) (string, error)
- func EncryptByCipher(str []byte, key string, cipher Cipher) string
- func IsEncrypt(str string) bool
- func Padding(cipherText []byte, blockSize int) []byte
- func UnPadding(cipherText []byte) (string, error)
- type Cfg
- type Cipher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Decrypt ¶
Decrypt 解密.
Example ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { fmt.Println(cfg.Decrypt("AES(A/43wTj2AVQboZZ0lNMqbw==)", "key")) str := cfg.EncryptByCipher([]byte("123"), "", cfg.DES) fmt.Println(cfg.Decrypt(str, "")) _, err := cfg.Decrypt(str, "err") fmt.Println(err) }
Output: aaa <nil> 123 <nil> password error
func Encrypt ¶
Example ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { str, err := cfg.Encrypt("AES[123]", "password") fmt.Println(str[:4]) fmt.Println(err) fmt.Println(cfg.Decrypt(str, "password")) }
Output: AES( <nil> 123 <nil>
func EncryptByCipher ¶
EncryptByCipher 加密.
Example ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { str := cfg.EncryptByCipher([]byte("123"), "password", cfg.AES) fmt.Println(str[:4]) fmt.Println(cfg.Decrypt(str, "password")) }
Output: AES( 123 <nil>
Types ¶
type Cfg ¶ added in v1.0.34
type Cfg struct {
// contains filtered or unexported fields
}
func (*Cfg) Bytes ¶ added in v1.0.34
Bytes 配置转字节.
Example ¶
patchRead := gomonkey.ApplyFuncReturn(os.ReadFile, []byte(_data), nil) defer patchRead.Reset() patchWrite := gomonkey.ApplyFuncReturn(os.WriteFile, nil) defer patchWrite.Reset() _, err := cfg.New("key").Bytes("test.toml") fmt.Println(err)
Output: <nil>
func (*Cfg) Read ¶ added in v1.0.34
Read 秘文读取成明文.
Example ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { data, err := cfg.New("key").Read([]byte(`a=AES(A/43wTj2AVQboZZ0lNMqbw==) b=DES(LABOK5l6Q64=) c=DES[abc]`)) fmt.Println(string(data)) fmt.Println(err) }
Output: a=aaa b=test2 c=abc <nil>
func (*Cfg) Reader ¶ added in v1.0.34
Reader 配置转 io.Reader .
Example ¶
patchRead := gomonkey.ApplyFuncReturn(os.ReadFile, []byte(_data), nil) defer patchRead.Reset() patchWrite := gomonkey.ApplyFuncReturn(os.WriteFile, nil) defer patchWrite.Reset() _, err := cfg.New("key").Reader("test.toml") fmt.Println(err)
Output: <nil>
func (*Cfg) String ¶ added in v1.0.34
String 配置转字符串.
Example ¶
patchRead := gomonkey.ApplyFuncReturn(os.ReadFile, []byte(_data), nil) defer patchRead.Reset() patchWrite := gomonkey.ApplyFuncReturn(os.WriteFile, nil) defer patchWrite.Reset() _, err := cfg.New("key").String("test.toml") fmt.Println(err)
Output: <nil>
Click to show internal directories.
Click to hide internal directories.