cfg

package
v1.0.38 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 15 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
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

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

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>

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) (string, 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("test.toml")
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("test.toml")
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("test.toml")
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
const (
	AES Cipher = iota
	DES
)

func Parse

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

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

func (Cipher) Block

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

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