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(src []byte, key string, cipher Cipher) string
- func IsEncrypt(str string) bool
- func Padding(cipherText []byte, blockSize int) []byte
- func UnPadding(cipherText []byte) ([]byte, error)
- type Cfg
- type Cipher
- func (p Cipher) Block(key string) cipher.Block
- func (p Cipher) BlockMode(key string, isEnc bool) (cipher.BlockMode, int)
- func (p Cipher) Decrypt(src, key string) (string, error)
- func (p Cipher) DecryptBytes(src []byte, key string) ([]byte, error)
- func (p Cipher) EncodeToString(src, key string) string
- func (p Cipher) Encrypt(src, key string) string
- func (p Cipher) EncryptBytes(src []byte, key string) []byte
- func (p Cipher) String() string
- func (p Cipher) Stringify(data []byte) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Decrypt ¶
Decrypt 解密.
Example (Md5) ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { fmt.Println(cfg.AESMD5.Decrypt("lob52vO/Av/yk0Ty+DBDag==", "pass")) }
Output: abc <nil>
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 ¶
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(_file) 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(_file) 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(_file) fmt.Println(err)
Output: <nil>
type Cipher ¶
type Cipher int
Example (Decrypt2) ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { fmt.Println( cfg.DES.Decrypt("HLZn0k/XGi5fmu2OCXsOuNdsstzF/2JCsY7q38hQEwSYFpGlO036ypruiYtMJmDB64JNZiiYh0sntLsphrX36A==", "woda"), ) }
Output: TXprMU5tRTVZV1pqTW1Kak5ESTVNV0l4TkdNME4yRmhOMlppTURZM05HVQ== <nil>
func (Cipher) Decrypt ¶ added in v1.0.42
Example ¶
package main import ( "fmt" "github.com/xuender/kit/cfg" ) func main() { fmt.Println(cfg.AESMD5.Decrypt(cfg.AESMD5.Encrypt("AESMD5", "pass"), "pass")) fmt.Println(cfg.DESMD5.Decrypt(cfg.DESMD5.Encrypt("DESMD5", "pass"), "pass")) fmt.Println(cfg.AES.Decrypt(cfg.AES.Encrypt("AES", "pass"), "pass")) fmt.Println(cfg.DES.Decrypt(cfg.DES.Encrypt("DES", "pass"), "pass")) }
Output: AESMD5 <nil> DESMD5 <nil> AES <nil> DES <nil>
func (Cipher) DecryptBytes ¶ added in v1.0.42
func (Cipher) EncodeToString ¶ added in v1.0.43
func (Cipher) EncryptBytes ¶ added in v1.0.42
Click to show internal directories.
Click to hide internal directories.