cfg

package
v1.0.50 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package cfg 读取可加密配置信息. 对配置文件中占位符 AES[明文] 或 DES[明文] 加密,并返回明文. 原有占位符被替换成 AES(密文) 或 DES(密文). 主要方法 cfg.New(password).Bytes(path), cfg.New(password).Reader(path), cfg.New(password).String(path).

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnpaddingLength = errors.New("invalid unpadding length")
	ErrNoCipher        = errors.New("no cipher")
	ErrKey             = errors.New("password error")
)

Functions

func Decrypt

func Decrypt(src, key string) (string, error)

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

func Encrypt(str, key string) (string, error)
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

func EncryptByCipher(src []byte, key string, cipher Cipher) string
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>

func IsEncrypt

func IsEncrypt(str string) bool

IsEncrypt 是否加密.

Example
package main

import (
	"fmt"

	"github.com/xuender/kit/cfg"
)

func main() {
	fmt.Println(cfg.IsEncrypt("AES(A/43wTj2AVQboZZ0lNMqbw==)"))
	fmt.Println(cfg.IsEncrypt("xxx"))

}
Output:

true
false

func Padding

func Padding(cipherText []byte, blockSize int) []byte

func UnPadding

func UnPadding(cipherText []byte) ([]byte, error)

Types

type Cfg added in v1.0.34

type Cfg struct {
	// contains filtered or unexported fields
}

func New added in v1.0.34

func New(password string) *Cfg

func (*Cfg) Bytes added in v1.0.34

func (p *Cfg) Bytes(path string) ([]byte, error)

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

func (p *Cfg) Read(data []byte) ([]byte, error)

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

func (p *Cfg) Reader(path string) (io.Reader, error)

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

func (p *Cfg) String(path string) (string, error)

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>

func (*Cfg) Write added in v1.0.34

func (p *Cfg) Write(data []byte) []byte

Write 明文写入成秘文.

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>
const (
	AES Cipher = iota
	DES
	AESMD5
	DESMD5
)

func Parse

func Parse(str string) ([]byte, Cipher, error)

Parse 解析密文, 返回数据和加密算法.

func (Cipher) Block

func (p Cipher) Block(key string) cipher.Block

func (Cipher) BlockMode added in v1.0.43

func (p Cipher) BlockMode(key string, isEnc bool) (cipher.BlockMode, int)

func (Cipher) Decrypt added in v1.0.42

func (p Cipher) Decrypt(src, key string) (string, error)
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 (p Cipher) DecryptBytes(src []byte, key string) ([]byte, error)

func (Cipher) EncodeToString added in v1.0.43

func (p Cipher) EncodeToString(src, key string) string

func (Cipher) Encrypt added in v1.0.42

func (p Cipher) Encrypt(src, key string) string

func (Cipher) EncryptBytes added in v1.0.42

func (p Cipher) EncryptBytes(src []byte, key string) []byte

func (Cipher) String

func (p Cipher) String() string

func (Cipher) Stringify

func (p Cipher) Stringify(data []byte) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL